Solved

Help with Pagination

Posted on 2011-03-11
5
302 Views
Last Modified: 2013-12-13
I have the following code which works great and show pagnation of just Previous and Next

The client wants to include the number of pages so it would be Previous 1 2 3 4 5 6 7 Next if there are 7 pages

What is the best way to incorporate that.

My code creates a temporary database that lists all of the files.
$offset = (int) $_GET['offset'];
$items_per_page = 5;
// get max count for pagination
$result = mysql_query("SELECT count(*) FROM `{$table_prefix}{$table_id}`");
$max_count = mysql_result($result,0);
// query random order table
$result = mysql_query(
  "SELECT * FROM `{$table_prefix}{$table_id}`
   ORDER BY sort_id
   LIMIT $offset,$items_per_page");
if (!$result) die("<p>Error performing query: " . mysql_error() . "</p>");
// pagination

 echo '<div class="pagination">';
 echo "<a href='articles.php?id=$article_id'>Back to Article Categories</a>";
echo "<table border=0 width=950px><td align=left><font size=\"2\" color=\"#999999\">";
echo "Record " .($offset+1). " to " .($offset+$items_per_page). " of $max_count";
echo "</font></td><td align=right>";
echo ($offset>0) ?
  '<a href="?table_id='.$table_id.'&offset='.($offset-$items_per_page).'"><font size=\"2\">Previous</font></a>' :
  '<font size=2>Previous';
echo ' | ';
echo ($offset<$max_count-$items_per_page) ?
  '<a href="?table_id='.$table_id.'&offset='.($offset+$items_per_page).'"><font size=2>Next</font></a>' : '<font size=2>Next';
echo "</td></table>";
echo '</div>';

Open in new window

0
Comment
Question by:katlees
5 Comments
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 35111508
between previous and next put a loop

for i=1 to num_of_rows / $items_per_page {
  echo ' | <a href="?table_id='.$table_id.'&offset='.((i-1)*$items_per_page+1).'"><font size=2>'.i.'</font></a>'
}

mostly pseudo code...
0
 
LVL 2

Expert Comment

by:saloj
ID: 35111533
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35112219
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 35117015
The canonical article on PHP pagination is available from SitePoint.
http://articles.sitepoint.com/article/perfect-php-pagination
0
 
LVL 9

Expert Comment

by:alex_code
ID: 35211198
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can i modify my File download link ? 6 47
php call to a non-object 3 31
updating table data with inner join 9 24
How to convert my query to the proper format? 5 16
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now