On Tue, May 04, 2004 at 02:37:50PM -0700, Chris W. Parker wrote:
> 
> > I don't follow what "$_GET record count from initial query performed
> > above;" is for, but that's the basic methodology.
> 
> well that just meant that after the initial count of records is found it
> will be retrieved from the querystring instead of through a select
> statement (because it had already been performed once before).

I never bother with getting the initial record count.  Unless you want
to display the total number of available pages, of course (in the vein
of Google search result set).

If all you need is to include "Previous" and "Next" buttons in the right
places, you could simply go with:

  $length=40;   // or whatever
  if ($_GET['offset'])
    $offset=$_GET['offset']);
  else
    $offset=0;

  // do the query
  $q="SELECT colums FROM table WHERE yadda LIMIT $offset," . 1+$length;
  $r=mysql_query($q);

  // make Prev/Next links as required
  if ($offset > 0)
    $prev="<a href='?offset=" . $offset-$length . "'>";
  else
    $prev="";
  if (mysql_num_rows($r) > $length)
    $next="<a href='?offset=" . $offset+$length . "'>";
  else
    $next="";

  // show the head
  print " <td align=left>$prev</td>\n";
  print " <td align=left>$next</td>\n";
  print "</tr><tr>\n";

  // show the page
  for ($i=0; $i<$length; $i++) {
    $row=mysql_fetch_array($r);
    print $row['blah']; // wrapped in stuff, of course
  }

The idea here is that we always try to SELECT one more entry than we can
display on the page.  If mysql_num_rows() sees that many rows, then
there's a page after the current one.  And of course, if $offset is > 0
then there's a page before the current one.

I find that this method simplifies my code.  I don't need to store the
result of a COUNT() in a session variable, so the "count" remains
valid even if the number of records changes while someone's browsing.

Note that this is not safe code as is.  :)  At the very least, you
should format-check $_GET['offset'] before using it for anything.

p

-- 
  Paul Chvostek                                             <[EMAIL PROTECTED]>
  it.canada                                            http://www.it.ca/
  Free PHP web hosting!                            http://www.it.ca/web/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to