Second page of pagination displays all results - php

I have spent days on this and can't make any progress, any help will be really appreciated. I have a page that processes a query from a form and displays the results. The first page of results works as expected showing number of rows found and displaying some of the results but when I click to the next page I just get all the results in the database, like the query has been forgotten.
<?php include_once "db_conx.php";
if (isset($_GET['submit']) && $_GET['submit'] == 'submit_1'){
//Build Search Query from form data
$whereClauses = array();
if (! empty($_GET['location'])) $whereClauses[] ="(location ='".mysqli_real_escape_string($db_conx,$_GET['location'])."' OR location_2 ='".mysqli_real_escape_string($db_conx,$_GET['location'])."' OR location_3 ='".mysqli_real_escape_string($db_conx,$_GET['location'])."' OR location_4 ='".mysqli_real_escape_string($db_conx,$_GET['location'])."')";
if (! empty($_GET['jobtitle'])) $whereClauses[] ="(jobtitle LIKE '%".mysqli_real_escape_string($db_conx,$_GET['jobtitle'])."')";
if (!empty($_GET['start']))
if ($_GET['start'] == "today") {
$whereClauses[] = "START = CURDATE()";
}
else if ($_GET['start'] == "tomorrow") {
$whereClauses[] = "START = DATE_ADD( CURDATE( ) , INTERVAL( 9 - IF( DAYOFWEEK( CURDATE( ) ) =1, 8, DAYOFWEEK( CURDATE( ) ) ) ) DAY )";
}
else if ($_GET['start'] == "next_week") {
$whereClauses[] = "START = DATE_ADD( CURDATE( ) , INTERVAL( 9 - IF( DAYOFWEEK( CURDATE( ) ) =1, 8, DAYOFWEEK( CURDATE( ) ) ) )
DAY )";
}
else if ($_GET['start'] == "this_month") {
$whereClauses[] = "START BETWEEN DATE_SUB( CURDATE( ) , INTERVAL( DAY( CURDATE( ) ) -1 ) DAY ) AND LAST_DAY( NOW( ) )";
}
else if ($_GET['start'] == "next_month") {
$whereClauses[] = "start BETWEEN DATE_SUB(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)),INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)))-1 DAY) AND LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH))";
}
else if ($_GET['start'] == "in_future") {
$whereClauses[] = "START > CURDATE()";
}
$where = '';
if (count($whereClauses) > 0) {
$where = ' WHERE '.implode(' AND ',$whereClauses);
}
// The first query to count the number of rows
$sql = "SELECT COUNT(id) FROM users " .$where."";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
// Here we have the total row count
$rows = $row[0];
// The number of results to display per page
$page_rows = 5;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// Make sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// The Query again to get just one page worth of rows by applying $limit
$sql="SELECT * FROM users " .$where." ORDER BY notescheck DESC $limit";
$query = mysqli_query($db_conx, $sql);
// This shows the user what page they are on, and the total number of pages
$textline1 = "Results (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
/* First we check if we are on page one. If we are then we don't need a link to
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<ul class="pagination"><li>Previous</li></ul> ';// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<ul class="pagination"><li>'.$i.'</li>
</ul> ';
}
}
}
// Render the target page number, but without it being a link
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= '<ul class="pagination"><li>'.$i.'</li>
</ul> ';
if($i >= $pagenum+4){
break;
}
}
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= '<ul class="pagination"><li>Next</li>
</ul> ';
}
}
}
$list = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$firstname = $row["firstname"];
$lastname = $row["lastname"];
$list .= '<p>'.$firstname.' '.$lastname.' Profile - Click the link to view user<br></p>';
}
// Close your database connection
mysqli_close($db_conx);
?>

$_SERVER['PHP_SELF'] refers to the script, but not the full url. This means that you loose all the information you previously had in your query string. Actually, the only reason this works, is because your script name is the same as the url. PHP_SELF is actually for internal use (including files) and not intended for building urls at all.
You can solve this by storing the values in cookies, or by using the full url (including query string) when you build the links for previous and next pages.
When building the url, you can use $_SERVER['REQUEST_URI']. This gives you the relative url to your script, similar to $_SERVER['PHP_SELF'], but actually giving the document part of the request url.
Additionally, you can use $_SERVER['QUERY_STRING'] to get the entire query string of the request, or you can use $_GET[] to get each of the query string values and only copy those that you want to use in the url.
To simply copy everything that is in the url, and just change the page number, you can do this:
$pagenum = 5; // example.
$urlParams = $_GET; // Copy the entiry query string.
$urlParams['pn'] = $pagenum - 1; // Replace the page number.
$previous = http_build_query($urlParams); // Build a new query string.
$urlParams['pn'] = $pagenum + 1;
$next = http_build_query($urlParams);
And then you can build a link like this:
'Previous page
Next page'

Related

Ajax PHP Article Paginator

I'm trying to find code to paginate a long post via Ajax and Php, e.g
My post has more than 2000 words, I would like to break the page into several pages with next& previous links with each page having 500 words. So in total 4 links should be displayed.
What I have gotten so far are plugins which paginate database records.
Any leads would be highly appreciated.
If you want to check how many characters are there in an article you can use a PHP strlen() function. Then if it's greater than 500 you can set up a new link with $_GET... I don't know that you have a database but I recommend it to save the article in a mysql database.
Code:
// This first query is just to get the total count of rows
$sql = "SELECT COUNT(id) FROM articles WHERE account_name=?"; // or you can use an strlen function here
$stmt = $conn->prepare($sql);
$stmt->bind_param("s",$u);
$stmt->execute();
$stmt->bind_result($rows);
$stmt->fetch();
$stmt->close();
// Here we have the total row count
// This is the number of results we want displayed per page
$page_rows = 10;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
/* First we check if we are on page one. If we are then we don't need a link to
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= 'Previous '; // here we set up the link
// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= ''.$i.' ';
}
}
}
// Render the target page number, but without it being a link
$paginationCtrls .= ''.$pagenum.' ';
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ''.$i.' ';
if($i >= $pagenum+4){
break;
}
}
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' Next ';
}
}
Since I don't know what do you want exactly watch this video: pagination

Pagination with Microsoft sql & PHP

Trying to do some pageintaion with mssql & php but i cant seem to get it working... i get no errors, so i cant really find out what im doing wrong, am i missing anything obvious?
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$dbhost = 'localhost';
$dbuser = '*****';
$dbpass = '******';
$db = 'blog';
$connectionInfo = array( "UID"=>$dbuser,
"PWD"=>$dbpass,
"Database"=>$db, "CharacterSet" => "UTF-8");
$connect_db = sqlsrv_connect ( $dbhost, $connectionInfo ) or die(sqlsrv_errors());
if ((!isset($_GET['pagenum'])) || (!is_numeric($_GET['pagenum'])) || ($_GET['pagenum'] < 1)) { $pagenum = 1; }
else { $pagenum = $_GET['pagenum']; }
$result = sqlsrv_query ($connect_db,"SELECT blogID FROM blog_posts") or die(sqlsrv_errors());
$rows = sqlsrv_num_rows($result);
$page_rows = 2;
$last = ceil($rows/$page_rows);
if (($pagenum > $last) && ($last > 0)) { $pagenum = $last; }
$max = ($pagenum - 1) * $page_rows;
$result2 = sqlsrv_query(($connect_db,"SELECT TOP $page_rows FROM blog_posts WHERE blogID NOT IN (SELECT TOP $max blogID FROM blog_posts ORDER BY blogID ASC) ORDER BY blogID ASC") or die(sqlsrv_errors());
while($info = sqlsrv_fetch_array( $result2, SQLSRV_FETCH_BOTH ))
{
print $info['blogID'];
echo "<br>";
}
echo "<p>";
echo " --Page $pagenum of $last-- <p>";
if ($pagenum == 1) { }
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
echo " ---- ";
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
blogID is the unique id of every post in the db
Non technical answer (eg, not writing all your code for you)
You've currently got the code that says $max = (page number-1) * length of page
-if page = 1, then max = 0, so there is no data for page 1
-for page 2, you'd have 1 page length of stuff
-for page 3, you'd have 2 page lengths of stuff
clearly - this isnt what you meant. What you meant to do was select the number of rows from end of previous page, to the end of the current page. MSSQL is less good at this
firstly
you need a min and max. to show from
$min = (page number -1 * pagelength)+1
(eg not the same record at end of one page and the beginning of the next)
$max = page number * pagelength
if page length was 5, a page view might be min record 21, and max 25
you can then use SQL like
SELECT *
FROM ( <your query here>
) AS RowConstrainedResult
WHERE RowNum >= $min
AND RowNum < $max
ORDER BY RowNum
your current select statement only gets a blog id - so its going to be a rather boring page but.. thats your call - but hopefully you get the idea.
You would also need to know the number of records, as you'd need to decide if there are more pages after all.

PHP Pagination - having trouble detecting pages

I want to make a minimalistic pagination script that basically does three things:
On first page, just a next button.
On last page, just a previous button.
For all others in between, both.
I have most of the code worked out, but I'm just making some if/elseif statements that determine which page the user is on and I'm having a bit of trouble. (those at bottom) First, here's the query code:
$per_page = 10;
$pages_query = mysql_query("SELECT COUNT(idnum) FROM images");
$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 images ORDER BY idnum DESC LIMIT $start, $per_page");
And here's the if statement part:
$nextend = $pages - 1;
$next = $page + 1;
$previous = $page - 1;
if ($pages >= 1 && $page = 1) {
echo 'next';
} elseif ($pages >= 1 && $page = 2) {
echo 'previous';
}
It always results in the next button, no matter what page I'm on. How do I detect the page number so I can display the pagination buttons the way I want to? By the way, I know I don't have the else statement for the middle pages (next and previous) yet.
You are assigning in your if statements rather than comparing. You don't want this in your if statement:
$page = 1
That just assigns 1 to $page.
You want this:
$page == 1
Or this:
$page === 1
$nextend = $pages - 1;
$next = $page + 1;
$previous = $page - 1;
$maxpages = ? //You need to have a variable with the last page number
if ($pages > 1) {
echo 'previous';
}
if ($page < $maxpages) {
echo 'next';
}
I don't understand what logic you are trying to do with your if/else statements, also when checking if a variable is equal to a number/other variable your "==" not "="
What you've got to do is way simple:
If you're not on the first page, you always have to show a back button.
If you're not on the last page you always have to show a next button.
Please note that both those conditions can happen at the same time so using an elseif between them won't work as it will only allow one of them to execute.
Example:
if ( $page > 1 )
{
echo( "Previous" );
}
if ( $page < $pages )
{
echo( "Next" );
}

pagination fixing php mysql

this pagination only for next and previous wallpaper/ query , but its show also empty rows,
how to fix it?
<?php
// Figure out the limit for the query based
// on the current page number.
$from = (($walpaperid * $max_results) - $max_results);
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT( * ) AS `Rows` , `wallpaperid`
FROM `wallpaper` ORDER BY `wallpaper`.`wallpaperid` ASC"),0) or die(mysql_error());
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($from / $max_results);
// Build Page Number Hyperlinks
// Build Previous Link
if($wallpaperid > 1){
$prev = ($wallpaperid - 1);
echo "<Previous Page ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($pagenum) == $i){
echo "<strong>$i</strong> ";
} else {
echo "<a class = 'mlnk' href=\"$siteurl3/$cat_url-$catid-$i.php\">$i</a> |";
}
}
// Build Next Link
if($wallpaperid < $total_pages){
$next = ($wallpaperid + 1);
echo "Next Page>";
}
?>
Correct your code to figure out the number of pages:
$total_pages = ceil($total_results / $max_results);

pagination incrementing problem

I have the following code, which is the basis for pagination in a larger application. However, it does not work, as although I should be obtaining the value of pg from the url, pg never goes higher than 2. For some reason, next = pg+1; seems to always see pg as 1, regardless of what is passed on the url. It is a similar problem with last. I assume I am overriding the value obtained from GET, but I am unsure where.
The problem seems to be in how I am working out $max and the limit, as instead of 0, 10, -10, 10 gets passed. Also the ifcode before $max does not seem to succeed in stopping pg from being 0.
<?php
if (isset($_GET["pg"])) {
$pg = $_GET["pg"];
} else $pg = 1;
$con = mysql_connect("localhost","","");
if(!$con) {
die('Connection failed because of' .mysql_error());
}
mysql_select_db("ebay",$con);
if ($pg < 1) {
$pg = 1;
} elseif ($pg > $last) {
$pg = $last;
}
$table = 'AUCTIONS';
$page_rows = 10;
$max = ' limit ' .($pg - 1) * $page_rows .', ' .$page_rows;
$rows = getRowsByArticleSearch($query, $table, $max);
$rowcount = count($rows);
echo $rowcount;
$last = ceil($rowcount/$page_rows);
$page_rows = 10;
$rowcount = 2;
// Would normally obtain the number of rows returned, but database stuff is snipped for brevity
$last = ceil($rowcount/$page_rows);
if ($pg < 1) {
$pg = 1;
} elseif ($pg > $last) {
$pg = $last;
}
$self = htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'utf-8');
echo " <a href='$self?pg=1'> <<-First</a> ";
$previous = $pg-1;
echo " <a href='$self?pg=$previous'> <-Previous</a> ";
echo "---------------------------";
$next = $pg+1;
echo " <a href='$self?pg=$next'>Next -></a> ";
echo " <a href='$self?pg=$last'>Last ->></a> ";
The code is correct, however, the data is wrong.
When you have
$page_rows = 10;
$rowcount = 2;
it actually means, that you have 2 rows displayed on pages which will display 10 rows each. This makes it 1 page in total.
Change it to
$page_rows = 10;
$rowcount = 200;
and voila, you will get your pagination.
Here's your problem:
$last = ceil($rowcount/$page_rows);
That's setting $last to be 1, and then you have:
if ($pg > $last) {
$pg = $last;
}
Edit: I wonder if you meant this, instead:
$last = ceil($page_rows/$rowcount);
Edit again: as per Cassy's answer, you probably really just need to set the right values for these variables:
$page_rows = 10;
$rowcount = 2;
based on the now revised question:
$rowcount = count($rows);
will naturally always return 10, as you're limiting the result
... LIMIT 10;
But this might help:
SQL CALC FOUND ROWS
Example:
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
-> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
The first query will still return your articles, while the second will return the number of rows the previous select would have had if not using the LIMIT clause.
Hope that helps.

Categories