PHP pagination with page numbers - php

I have code that I have used on several sites to deal with pagination. The problem I'm facing is that the code only give 'next' and 'previous' links. For the site I am working on I need page numbers too, not them all, maybe 5 at a time, kin of like this
< 1 2 3 4 5 >
then when you get to page 5
< 6 7 8 9 10 >
This is my pagination code so farf
//paganation settings
$pages_query = mysqli_query($link, "SELECT COUNT(`id`) FROM `products` WHERE subcat = 1 AND
status = 1") or die(mysqli_error($link));
$result = mysqli_fetch_array($pages_query, MYSQLI_NUM);
$pages = $result[0] / $per_page;
$page = (isset($_GET['page']) AND (int)$_GET['page'] > 0) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$last = ($pages - 1) / $per_page;
$prev = $page - 1;
$next = $page + 1;
//query the db
$q =mysqli_query($link, "SELECT * FROM products WHERE subcat = 1 AND status = 1
ORDER BY id DESC LIMIT $start, $per_page");
if(mysqli_num_rows($q) > 0){
//find out the page we are on and display next and previous links accordingly
if ($page >= 1){
if ($page < $pages) {
echo "<span class='next'>";
echo "Next ";
echo "</span>";
}
if ($page >=2){
echo "<span class='prev'>";
echo "Back ";
echo "</span>";
}
}
else if ($page > $pages + 1){
echo 'No more images in the database';
}
Can anyone help me add the page numbers in here
Thanks

for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<a href='index.php?page=".$i."'>".$i."</a>";
};
This might hel you
put it between if loops for next and prev page

Related

Limiting the number of pagination pages

I have one page where I echo 10 items from MySql DB and has pagination for next page. The problem is that there are 1000+ rows in database.. so you can guess how many pages are in pagination links. How can I limit their numbers for example to show
1 2 3 4 5 ... N
when user change to page 2
2 3 4 5 6 ... N
and so on?
Or something else.. the main goal is just to hide every single page number.
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 10;
$result = $pdo->prepare("SELECT t.* FROM images t order by randorder ASC LIMIT $start_from, 10");
$result->execute();
for($i=0; $row = $result->fetch(); $i++)
{
echo 'echo data';
}
echo '<div class="pagination" >';
$result = $pdo->prepare("SELECT COUNT(image_id) FROM images");
$result->execute();
$row = $result->fetch();
$total_records = $row[0];
$total_pages = ceil($total_records / 10);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='?page=".$i."'";
if($page==$i)
{
echo "class=active";
}
echo ">";
echo "".$i."</a> ";
};
echo '</div>';
?>

Pagination script last page

I created php script following the tutorial, but it has a mistake. It displays in the last page information which is in the previous page - it's because $perpage. How can I display only data which wasn't display yet.
EXAMPLE - If I set $perpage to 3 and I have 7 records (named 1,2,3,4,5,6,7) on page one is 1,2,3 on page two is 4,5,6 and on the last page is 5,6,7 (I want to display only record 7)
$query = mysql_query("SELECT ID FROM clanek");
$count = mysql_num_rows($query);
$perpage = 3; // řádků na stránku
$pages_count = ceil($count / $perpage); //celkem stránek zaokrohleno
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$is_first = $page == 1; //první stránka
$is_last = $page == $pages_count;
$prev = max(1, $page - 1);
$next = min($pages_count , $page + 1); /
$data = mysql_query("SELECT DATE_FORMAT(datum_pridani_c,'%d.%m.%Y %H:%i:%s')as datumcas,nazev,kratky_popis,ID FROM clanek ORDER BY datum_pridani_c DESC LIMIT ".($page - 1).", ".$perpage); /
while ($zaznam = mysql_fetch_array($data)) {
//some info her
}
if($pages_count>0) {
if(!$is_first) {
echo '<a class="predchozistranka" href="index.php?page='.$prev.'">Předchozí</a>';
}
echo '<span class="stranka">Stránka '.$page.' / '.$pages_count.'</span>';
if(!$is_last) {
echo '<a class="dalsistranka" href="index.php?page='.$next.'">Další</a>';
}
}
$data = mysql_query("SELECT DATE_FORMAT(datum_pridani_c,'%d.%m.%Y %H:%i:%s')as datumcas,nazev,kratky_popis,ID FROM clanek ORDER BY datum_pridani_c DESC LIMIT ".($page - 1).", ".$perpage);
It has been a while since I have used MySQL but I fired up my Workbench just now and I see that the syntax for LIMIT is LIMIT offset,rowcount. The doc says so too http://dev.mysql.com/doc/refman/5.0/en/select.html
For your query to work then, instead of ($page - 1), $perpage, it should be ($page - 1)*$perpage, $perpage
$query = mysql_query("SELECT ID FROM clanek");
$count = mysql_num_rows($query);
Irrelevant but the above code is highly inefficient for getting the total number of rows. It would be better if you use SELECT count(id) FROM clanek

pagination going to other link when clicking but counts the number of rows

I have a problem with my pagination it's working but when I click for example
I have search ermel ermel has 8 fields and it's gonna output pagination
because limit is set to 5 per page
[1][2]
but when I click on the second page it will go to the other page and remove my query.
and will count my whole query again
[1][2][3][4][5][6]
like that and will reset to may default query
I'm not sure where the error is - I tried to remove some part but that's the only part that's not working properly thanks in advance .
Here's my working php code.
<?php
$searchtext = '';
if(isset($_GET['q'])) $searchtext = mysql_real_escape_string($_GET['q']);
if($searchtext) {
$per_page =5;
$pages_query = mysql_query("SELECT COUNT('PersonID') FROM persons where firstname like '%$searchtext' or lastname like '%$searchtext' order by date desc ");
$pages = ceil(mysql_result($pages_query,0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query=mysql_query("select * from persons where firstname like '%$searchtext' or lastname like '%$searchtext' order by date desc LIMIT $start,$per_page ");
}
else
{
$per_page =5;
$pages_query = mysql_query("SELECT COUNT('PersonID') FROM persons ");
$pages = ceil(mysql_result($pages_query,0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query=mysql_query("select * from persons order by date desc LIMIT $start,$per_page ");
}
while($test = mysql_fetch_array($query))
{
$id = $test['PersonID'];
echo"<div class = content />";
echo "<table>
<tr>
<td rowspan='2'><img height=200 width=200 src='upload/". $test['Image'] ."'/></td>
<td><a href ='profile.php?PersonID=$id'>".$test['LastName'].", ". $test['FirstName']." ". $test['MiddleName']."</a>
</tr>
<tr>
<td><a href ='user_edit_reports.php?PersonID=$id'><input type='submit' name='submit' value='Edit'/></a>
</tr>
</table>";
echo"</div>";
}
if ($pages >=1 && $page <= $pages) {
for ($x=1; $x<=$pages; $x++) {
echo ($x == $page) ? '<strong>'.$x.' </strong>' : ''.$x.' ';
}
}
?>
My guess is that the links in the pagination do not include the search string part (?q=...), and so when you click them, you get the entire, unfiltered list again.

Php pagination and MYSQL Distinct

OK i have a basic php pagination script, which has a basic next and previous button. Now this works fine until i add a distinct clause. Please see code below.
$query = "SELECT COUNT(*) as num FROM $tableName WHERE engine='$type' AND manufacturer='$man' AND '$year' BETWEEN start_year AND end_year";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
echo "$total_pages";
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT Distinct model_group from $tableName WHERE engine='$type' AND manufacturer='$man' AND '$year' BETWEEN start_year AND end_year LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
Now if i change the query in query 1 to be
$query = "SELECT * From
the code works fine, below is the code for my next and previous buttons.
Previous Button
if ($page > 1){
echo "<a href='$targetpage?page=$prev&type=$type&manufacturer=$manufacturer&year=$year'><div class='previous'><img src='images/PrevButton1.fw.png' width='108' height='58' style='border: none;'/></span></a>";
}else{
echo "<span class='disabled'><div class='previous'>Previous</span></span>"; }
Next Button
if ($page < $lastpage){
echo "<a href='$targetpage?page=$next&type=$type&manufacturer=$manufacturer&year=$year'><div class='next'><img src='images/MoreButton1.fw.png' width='108' height='58' style='border: none;'/></span></a>";
}else{
echo "<span class='disabled'><div class='next'>More</span></span>";
}
IS there anyway i can include the distinct value in to the count query? as i think it returns a different value to the second query.
i think you have to use a
SELECT COUNT(DISTINCT *)
in your first statement
a better way in mysql is to fix your paging problem with the SQL_CALC_FOUND_ROWS option in your select:
SELECT SQL_CALC_FOUND_ROWS * FROM mysql.user LIMIT 1
your result will be 1 row... but with
SELECT FOUND_ROWS();
you become the count of your result without the limit!

SQL Server 2008 Pagination PHP

I am trying to display 10 records per page by using ROW_NUMBER function of SQL SERVER 2008.
I think there is something wrong with my query because I only define where to start and where to end displaying records (from 1 to 10), but not the amount of records displayed per page.
As I go to the next page I get no results displayed at all because I don't know how to add $per_page variable to my SQL query properly.
All I want is be able to display the first 10 product IDs on page 1, and once I click on the "next" button, the next 10 product IDs will be displayed, etc.
This is the code that I have right now:
$per_page = 10;
if(!isset($_GET['page']))
{
$page = 1;
}
else
{
$page = $_GET['page'];
}
if($page<=1)
{
$start = 0;
}
else
{
$start = $page * $per_page - $per_page;
}
$tsql = " SELECT *
FROM (SELECT ROW_NUMBER() OVER(ORDER BY productID) AS
rownum, productID FROM products) AS products1
WHERE rownum >= $start AND rownum <= $per_page";
$num_rows = sqlsrv_num_rows(sqlsrv_query($conn,$tsql));
$num_pages = ceil($num_rows / $per_page);
$stmt = sqlsrv_query($conn,$tsql);
while($row = sqlsrv_fetch_array($stmt)){
echo $row['productID']. "<br/>";
}
$prev = $page - 1;
$next = $page + 1;
echo "<hr>";
//prev
if($prev > 0)
{
echo "<a href='?page=$prev'>prev</a> ";
}
//numbers
$number = 1;
for($number; $number <= $num_pages; $number +=1)
{
if($page==$number)
{
echo " <b>[$number]</b> ";
}
else
{
echo "<a href='?page=$number'>$number</a> ";
}
}
//next
echo "<a href='?page=$next'>next</a>";
I'm not familiar with sql server, but it seems to me you just need the end point:
$tsql = " SELECT *
FROM (SELECT ROW_NUMBER() OVER(ORDER BY productID) AS
rownum, productID FROM products) AS products1
WHERE rownum >= $start AND rownum < ($start + $per_page)";
^ changed ^
And if $page is supposed to be an integer, it's always best to make sure that it is:
$page = (int) $_GET['page'];

Categories