Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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)
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
    
bahaha omg i can't believe it was just that ! works like a charm ! thanks a bunch !! :)) – rainerbrunotte Dec 3 at 18:22
    
You are welcome! – Standej Dec 3 at 18:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.