Re: I need pagination
From: leegold2 (leegold2_at_verizon.net)
Date: 06/20/04
- Next message: 'bonehead: "phpMyAdmin Setup"
- Previous message: leegold2: "Re: I need pagination"
- In reply to: Shawn: "Re: I need pagination"
- Next in thread: Tim Van Wassenhove: "Re: I need pagination"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 20 Jun 2004 02:00:07 GMT
Thanks but I looking for working code. It's really hard to find
a pager that works it's impossible in Perl and seems like the same deal
in PHP too - why i don't know.
Shawn wrote:
> The best way to make a simple query-result pagination is using the
> LIMIT parameter in the SQL query.
>
> I'll write a simple script that will extract some records from the
> table "table1".
>
> <?php
> include('config.inc.php'); //configuration file containing database
> connection params
> $db = mysql_connect($db_host, $db_user, $db_password) or die
> ("Error");
> mysql_select_db($db_name, $db) or die ("Error");
>
> $count_query = mysql_query("SELECT COUNT(id) FROM table1"); //Returns
> the total number of the table records
>
> $count_result = mysql_fetch_row($count_query);
>
> $tot_records = $count_result[0]; //Total number of the records
>
> $records_per_page = 10;
>
> $tot_pages = ceil($tot_records / $records_per_page); //Calculates the
> total number of the pages. ceil() function rounds the result of the
> division to the nearest integer
>
> $current_page = (!$_GET['page']) ? 1 : (int)$_GET['page']; //If the
> page is the first (so without $_GET parameters sent) the current page
> is 1
>
> $first_record = ($current_page - 1) * $per_page; //The first record to
> be shown
>
> $records_query = mysql_query("SELECT id, name FROM table1 LIMIT
> $first_record, $records_per_page");
>
> while ($results = mysql_fetch_array($records_query)) {
> echo "<p>" . $results ['nome'] . "</p>";
> }
>
> //Now you have to make the navigation bar
> echo "page -";
> for ($i=1;$i<=$tot_pages;$i++) { // This will print "page - 1 - 2 - 3
> - n -"
> if ($i == $current_page) { //If the number is the current page
> no link is made...
> echo $i . " - ";
> }
> else { //...else the number links to the n^th page
> echo "<a href='this_page.php?page=" . $i . "'>" . $i .
> "</a> - ";
> }
> }
>
> ?>
>
>
> That's all I think, but I didn't check for any errors in the script,
> as i made it at the moment. I just wanted to show you the simplest way
> (IMHO) to handle records pagination so forgive any bugs :-)
- Next message: 'bonehead: "phpMyAdmin Setup"
- Previous message: leegold2: "Re: I need pagination"
- In reply to: Shawn: "Re: I need pagination"
- Next in thread: Tim Van Wassenhove: "Re: I need pagination"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|