In simple pagination, when clicking different no. 1,2,3,4, records from table not changing
as they should.
I'm displaying 5 records on each page,as on click 1 it display record from table 1 to 5,and so on.But,it is not changing.Help me.
<?php
mysql_connect("localhost","root","");
mysql_select_db("bluewater");
$page = $_GET["page"];
if($page=="" || $page=="1")
{
$page1=0;
}
else
{
$page1=($page*5)-5;
}
$sql=mysql_query("select * from countries limit 1,5");
while($ser = mysql_fetch_array($sql)) {
echo $ser["ccode"];
echo $ser["country"];
echo "<br>";
}
$sql=mysql_query("select * from countries");
$cou=mysql_num_rows($sql);
$a=$cou/5;
$a= ceil ($a);
echo "<br>"; echo "<br>";
for ($b=1;$b<=$a;$b++)
{
?><a href="index.php?page=<?php echo $b; ?>" style="text-decoration:none"><?php echo $b." ";?></a> <?php
}
?>
1) Don't use mysql_ functions, use mysqli_ or PDO.
2) To move via pages, you need to increase offset in query.
$page = $_GET['page'];
$itemsPerPage = 5;
$offset = ($page * $itemsPerPage) - $itemsPerPage;
$query = $db->prepare("SELECT * FROM `countries` LIMIT {$itemsPerPage} OFFSET {$offset}");
$query->execute();
$results = $query->fetchAll();
You never make use of your $page variable in the query. Change it to
select * from countries limit ".$page.", 5
and it should work
Also, mysql_real_escape_string($page)!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With