page numbers and PHP

W

wyrd_fish

Guest
how the hell do you manage to do page numbers..???

say i had a MySQL table with 346 rows and i had 20 rows per page how would I go about implimenting page numbers for it???
 
S

(Shovel)

Guest
The simplest way would be just to have a counter built into your output loop. Assuming it's all coming out at once.

If you're having 20 results per page, keep an increment of each result being output during the output part of your code, or whatever it is that you choose to use. E.g.:
Code:
"while($row = mysql_fetch_assoc($result)) {
  /* Output the row contents here */
  $counter++;
}
Then have a statement at the end of each output that reads:
Code:
if ($counter % 20 == 0) {
  echo "Page: ".$counter/20.".";
}

You may of course want to do lots of other things when a page ends - e.g. a page footer - all of which can be done within that same if branch.

Best to have the 20 as a variable of course :)

So, final code might look like:
Code:
"while($row = mysql_fetch_assoc($result)) {
  /* Output the row contents here */

  /* Increment row count */
  $counter++;

  /* Check for end of page */
  if ($counter % $results_per_page == 0) {
    echo "Page: ".$counter/$results_per_page.".";
  }
}
 
W

wyrd_fish

Guest
you seem to have misunderstood me, if you look at the botom of the page*, you will see "Pages (4): [1] 2 3 » ... Last » ", or summit like

i want to impliment a system like this, so i can jump to pages.

*hardware software security...
 
L

lovedaddy

Guest
If you can wait until I get 20 mins at home, I'll have a look at the code I wrote for a database frontend I did a while back. Probably be quicker if I look at how I did it rather than trying to work it out again ;-)

Basically generates:

Goto page [ Previous 20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, Next 20 ]

with adjustable per page type crap and bold not clickable for the page your currently on.
I warn you, its probably a bodgy mess when I go back and look at it again =)
 
F

FatBusinessman

Guest
If you take a look here you should find that MySQL SELECT statements allow you to include a LIMIT argument to specify the number of rows and the offset. So, to return the second page, you'd use:
Code:
SELECT field1, field2 FROM table WHERE condition LIMIT 20,20
This should be fairly easy to generate automatically by querying for the number of matching rows.
 
S

(Shovel)

Guest
Ahhhh, OK I'm with you now :)

Erm, I think all you'll need to do is to query for all the results and get the total number of results, then dived up to generate the number of links:
Code:
SELECT count(*) AS total from TABLE where yada=yada;

The the value of total will be the total number of records. You could then do an integer division by 20, to get the number of total pages.
Then run a for loop with the result of that, generating a link to each page, with one more page at the end if there are remaining results (e.g if it doesn't fit exactly into 20s)
 
W

wyrd_fish

Guest
yes, but can PHP round up, thats theonly problem i had with that method.

EDIT: found one

Code:
$rounded_up = ceil($not_rounded);
 
L

lovedaddy

Guest
this is how I achieved it: (if there is any tidy up, feel free to correct, this was written as a disgusting hack on top of Phpnuke to track my games collection, hence I didnt really tidy it up =)


// do the footer with page numbers
$pageurl = "Goto page [ ";
if ( ($xstart - $xperpage) >= 0)
{
$xback = $xstart - $xperpage;
$pageurl .= " <a href=$sharedurl[0]$sharedurl[1]&xstart=$xback&xperpage=$xperpage$sharedurl[2]>Previous $xperpage</a>, ";
}
for ($m=1; $m <= $numpages; $m++)
{
$xrange[0] = ($m * $xperpage) - $xperpage;
$xrange[1] = ($m * $xperpage);
if ($xstart >= $xrange[0] && ($xstart + $xperpage) <= $xrange[1])
{
$pageurl .= "$m";
}
else
{
$pageurl .= "<a href=$sharedurl[0]$sharedurl[1]&xstart=$xrange[0]&xperpage=$xperpage$sharedurl[2]>$m</a>";
}
if ($numpages > $m)
{
$pageurl .= ", ";
}
}

if ( ($numresult > ($xstart + $xperpage)) )
{
$xforward = $xstart + $xperpage;
$pageurl .= ", ";
$pageurl .= " <a href=$sharedurl[0]$sharedurl[1]&xstart=$xforward&xperpage=$xperpage$sharedurl[2]>Next $xperpage</a>";
}
$pageurl .= " ]\n";
echo "$pageurl";


Hope that helps (send me a pm if you want to see it in action)
 

Users who are viewing this thread

Similar threads

M
Replies
2
Views
554
Jonty
J
F
Replies
6
Views
594
(Shovel)
S
B
Replies
2
Views
1K
B
T
Replies
6
Views
757
(Shovel)
S
S
Replies
5
Views
726
L_Plates
L
Top Bottom