我尝试在刷新时显示下一行,但得到一个空白页


I try to display next row on refresh but i get a blank page

嘿,我尝试在刷新时显示下一行,当它到达最后一行时从头开始。这是我的桌子

col1 row(1)
col2 row(1)
col3 row(1)
col4 row(1)
col5 row(1)

并在刷新时获得

col1 row(2)
col2 row(2)
col3 row(2)
col4 row(2)
col5 row(2)

当它到达最后一排时

,从头开始。
col1 row(1)
col2 row(1)
col3 row(1)
col4 row(1)
col5 row(1)

这是到目前为止的代码:

<?php
include 'connect.php';
session_start();
if(isset($_SESSION["id"])){
    $_SESSION["id"] = $_SESSION["id"] + 1;
}
else{
    $_SESSION["id"] = 1;
}
$id = $_SESSION["id"];
//Your mysql connection 
$results = mysqli_query($con, "SELECT * FROM testtable WHERE `id` = $id") or die('AM error occured: ' . mysqli_error());
while($row = mysqli_fetch_array($results)){
    echo $row["content0"];
    echo "<br>";
    echo $row["content1"];
      echo "<br>";
    echo $row["content2"];
      echo "<br>";
    echo $row["content3"];
      echo "<br>";
    echo $row["content4"];
      echo "<br>";
    echo $row["content5"];
}
要在 id

值有间隙时从表中获取下一个 id,请使用此查询

select * 
from testtable 
where id >= if($id > (select max(id) from testtable), 1, $id) 
order by id 
limit 1

并保存在会话变量中选择的 ID

<?php
include 'connect.php';
session_start();
if(isset($_SESSION["id"])){
    $_SESSION["id"] = $_SESSION["id"] + 1;
}
else{
    $_SESSION["id"] = 1;
}
$id = $_SESSION["id"];
//Your mysql connection 
$result=mysqli_query($con,"SELECT count(id) as total from testtable");
$data=mysqli_fetch_assoc($result);
echo $data['total'];
if($data['total']<$id )
{
$_SESSION["id"] = 1;
    $id=1;
} 

$results = mysqli_query($con, "SELECT * FROM testtable WHERE `id` = $id") or die('AM error occured: ' . mysqli_error());
while($row = mysqli_fetch_array($results)){
    echo $row["content0"];
    echo "<br>";
    echo $row["content1"];
      echo "<br>";
    echo $row["content2"];
      echo "<br>";
    echo $row["content3"];
      echo "<br>";
    echo $row["content4"];
      echo "<br>";
    echo $row["content5"];
}

解释:

在这里,我们获得总记录并检查当前刷新的项目,如果大于,则分配给 start(1)

你需要的是分页,可以用 _SESSION 美元做什么。接下来是一个包含数据数组的示例:

<?php
session_start();
// SAMPLE DATA.
$items = array( array( "a","b","c","d","e","f" ),
                array( "1","2","3","4","5","6" ),
                array( "+","-","*","/","&","#" )
              );
if ( ! isset( $_SESSION[ "current" ] ) ) // IF THIS IS THE FIRST TIME.
     $_SESSION[ "current" ] = 0; // FIRST ITEM TO SHOW.
else if ( $_SESSION[ "current" ] < ( count( $items )-1 ) )
          $_SESSION[ "current" ] ++; // NEXT ROW.
     else $_SESSION[ "current" ] = 0; // FIRST ROW AGAIN.
?>
<html>
  <body>
    Showing items :
    <br/>
<?php
// DISPLAY CURRENT ROW.
foreach ( $items[ $_SESSION[ "current" ] ] as $item )
   echo $item . " row(" . $_SESSION[ "current" ] . ")<br/>";
?>
    <br/>
    <br/>
    -- Refresh to see next page --
  </body>
</html>

将以前的代码复制粘贴到 PHP 文件中,然后在浏览器中打开它。每次刷新页面时,将显示下一个项目块。

要使其适应您的代码,我们只需要使用 sql 查询填充数据数组:

$results = mysqli_query($con, "SELECT * FROM testtable WHERE `id` = $id")
           or die('AM error occured: ' . mysqli_error());
$items = array();
while($row = mysqli_fetch_array($results)){
  array_push( $items,array( $row["content0"],
                            $row["content1"],
                            $row["content2"],
                            $row["content3"],
                            $row["content4"],
                            $row["content5"] ) );
}