Hey I try to display next row on refresh and when it arrive at last row to take it from the beginning. Here is my table
col1 row(1)
col2 row(1)
col3 row(1)
col4 row(1)
col5 row(1)
and on refresh to get
col1 row(2)
col2 row(2)
col3 row(2)
col4 row(2)
col5 row(2)
and when it arrive at last row take it from the beginning.
col1 row(1)
col2 row(1)
col3 row(1)
col4 row(1)
col5 row(1)
Here is the code so far:
<?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"];
}
<?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"];
}
explanation:
here we are getting total records and checking with current refreshed item, if greater then that we are assigning to start(1)
To get next id from the table if it has gaps in id values use this query
select *
from testtable
where id >= if($id > (select max(id) from testtable), 1, $id)
order by id
limit 1
and save in session variable selected id
What you need is pagination, what can be done with $_SESSION. Next is an example with an array of data :
<?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>
Copy-paste previous code in a PHP file and open it in your browser. Each time you refresh the page, the next block of items will be shown.
To adapt it to your code, we only need to fill the array of data with the sql query :
$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"] ) );
}