Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination displaying issue php

In simple pagination, when clicking different no. 1,2,3,4, records from table not changing as they should. Pagenation i created 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
}
?>
like image 976
125fura Avatar asked Dec 29 '25 05:12

125fura


2 Answers

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();
like image 82
Justinas Avatar answered Dec 31 '25 17:12

Justinas


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

like image 32
Digifaktur Avatar answered Dec 31 '25 17:12

Digifaktur