Have an account? Sign in
Login  Register  Facebook
Help with PHP pagination
I am using the "b_black pagination.css" and am having an issue. I need 20 results per page and am getting this but at the bottom it is saying "Page 20 of 5" and then clicking on page two says "Page 40 of 5." How to I set this up to get 20 results per page but keep it the standard "Page 1 of 5," "Page 2 of 5" etc?

--------

The problem is that I added the pagination after the site was completed for me by a developer. So, on my index page I have:
<?php
$maxresults = 20;
if($_GET['page'])
$page = $_GET['page'];
else
$page = 0;
?>

<?php
$currentpage = $page;
$page = $page*$maxresults;
$numpages = QuickQuery("SELECT COUNT(id) FROM movies WHERE visible=1");
$numpages = mysql_result($numpages, 0);
$numpages = $numpages/$maxresults-1;
$result = GetMoviesByRangeID($page, $maxresults);//Show <maxresults> pages at a time
DisplayResults($result);
?>
and then to add the pagination it required that I add this code:
<?php
//connect to the database
include_once ('db.php');
//get the function
include_once ('function.php');

$page = (int) (!isset($_GET["page"]) ? 0 : $_GET["page"]);
$limit = 20;
$startpoint = ($page * $limit) - $limit;

//to make pagination
$statement = "`movies` where `active` = 1";
?>

<?php
//show records
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}");

while ($row = mysql_fetch_assoc($query)) {
?>

<?php
}
?>


This is where the conflict is. How can I somehow merge the two?
Started: October 29, 2011 Latest Activity: October 29, 2011 pagination php troubleshooting

can you post your code please
October 29, 2011

3 Answers
Thanks man! I think I'm almost there. Just having one last issue-

Here is my site: http://www.bestcomedymovielist.com/

the pagination is at the bottom. the way the developer did the site, the index page is not counted as page 1, the second page actually lists as

http://www.bestcomedymovielist.com/index.php?page=1

page 3 as

http://www.bestcomedymovielist.com/index.php?page=2

and so on and so forth. So, how can I get the index.php page to be page zero and then clicking on the page 1 pagination link will bring you to

http://www.bestcomedymovielist.com/index.php?page=1

Also, I added the home link in the pagination, but check the url when you click on home...it is repeated

Thanks for helping

Posted:
In: October 29, 2011

Edited: MacOS
In: October 29, 2011

Thanks for using our Questions sections, this is what you want:
<?php
// get the function
include_once ('function.php');

$maxresults = 20;
if($_GET['page'])
$page = $_GET['page'];
else
$page = 1;

$currentpage = $page;
$page = $page*$maxresults;
$numpages = QuickQuery("SELECT COUNT(id) FROM movies WHERE visible=1");
$numpages = mysql_result($numpages, 0);
$numpages = $numpages/$maxresults-1;
$result = GetMoviesByRangeID($page, $maxresults);//Show <maxresults> pages at a time
DisplayResults($result);

echo pagination("movies WHERE visible=1",$maxresults,$currentpage);


?>

Posted: MacOS
In: October 29, 2011

MacOS, Thank you, but I replaced all of my code with yours and the pagination does not even show up now. How do I implement your code? Do I keep any of my original code? I.E. The index code AND the code from movie.php?
October 29, 2011

no, in you original code add echo pagination("movies WHERE visible=1",$maxresults,$currentpage); at place you want to show it
October 29, 2011

to get your current page use the $_GET['page_id']
to get the complete number of pages
$num_of_rec = mysql_query("SELECT COUNT(*) `count` FROM `contents`"); 
$num_of_rec = mysql_fetch_array($num_of_rec); 
$pages_num = $rec['count']/20; 

Posted: Nasser
In: October 29, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer