Reputation: 907
I need to check if an image size condition is true for the next and previous MYSQL row / loop (relative to the current while loop) and according to that, arrange the layout of the current loop with PHP.
In order to test and verify that the code is working correctly, I am retrieving the image size per row, and simply want to output the layout condition per loop, as well as the post ID.
The code below is working fine for the current while loop, as well as for the next row item, however I get the same constant value for the previous row item.
Where is my mistake? Is there a better / easier way to do this? Maybe to avoid multiple queries?
<?php
$sql = "SELECT * FROM posts WHERE image_featured!='' ORDER BY id DESC LIMIT 5";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($query)){
$id = $row["id"];
$current_width = $row["width"];
$current_height = $row["height"];
if ($current_width > $current_height){
$current_layout = 'horizontal';
} // end if
if ($current_width == $current_height){
$current_layout = 'square';
} // end if
if ($current_width < $current_height){
$current_layout = 'vertical';
} // end if
echo 'Current: '.$current_layout.' (ID: '.$id.')<br/>';
$nextsql = "SELECT * FROM posts WHERE id > $id AND image_featured!='' LIMIT 1";
$nextquery = mysqli_query($connection, $nextsql);
if(mysqli_num_rows($nextquery) > 0) {
while($nextrow = mysqli_fetch_array($nextquery)){
$next_id = $nextrow['id'];
$next_width = $nextrow["width"];
$next_height = $nextrow["height"];
if ($next_width > $next_height){
$next_layout = 'horizontal';
} // end if
if ($next_width == $next_height){
$next_layout = 'square';
} // end if
if ($next_width < $next_height){
$next_layout = 'vertical';
} // end if
echo 'Next: '.$next_layout.' (ID: '.$next_id.')<br/>';
} // end while
} // end if
$previoussql= "SELECT * FROM posts WHERE id < $id AND image_featured!='' LIMIT 1";
$previousquery = mysqli_query($connection, $previoussql);
if(mysqli_num_rows($previousquery) > 0) {
while($previousrow = mysqli_fetch_array($previousquery)){
$previous_id = $previousrow['id'];
$previous_width = $previousrow["width"];
$previous_height = $previousrow["height"];
if ($previous_width > $previous_height){
$previous_layout = 'horizontal';
} // end if
if ($previous_width == $previous_height){
$previous_layout = 'square';
} // end if
if ($previous_width < $previous_height){
$previous_layout = 'vertical';
} // end if
echo 'Previous: '.$previous_layout.' (ID: '.$previous_id.')<br/>';
} // end while
} // end if
echo '<hr/>';
} // end while 1
?>
Output:
Current: square (ID: 8674)
Previous: square (ID: 76)
Current: horizontal (ID: 8667)
Next: square (ID: 8674)
Previous: square (ID: 76)
Current: horizontal (ID: 8664)
Next: horizontal (ID: 8667)
Previous: square (ID: 76)
Current: horizontal (ID: 8633)
Next: horizontal (ID: 8664)
Previous: square (ID: 76)
Current: square (ID: 8632)
Next: horizontal (ID: 8633)
Previous: square (ID: 76)
Upvotes: 0
Views: 1508
Reputation: 753
Maybe you need to add into your query order by since it always giving you same solution from begining of the table results
$previoussql= "SELECT * FROM posts WHERE id < $id AND image_featured!='' ORDER BY id DESC LIMIT 1";
or maybe ASC since I dont know your table and result set you getting
Upvotes: 2