Solved

Help with Pagination

Posted on 2011-03-11
5
299 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

11 Experts available now in Live!

Get 1:1 Help Now