0

我使用此代码获得了 previousRow of record

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

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

  } 
 ?>

输出

当前行:1上一个行:

当前行:5上一个行:1

当前行:6上一个行:5

当前行:7上一个行:6

当前行:8上一个行:7

如何检查由 Previous Row 替换的下一行的值?

任何帮助将不胜感激。

4

3 回答 3

1

如果我理解正确,那么这样的事情会有所帮助吗?

$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;
}
于 2012-11-08T10:36:46.923 回答
1

请尝试下面给出的代码。

$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>";
 }

谢谢

于 2012-11-08T11:33:39.510 回答
0

我会先收集所有行,然后用 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];
         }
} 
?>
于 2012-11-08T10:34:04.397 回答