View Full Version : Separate Pages
Christopher
June 2nd, 2002, 10:06
Okay, say you have a whole database full of jokes and you want to display them page by page about 10 or 20 at a time.
I know how to display them and everything, but how would you put like a link on the bottom to go to the next page?
If there were 20 jokes, there should be 2 pages, then if someone
adds a joke it should change to three pages... Get what I mean?
AXCess
June 2nd, 2002, 12:26
If I know how to I`ld put `m like this --> [Link (http://www.bubblesonline.com/?sid=qa&pid=archive)]
Christopher
June 2nd, 2002, 13:20
Yes, thats what I want too...
spec
June 2nd, 2002, 15:19
Snipe (She is not on this board)
wote this little script for that purpose
enjoy
http://www.snipe.net/geek/toolz/page_nums.php?show_source=yes
Christopher
June 2nd, 2002, 15:31
Thanx spec, I wanted to write it on my own, and just get the technique, but I'll just look over that code.
Ever notice on that site the hand when you go over a link has a middle finger up instead of the index? lol
Dusty
June 2nd, 2002, 18:31
Just the technique? If you want to write it yourself there's always more than one way, but what you can do is:
1. Have a "start at" variable that says what row you're on, defaulting to zero.
2. Get the count of rows from the database ("SELECT COUNT(1) FROM ... WHERE ...")
3. Divide that by how many per page you're showing to get how many pages you'll have
4. Do a for loop from 1 to the number of pages, this will give you your "1 2 3..." links
5. In the loop, have a variable start at zero and add to itself each time around how many per page you have, use this in the link as the "start at" variable.
6. For the "next" link, take your current "start at" and add the amount per page to it. Check sum against count to make sure you've not gone past the end
7. For the "last" link, take your current "start at" and subtract the amount per page to it. Check difference against count to make sure you've not gone past the start.
8. Get the rows from the database limiting to the "start at" variable and taking the number per page ("LIMIT $start_at,$per_page")
Christopher
June 3rd, 2002, 07:50
Now thats what I needed. Glad you inserted the "count" thing too, have to read up that in my book, don't remember seeing it.
Christopher
June 3rd, 2002, 21:07
Ok, I'm stuck... I over estimated my knowlege...
How would you actually display the items if your have to display X at a time starting at X?
Here is what I have so far, I think I did the COUNT thing wrong...
<?php
mysql_connect("localhost","...","...");
mysql_select_db("ugz_disdb");
$result = mysql_fetch_array(mysql_query("SELECT * FROM dis");
$num_result = mysql_query("COUNT(".$result.")");
if(empty($display)
{
$display = 10;
}
for($i=0,$i++,$i <= $display) // getting ready to display X amount of items
{
// ???????
Help! :cry2:
AlieXai
June 4th, 2002, 02:13
> How would you actually display the items if your have to display
> X at a time starting at X?
I'm a bit confused by what you mean there, but here's a working version of your code. Or atleast it should work, but i've not tested.
<?php
mysql_connect('', '', '');
mysql_select_db('database');
// i'm being safe here, since i'm not actually testing the below code...
$query = "SELECT COUNT(*) AS total FROM dis";
$go = mysql_query($query);
$result = mysql_fetch_array($go);
$display = (!empty($display) || $display > 0) ? $display : 10;
for($i = 0; $i < $result['total']; $i++)
{
// code goes here
}
?>
If you want me to go into greater detail (e.g. - by giving example code) about pagination... I can.
Dusty
June 4th, 2002, 10:24
I think I did the COUNT thing wrong... The count is done in the SELECT:
$result=mysql_fetch_array(mysql_query("SELECT COUNT(1) FROM dis");
I would advice against counting *, that will just take more time and use more resources, just count a constant like 1. It'll give the same result and run much faster.
biggulp
June 5th, 2002, 03:48
whats the diff between count(1) and count(*)?
Powered by vBulletin® Version 4.1.7 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.