Jump to content

Help with a "Page: 1 [2] 3 4 5 6 ... 120" script


DBookatay

Recommended Posts

I have a script that displays how many pages (of search results) are available, like the one on this forum, however I do not know how to "trim" it, so it will look like my topic subject:

Page: 1 [2] 3 4 5 6 ... 120

 

Currently it returns ALL 120 pages.

Here is how I wrote the script, and ideas or changes would be greatly apprechiated...

 

$offset = ($_GET['page']-1) * 20;
$query = "SELECT count(*) as thecount from Sold $where";
$result = mysql_query($query); 
$row = mysql_fetch_array($result);
$total = $row[thecount];

$pages = ceil($total/20);
$query = "SELECT * FROM Sold $where ORDER BY $order limit $offset,20";
$result = mysql_query($query); 
$numrows = mysql_num_rows($result); 
for($x = 0; $row = mysql_fetch_array($result); $x++) {

       [bUNCH OF PHP STUFF GOES HERE]

}

 

Then on the bottom of the page, where the links are:

Page: <? for($x = 1; $x<= $pages ; $x++) {if($_GET['page'] != $x) { echo '<a href="list.php?category='.$_GET['category'].'&page='.$x.'">'.$x.'</a>';} else {echo $x;}}?>

Link to comment
Share on other sites

There are many different approaches to pagination as you will find out, but you are on the right path with two queries, one for the count, one for the information.

 

Your format:

Page: 1 [2] 3 4 5 6 ... 120

 

I'm assuming 120 is the last page ?

 

You can have three branches of logic assuming you have more than 6 pages.

 

$pagelinks = 6  //how many pages you wanna have

//if close to 1

if ($page < $pagelinks){

    [echo format that works for beginning]

} elseif(($page + $pagelinks) > $lastpage){

    [echo format that works in at the end]

} else {

//somewhere in the middle

  [echo format that works anywhere in the middle range]

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.