pagination incrementing problem - php

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.

Related

how to fix pagination continuous number of page IN PHP

Here is my index.php code
My problem is my pagination has a continuous number of
pages.
<?php
$limit = 10;
$offset = (isset($_GET["page"]) ? $_GET["page"] - 1 : 0) * $limit;
$query = "SELECT * FROM employee ORDER BY employee_datecommenced ASC LIMIT
$offset,$limit ";
$list = getdata_inner_join($query);
?>
<?php
$total = $dbcon->query("SELECT count(*) FROM employee") or
die(mysqli_error());
$fetch = $total->fetch_assoc();
for($x = 0; $x < $fetch["count(*)"] / $limit ; $x ++){
$page = $x + 1;
if((isset($_GET["page"]) ? $_GET["page"] : 1) == $page)
$page = "<b>".$page."</b>";
echo ''.$page.' ';
}
?>
Click here to see the output photo
Thanks in advance
You say "My problem is my pagination has a continuous number of pages", but why is that a problem? What do you need to change?
in your for loop while printing the anchor tags, check the current page, if suppose the page is ($current), disable ($current-1), and print $current to ($current+5) and '....' and (current+1), if current is $min or $max manage the prev and next

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.

how to Limit pagination pages links?

I have my pagination page links like this.And the pagegrows like:
1 2 3 4 5 6 7 ,8,9,10 and goes on.
But i want to limit this so if there is more than 5 pages it shall only show 5 links like this:
1 2 3 4 5... 97 98 99
where 99 is the last page.
And if you go to next page it will only change the first pages like this:
3 4 5 ... 97 98 99
function pagination($current_page_number, $total_records_found, $query_string = null)
{
$page = 1;
echo "Page: ";
for ($total_pages = ($total_records_found/NUMBER_PER_PAGE); $total_pages > 0; $total_pages--)
{
if ($page != $current_page_number)
echo "<a href=\"?page=$page" . (($query_string) ? "&$query_string" : "") . "\">";
echo "$page ";
require_once('inc/database.php');
define("NUMBER_PER_PAGE", 5); //number of records per page of the search results
$page = ($_GET['page']) ? $_GET['page'] : 1;
$start = ($page-1) * NUMBER_PER_PAGE;
$sql = "SELECT * FROM members WHERE 1=1";
$total_records = mysql_num_rows(mysql_query($sql));
//we limit our query to the number of results we want per page
$sql .= " LIMIT $start, " . NUMBER_PER_PAGE;
// we display our pagination at the top of our search results
pagination($page, $total_records, "id=$id&username=$username&email=$email");
$loop = mysql_query($sql)
or die ('cannot run the query because: ' . mysql_error());
while ($record = mysql_fetch_assoc($loop))
echo "<br/>{$record['id']}) " . stripslashes($record['username']) . " - {$record['email']}";
echo "<center>" . number_format($total_records) . " search results found</center>";
if ($page != $current_page_number)
echo "</a>";
$page++;
Pagination is one of those things that has been done so many times that you're better off figuring out how to use a pre made class like this one http://code.tutsplus.com/tutorials/how-to-paginate-data-with-php--net-2928
The basics of what you need to do are calculate how many links you want to see on either side of the elipses ... and then make loops to create those links. The ceil() function may be new for you, but it returns a fraction always rounded up.
$buffer = 3;
$results_per_page = 5;
$page = ($_GET['page']) ? $_GET['page'] : 1;
$start = ($page-1) * $results_per_page;
$sql = "SELECT * FROM members WHERE 1=1";
$total_records = mysql_num_rows(mysql_query($sql));
$total_pages = ceil(intval($total_records) / $results_per_page);
for ($x = $page - $buffer; $x < $page; $x++){
echo " $x";
}
echo " ... ";
for ($x = $total_pages - $buffer; $x <= $total_pages; $x++){
echo " $x";
}
This quick little bit of code does not take in to consideration result sets small enough to fit all links in to one line. Which is why I suggest using the pagination classes that exist already.

Code not working, Sorting MySQL results into pages

Ok so iv got this code which is suppose to bring back all my results from my database and display 5 results per page. At the moment I have 12 results in my database and it is only showing 10, it wont show the 3rd page as there isnt 5 results to display. Here is my code
//$_GET['page'] is to get the current page opened
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$results_per_page = "5"; //number of results I want displayed per page
$start_from = ($page-1) * $results_per_page;
$query = "SELECT * FROM items WHERE subcat = '$conditions' LIMIT $start_from, $results_per_page";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
while ($row_condition=mysql_fetch_array($result)) {
//Display the results here
}
//Count number of results from database and work out how
//many pages I need to display all the results.
$result1 = mysql_query("SELECT * FROM items WHERE subcat = '$conditions'");
$num_rows = mysql_num_rows($result1);
$num_pages = $num_rows / $results_per_page;
if ($num_rows > $results_per_page){
?>
<div id="pagenum">
<?php
//This creates and displays the page numbers for the user to select
foreach( range( 1, $num_pages) as $i) {
if ($thepage == $i){
echo '<b>' . $i . '</b>';
}else{
echo '' . $i . '';
}
//This places a line between each number of pages
if ($i == $num_pages){
}else{
echo " | ";
}
}
?>
</div>
<?php
}else{ echo "Test";}
?>
So can anyone see a problem with this?, so I have 12 entries in my database. However im only getting 2 pages, 5 on each page and im not getting the 3rd page as there are only 2 results left and it seems to want 5 to finish it.
Thanks
You need to use ceil function:
$num_pages = ceil($num_rows / $results_per_page);
I think your biggest problem is that PHP will make give you a float when you divide two numbers that don't result in a whole one.
$one = 12;
$two = 5;
$result = $one / $two; // That will result in 2.4
Now, the issue is you, technically, have three pages, but PHP will NOT treat the .4 pages as a page, so when you loop through it will technically only do it twice. Since your using range and it's 2.4, it seems like PHP is rounding down. I would suggest something like this:
$result1 = mysql_query("SELECT * FROM items WHERE subcat = '$conditions'");
$num_rows = mysql_num_rows($result1);
$num_pages = ceil($num_rows / $results_per_page);
if ($num_rows > $results_per_page){
for($i = 0; $i < $num_pages; $i++) {
ceil() will force that division to round up every time, which will always give you that extra page.
Give it a shot! Let me know if it fixes your issue!

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

Categories