Forum Moderators: coopster

Message Too Old, No Replies

long result set, how to create links on pages?

         

dbarasuk

5:50 pm on Jul 30, 2009 (gmt 0)

10+ Year Member



Hello,

I often see on php pages numbers on which you can click to see the resultset of a query from a mysql database that returns a big number of records.

I know how to create the next, previous and... links to traverse the resultset as long as there are still some records to be shown on next pages.

However, i have no idea of how i can put numbered links (1 2 3 4 5 6 7 ...) on the bottom of a page, so that when i click on the number i may be redirected to the content of that particular result set of the query.

Can someone point me to the right tutorial to do this?

Thanks

d40sithui

6:29 pm on Jul 30, 2009 (gmt 0)

10+ Year Member



The methodology for this is called pagination. If you do a google search for "php pagination" you will find a lot of methods. Basically you will do a main query to get all your rows. Then, depending on how many records you want to be displayed on each page, you will divide that number by the number of rows retrieved. So if you have 87 records retrieved, and you want 10 records as the limit to be displayed on each page, then you know that you will have 9 pages total (87/10 rounded up). All the pages will have 10 records, while the last page will have 7 records. My method is a bit complicated using 2d arrays and several loops and ifs to get it done. That was before I knew about seeking help on the web, but I'm too lazy to figure new (and probably more efficient) methods so I still use it. If you want to see my code I'll be happy to share. Good luck =)

jatar_k

2:44 pm on Jul 31, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



i use this on a site, not stupendous but it works

// start paging 
$paging = "";
$paging .= "<td colspan='4' valign='top' align='right'>\n";
$sql1 = "select count(comm_id) as totcomm from stcomment";
$query1 = mysql_query($sql1) or die("<p>oops, couldn't get the commcount: " . mysql_error());
$tcr = mysql_fetch_array($query1);
$totcomm = $tcr['totcomm'];
$numpages = ceil($totcomm/20);
$counter = 0;
while ($counter < $numpages) {
$start = $counter * $perpage;
$pg = $counter + 1;
if ($counter + 1 == $numpages ¦¦ $start == 0 ¦¦ ($start >= $offset - 40 && $start <= $offset + 40)) {
if ($counter == $offset/$perpage) $paging .= " <b>$pg</b> ";
else $paging .= " <a href='comments.php?s=$start'>$pg</a> ";
//echo "¦ <a href='comments1.php?s=$start'>$pg</a>";
} else {
$paging .= '.';
}
$counter++;
}
$paging .= " </td>\n";
// end paging
echo $paging;