0

I got previousRow of record using this code

<?php
  $previousRow = array();
  while ($temp = mysql_fetch_row($res2)) 
 {

     echo "<br>currentRow:".$temp[1];
     echo "previousRow:".$previousRow[1];
     $previousRow = $temp; 

  } 
 ?>

oupout

currentRow:1previousRow:

currentRow:5previousRow:1

currentRow:6previousRow:5

currentRow:7previousRow:6

currentRow:8previousRow:7

How can I check the value of the next row replaced by Previous Row ?

Any help would be grateful.

3 Answers 3

1

If I get you correctly, then something like this would help?

$previousRow = array();
$currentRow = mysql_fetch_row($res2);

while ($currentRow) {
    $nextRow = mysql_fetch_row($res2);

    echo "<br>currentRow:".$currentRow[1];
    echo "previousRow:".$previousRow[1];
    echo "nextRow:".$nextRow[1];

    $previousRow = $currentRow;
    $currentRow = $nextRow;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Please try code given below.

$res = array();
while ($result = mysql_fetch_row($r)) {
    $res[] = $result;
 }
 echo "<pre>";
 foreach($res AS $index=>$res1){
     echo "Current".$res1[1]; 
     echo "  Next" . $res[$index+1][1];
     echo "  Prev" . $res[$index-1][1]; echo "<br>";
 }

thanks

Comments

0

I'd collect all the rows first, then walk through them with a for:

<?php
$rows = array();
while ($temp = mysql_fetch_row($res2)) 
{
    $rows[] = $temp;
}
$rowCount = count($rows);
for ($i = 0; $i < $rowCount; $i++) {
     echo "<br>currentRow:".$rows[$i][1];
     if ($i > 0) {
         echo "previousRow:".$rows[$i - 1][1];
     }
         if ($i + 1 < $rowCount - 1) {
             echo "nextRow:".$rows[$i + 1][1];
         }
} 
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.