Adding next button to php pagination script. Its short and simple - php

This is the first pagination script I've coded with help from a friend. I customized it a bit and want to also add NEXT and PREVIOUS buttons. Here is my code.
$per_page = 6;
$pages_query = mysql_query("SELECT COUNT(`user_id`) FROM `users`");
$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 `username` FROM `users` WHERE `active` = 1 LIMIT $start, $per_page");
while ($query_row = mysql_fetch_assoc($query)) {
echo '<p>', $query_row['username'] , '</p>';
}
//previous (this is where the previous button would go)
if ($pages >=1 && $page <=$pages) {
for ($x=1; $x<=$pages; $x++) {
echo ($x == $page) ? '<strong>'.$x.'</strong> ' : ''.$x.' ';
}
}
I've simplified the script and have removed the last page first page buttons. I also exluded the php tags and mysql connection

This is pretty simple
add this before loop starts with $_GET global value
for your previous
if(isset($_GET['page']) && $_GET['page'] > 1){ href($_GET['page'] -1) }
for your next
if(isset($_GET['page']) && $_GET['page'] < $pages){ href($_GET['page'] +1) }
you can add this with subtract and add operator
however thanks for sharing the code i got really easy way to use your code with my custom php application too :)

Related

PDO pagination query not executed correctly?

I'm currently trying to make some pagination work, but without any success.
The first query I'm executing is to grab all the data needed: so first result should be on the first page, second result on the second and so on... But I'm always getting the first result. Although I copied the query into phpmyadmin to manualy set the query and there it showed me the right results.
Here's the code where everything is happening. I'm stuck why it won't work.
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = 1;
$start = 0;
$query = $db->prepare("SELECT * FROM `users` LIMIT $limit OFFSET $start");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_OBJ);
$count = count($result);
$query2 = $db->prepare("SELECT * FROM `users`");
$query2->execute();
$result2 = $query2->fetchALL(PDO::FETCH_OBJ);
$count2 = count($result2);
// Pagination
$total = ceil($count2 / $limit);
if ($page > 1) {
$start = ($page - 1) * $limit;
}
if ($page != $total) {
$next_page = '<li>»</li>';
} else {
$next_page = '<li class="disabled">»</li>';
}
if ($page > 1) {
$previous_page = '<li>«</li>';
} else {
$previous_page = '<li class="disabled">«</li>';
}
What am I doing wrong?
You are doing nothing with $page. $start is always 0 in the query.
You're going to need to define how many records you want per page, then multiply that by $page -1, e.g.
$start = ($page - 1) * $records_per_page;

Call to undefined function cell()

I am trying to build a simple pagination in PHP. But for some reason it keeps outputting the error. I just can't seem to find the issue. Here is my code:
PHP :
include_once('connect.php'); // connect to db is successful
$count_query = mysql_query("SELECT NULL FROM users");
$count = mysql_num_rows($count_query);
// pagination starts here
if (isset($_GET['page'])) {
$page = preg_replace("#[^0-9]#", "", $_GET['page']);
} else {
$page = 1;
}
$perPage = 5;
$lastPage = cell($count / $perPage);
if ($page < 1) {
$page = 1;
} else if ($page > $lastPage) {
$page = $lastPage;
}
$limit = "LIMIT " . ($page - 1) * $perPage . ", $perPage";
$query = mysql_query('SELECT first_name FROM users ORDER BY user_id DESC $limit');
if ($lastPage != 1) {
if ($page != $lastPage) {
$next = $page + 1;
$pagination .= 'Next';
}
if ($page != $lastPage) {
$prev = $page - 1;
$pagination .= 'Previous';
}
}
while ($row = mysql_fetch_array($query)) {
$output .= $row['first_name'] . '<hr />';
}
HTML :
<!DOCTYPE html>
<html>
<head>
<title>Pagination</title>
</head>
<body>
<h1>My Pagination</a>
<?php echo $output; ?>
<?php echo $pagination; ?>
</body>
</html>
As said in the title, i'm getting a "Fatal error: Call to undefined function cell()".
I've spent hours of fixing and still no luck. I hope you guys figure it out and let me know what's the problem.
It is telling you that you are calling an undefined function. Your problem looks to be over here.
$lastPage = cell($count / $perPage);
You mean ceil(), not cell().
Simple spelling mistake, it is short for ceiling.
cell is a function call that PHP can't find...
You haven't shown it in your code so does it exist?

Pagination links on top of the page, before actual script

I have a simple navigation php script which works ok.
The only problem is that I need to find a way to display the pagination links on top of the page instead of the bottom.
So for me a newb, it seems impossible since I have to show the pagination links before the actual navigation variables are being declared.
Can this be done?
Here is the script:
<?
$numrows = '600';
$rowsperpage = 20;
$totalpages = ceil($numrows / $rowsperpage);
if ($currentpage > $totalpages){ $currentpage = $totalpages; }
if ($currentpage < 1){ $currentpage = 1; }
$offset = ($currentpage - 1) * $rowsperpage;
HERE I query the data from the table and basically fill in the page.
Now starts the pagination links:
$range = 3;
if ($currentpage > 1) {
echo 'First';
}
$prevpage = $currentpage - 1;
echo 'Previous';
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if($x == $currentpage){
echo '<span class="current">'.$x.'</span>';
} else {
echo ''.$x.'';
}
}
}
if ($currentpage != $totalpages){
$nextpage = $currentpage + 1;
echo 'Next';
echo 'Last';
}
?>
When I do anything in PHP I do the following
<?php
//Calculate anything I need for the page to be displayed
//Build the html page and fill in variables where needed
?>
You may also want to look into frameworks like smarty that separate the logic from the template designs. PHP is really a templating language in itself but I actually quite like using smarty.

PHP-Modifing Pagination for Sorting

Hi i am a new programmer.
i have a simple pagination which works fine,
i have sorting options for sorting my recordset result by id, title etc which is also working fine, the codes are below.
now i want to combine both and have a functionality that pagination should work on both conditions.
that is when recordset result is displayed by default pagination should work as before.
and when recordset result is sorted by options pagination should work on sorted result too.
i got something figured out, the codes are below, but i cant get it to work.
my code for recordset and basic pagination-sorting are:
<?php
$table='mytable';
$pagename = "is-test.php";
$db = mysql_select_db($database,$connection) or trigger_error("SQL", E_USER_ERROR);
$sql1 = "SELECT COUNT(*) FROM $table";
$result1 = mysql_query($sql1, $connection) or trigger_error("SQL", E_USER_ERROR);
$row = mysql_fetch_row($result1);
$numrows = $row[0];
$rowsperpage = 5;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
$currentpage = (int) mysql_real_escape_string($_GET['page']);
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$orderBy = array('id', 'title',);
$order = '';
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = mysql_real_escape_string($_GET['orderBy']);
}else{
$order='id';
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql2 = "SELECT * FROM $table ORDER BY $order ASC LIMIT $offset, $rowsperpage";
$result2 = mysql_query($sql2, $connection) or trigger_error("SQL", E_USER_ERROR);
$list = mysql_fetch_assoc($result2);
$startrow = ($currentpage-1) * $rowsperpage
my codes for sorting options:
Sort by
id:
title:
my codes for pagination are:
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <li><a href='$pagename?page=$nextpage'>Next»»</a></li> ";
}
if($currentpage<$totalpages){
for ($x = ($currentpage - 3); $x < (($currentpage + 3) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <li id='pcurrent'><a href='$pagename?page=$x'>$x</a></li>";
} else {
echo " <li><a href='$pagename?page=$x'>$x</a></li> ";
}}}
}
if ($currentpage > 1){
$prevpage = $currentpage - 1;
echo " <li><a href='$pagename?page=$prevpage'>««Prev</a></li> ";
}
upto now everytthing is working except pagination only paginate defaut recordset resunt not sorted recordset result for that i have to change url parameter for pagination to work on sorting result,
so i have changed my pagination links as
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <li><a href='$pagename?orderBy=$order,page=$nextpage'>Next»»</a></li> ";
}
if($currentpage<$totalpages){
for ($x = ($currentpage - 3); $x < (($currentpage + 3) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <li id='pcurrent'><a href='$pagename?orderBy=$order,page=$x'>$x</a></li>";
} else {
echo " <li><a href='$pagename?orderBy=$order,page=$x'>$x</a></li> ";
}}}
}
if ($currentpage > 1){
$prevpage = $currentpage - 1;
echo " <li><a href='$pagename?page=$prevpage'>««Prev</a></li> ";
}
i.e added orderBy=$order in links for pagination, but pagination is not working now,
not on default recordset result and not on sorted recordset result.
please see what i am doing wrong
I didn't read the whole code but you have at least one error in your query string. Each GET-parameter should be separated by "&" instead of "," what you did. Changing your code from
'?orderBy=$order,page=$nextpage'
to
'?orderBy=$order&page=$nextpage'
should fix this error at least.
You should put error_reporting(E_ALL); at the beginning of your code to see notices from php (which would have helped you a lot). If it's still not working afterwards you should debug your GET params with
<?php var_dump($_GET); ?>

How to add Ajax on existing PHP & Mysql based pagination

Hi i am a new programmer,
i have a PHP & Mysql based pagination, i want to add Ajax functionality to it.
i have gone through many tutorials but i was not able to find any which tells about adding ajax onto existing pagination they all tell about making Ajax based pagination.
i want user be able to paginate even if javascript is turned off. so i want to add some Ajax to my code so that i can be able to paginate with Ajax and PHP.
i can use jquery .load() method to paginate.
please look at my code and suggest me how i can fetch page url for ajax to paginate
i guess something like this has to work. i cant figure out how, please help.
or tell me some tutorial i can learn from.
Jquery Code
$(document).ready(function(){
$('#pagination').click(function(){
$('pageurl').load('is-test2.php #PaginationDiv');});
});
PHP & MySQL Based Pagination
<?php
require_once('_ls-global/php/connection.php');
$db = mysql_select_db($database,$connection) or trigger_error("SQL", E_USER_ERROR);
$sql1 = "SELECT COUNT(*) FROM $table";
$result1 = mysql_query($sql1, $connection) or trigger_error("SQL", E_USER_ERROR);
$row = mysql_fetch_row($result1);
$numrows = $row[0];
$rowsperpage = 2;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
$currentpage = (int) $_GET['page'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql2 = "SELECT * FROM internet_security ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result2 = mysql_query($sql2, $connection) or trigger_error("SQL", E_USER_ERROR);
$list = mysql_fetch_assoc($result2);
$startrow = ($currentpage-1) * $rowsperpage;
Code in html
h3>Results <?php echo ($startrow+1) ?> - <?php echo min($startrow + $rowsperpage, $row) ?> of <?php echo ($totalpages *$rowsperpage) ?></h3>
<ul><?php
if ($currentpage!=$totalpages) {
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$totalpages'>$totalpages</a></li> ";
$nextpage = $currentpage + 1;
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Next»»</a></li> ";
}?></ul>
<ul><?php
if($currentpage<$totalpages){
for ($x = ($currentpage - 3); $x < (($currentpage + 3) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <li id='pcurrent'><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></li>";
} else {
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></li> ";
}}}
}
}
?> </ul>
<ul><?php
if ($currentpage > 1){
$prevpage = $currentpage - 1;
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>««Prev</a></li> ";
echo "<li><a href='{$_SERVER['PHP_SELF']}?page=1'>1</a></li> ";
}?></ul>
since you are getting your page variable into the PHP script with GET method, you can pass the variable like :
$(document).ready(function(){
$('#pagination ul li a').click(function(){
e.preventDefault();
$('#divtoreplace').load($(this).attr("href"));
});
});

Categories