Pagination php display
In simple pagination when clicking on another, no. 1,2,3,4,
the records from the table do not change as you would expect.
I am showing records 5
on every page as on click 1
it displays record from table 1
to 5
etc. But that doesn't change. 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
}
?>
source to share
1) Don't use mysql _ functions , use mysqli _ or PDO .
2) To navigate through the pages, you need to increase the offset in the 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();
source to share
Just replace your first SQL query with my sql query:
your sql query $ sql = mysql_query ("select * from countries limit 1.5");
You have specified that the static start limit is "1"
Correct Sql query: $ sql = mysql_query ("select * from the country limit $ page1,5");
Make your static limit variable $ page1.
source to share
Try this approach. It's easier to understand.
// code for connecting to the database
$ Left_rec = $ rec_count - ($ page * $ rec_limit);
$ res = mysqli_query ("select * from countries");
$ rec_count = mysqli_num_rows ($ res); // Total number of records
if (Net ($ _ GET ['page']))
{
$ Page = $ _ GET ['page'] + 1;
$ Offset = $ rec_limit * $ pages;
}
more
{
$ Page = 0;
$ Offset = 0;
}
$ rec_limit = 5; // no records on the page
$ Offset = $ page * $ rec_limit;
$ Left_rec = $ rec_count - ($ page * $ rec_limit);
$ res = mysqli_query ("select * from countries limit $ offset, $ rec_limit");
while ($ row = mysqli_fetch_array ($ res))
{
echo $ row ["ccode"];
echo $ row ["country"];
echo "
";
}
if ($ page == 0)
{
echo "Next";
}
else if ($ left_rec <= $ rec_limit)
{
$ Recent = $ page-2;
echo "Prev";
}
? >
source to share