Solved

Help with Pagination

Posted on 2011-03-11
5
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 55

Accepted Solution

by:
Huseyin KAHRAMAN 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 110

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

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

695 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