VB icon

Next, Previous, and direct links to each page #

Email
Submitted on: 1/1/2015 1:59:00 AM
By: PHP Code Exchange (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 1282
 
     This is the code that I use to get xxx entries per page from a database. It also provides direct page links (i.e. if there are 3 pages of db entries, you will see Prev 1 2 3 Next). by Matt Whitted
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Next, Previous, and direct links to each page #
// Description:This is the code that I use to get xxx entries per page from a database. It also provides direct page links (i.e. if there are 3 pages of db entries, you will see Prev 1 2 3 Next). by Matt Whitted
// By: PHP Code Exchange (from psc cd)
//**************************************

<? php
// Simple chunk of code to take a db query and display it xx entries
// per page, by Matt Whitted (admin@virtuaweb.net).
// Not the cleanest code, but it works.
// Number of entries per page 
$per_page = 25; 
// You will need to change this. This should be your normal SQL query that
// you use to get the info to be displayed from the db.
$sql_text = ("SELECT * from table"); 
// Set page #, if no page isspecified, assume page 1 
if (!$page) { 
$page = 1; 
} 
$prev_page = $page - 1; 
$next_page = $page + 1; 
$query = mysql_query($sql_text);
// Set up specified page 
$page_start = ($per_page * $page) - $per_page; 
$num_rows = mysql_num_rows($query); 
if ($num_rows <= $per_page) { 
$num_pages = 1; 
} else if (($num_rows % $per_page) == 0) { 
$num_pages = ($num_rows / $per_page); 
} else { 
$num_pages = ($num_rows / $per_page) + 1; 
} 
$num_pages = (int) $num_pages; 
if (($page > $num_pages) || ($page < 0)) { 
error("You have specified an invalid page number"); 
} 
// 
// Now the pages are set right, we can 
// perform the actual displaying... 
$sql_text = $sql_text . " LIMIT $page_start, $per_page"; 
$query = mysql_query($sql_text); 
while ($result = mysql_fetch_array($query)) { 
// This stuff is obviously just an example. This is where you want
// to layout your HTML to display the queries. This loop will run
// once for every entry to be displayed on the current page.
echo $result[email];
echo "<br>";
} 
// You will probably want to modify this stuff too. This displays
// the previous, next, and direct links to each page. It is laid out
// VERY plain below, so you will likely want to change it to fit the 
// layour of your site.
// Previous 
if ($prev_page) {
echo "<a href=\"$PHP_SELF page=$prev_page\">Prev</a> 
}
// Page # direct links 
// If you don't want direct links to each page, you should be able to
// safely remove this chunk.
for ($i = 1; $i <= $num_pages; $i++) { 
if ($i != $page) { 
 echo " <a href=\"$PHP_SELF?page=$i\">$i</a> "; 
} else { 
 echo " $i "; 
} 
} 
// Next 
if ($page != $num_pages) { 
echo "<a href=\"$PHP_SELF page=$next_page\">Next</a> 
}
?> 


Other 13 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.