<? 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 (($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>
}
brett jungblut wrote :212
wouldn`t a "SELECT COUNT(column) as num_rows FROM table" be faster & less ram intensive just to get the
number of rows in the table?
yabel.com - yet another BeOS link
SWiTCH2 wrote :214
I`m a newbie, and was looking for a script like this.... THANKS!!
Is it possible so that the display of number of pages with the NEXT and PREV
is th same as LIMIT of records?
(ie) if LIMIT is 10, the total number of page number shown
should be 10 , 1 2 3 4 5 6 7 8 9 10 NEXT 20 pages ...etc
norhafiz mohamad wrote :222
This example help me alots to understand limit and offset, also very nice example
its works with me
zhenheng wu wrote :286
Thank you for sample!
zhenheng wu wrote :287
Thank you !
Roger West wrote :290
Thanks, this is just what I was looking for. It`s a
beautiful day out. Now I can go Play! Or mow the
grass...
Stephen Cameron wrote :338
Thanks, this is a great example!
With a few small mods it could be perfect :)
Stephen
Jordan Elver wrote :407
Thank You. This is the only piece of easy to use
code that I have found. I modified it a bit and it
worked. Cheers mate. My next/prev links nightmare
is over!
Marduk de la Rocha wrote :413
Thanks, but I need to formatting results of any search...
Anybody know to make this???
Contact me: trencito@gmx.net
A. Gianotto wrote :476
It`s the strangest thing! I have used this piece of code many times (THANKS!) and now all of a sudden it`s not working. :-(
http://www.snipe.net/tech/news/news.php
Any thoughts?
Trevor Garrett wrote :536
Could someone help me. I`m having a problem going to the second page of results. It says the page cannot be found. the location shows www.mydomain.com/search.php3%20page=2
Boaz Yahav wrote :537
Did you call your file "search.php3" ?
army sierra wrote :552
this code doesn`t work when i used variables in my query (ie. [$query = mysql_query("SELECT * FROM &variabletable WHERE LNAME=`$lname` and FNAME=`$fname` and POSITION=`$position` ORDER BY LNAME, FNAME");]
would appreciate ur help...
army
army@idess.com
Renato Preti wrote :700
Great stuff! Thanx by this code
Alexei Selivanov wrote :794
Using variables with this example (in reply to sierra army`s comment).
Try to send these variables like this:
echo " <a href="$PHP_SELF?page=$i&lname=`$lname&fname=`$fname&position=`$position`\">$i</a> ";
Mark Ellis wrote :1205
Can anyone help me - I am having problems using the following variables:
WHERE $category LIKE `y` AND town LIKE `%$category2%` AND checked LIKE `%1%` ORDER BY name ASC
When clicking on the page links I get an error - how do I send these variables using echo?