Solved

Help with Pagination

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

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

770 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