[thelist] php/mysql previous next function

Edwin Martin e.j.martin at chello.nl
Wed Mar 27 14:12:05 CST 2002


Hi kris burford [midtempo],

>i'm trying to do a simple previous, next structure to display a large list
>of returned fields from a mysql database, plus displaying numbers of
>results pages a la google.

I once wrote a simple PHP-function to generate a
AltaVista-style (or Google-style, if you prefer)  'browse bar'.

Just fill in the arguments and the bar will be printed.

You can leave $q to an empty string ("").

Example:
pager( $pg, 10, 36, "" );

Bye,
Edwin Martin.

// $pg: Current page
// $pglen: Number of items on a page
// $cnt: Total number of items
// $q: Query string (optional)
function pager( $pg, $pglen, $cnt, $q ) {
         $pages = ceil($cnt/$pglen);
         if ( $pages == 1 )
                 return;
         $q = $q?"q=$q&":"";
         echo "<p>";
         if ( $pg > 1 )
                 echo "<a href='?${q}pg=".($pg-1)."'>&lt;- Previous</a> ";
         for ( $i = 1; $i <= $pages; $i++ ) {
                 if ( $i == $pg )
                         echo "<b>$i</b> ";
                 else
                         echo "<a href='?${q}pg=$i'>$i</a> ";
         }
         if ( $pg < $pages )
                 echo "<a href='?${q}pg=".($pg+1)."'>Next -&gt;</a> ";
         echo "</p>\n";
}





More information about the thelist mailing list