Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with Pagination

Posted on 2011-03-11
5
Medium Priority
?
312 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 60

Accepted Solution

by:
HainKurt earned 2000 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 111

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 October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free 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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

604 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