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

时间:2016-07-07 17:03:57

标签: php sql

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

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"];

}

3 个答案:

答案 0 :(得分:0)

<?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)

答案 1 :(得分:0)

如果ID值中存在空白,要从表中获取下一个ID,请使用此查询

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

并保存在会话变量中选择id

答案 2 :(得分:0)

你需要的是分页,可以用$ _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"] ) );
}