Jump to content

[SOLVED] Pagination with Function to Choose Number of Results Per Page


Fluoresce

Recommended Posts

Hi, guys. I was wondering if anyone knew where I could find a pagination script which allows a user to choose the number of results to display per page. And since most scripts just have 'First', 'Prev', 'Next' and 'Last' buttons, I would also like the script to have a drop-down menu which will allow a user to jump to any page.

 

Is anyone aware of such a script?

Dude, chill.

 

I have obviously already searched Google for such a script. I couldn't find one, which is why I thought I would ask the pros in a PHP forum.

 

Here's what I've already got:

 

<?php

$conn = mysql_connect('localhost', 'hidden', 'hidden') or trigger_error("SQL", E_USER_ERROR);  
$db = mysql_select_db('hidden', $conn) or trigger_error("SQL", E_USER_ERROR);

$query = "SELECT COUNT(*) FROM . . .";
$result = mysql_query($query, $conn) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
} // end else

$rows_per_page = 5;
$lastpage      = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
} // end if
if ($pageno < 1) {
$pageno = 1;
} // end if

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

$query = "SELECT . . . $limit";

$result = mysql_query($query, $conn) or trigger_error("SQL", E_USER_ERROR);

if(mysql_num_rows($result) >= 1) {

if ($pageno == 1) {
echo " ";
} else {
echo " <a class=\"pagination\" href='{$_SERVER['PHP_SELF']}?pageno=1'>First</a> ";
$prevpage = $pageno-1;
echo " <a class=\"pagination\" href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>Prev</a> ";
} // end else
echo " $pageno of $lastpage ";
if ($pageno == $lastpage) {
echo " ";
} else {
$nextpage = $pageno+1;
echo " <a class=\"pagination\" href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>Next</a> ";
echo " <a class=\"pagination\" href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>Last</a> ";
} // end else

while($row = mysql_fetch_assoc($result)) {

echo "Results . . ."

} // end while

} // end first if

else {
echo "<p>None</p>";
}

?>

 

This code produces 'First', 'Prev', 'Next' and 'Last' buttons. As such, if there are 50 pages and a user wants Page 36, s/he will have to click all the way there. I therefore want to include a drop-down menu which users can use to choose the page that they want.

 

This code also produces five results per page. I can change that, of course, but I want the users to be able to change it manually.

 

Can these alterations be incorporated into the current script? Or, should I continue looking for a new script?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.