NYCPHP Meetup

[nycphp-talk] Using Pagination

Michael Southwell michael.southwell at nyphp.com
Thu Aug 23 15:56:34 EDT 2007


At 01:36 PM 8/22/2007, you wrote:
>I am really sorry to have to ask your help once again, but no matter how
>hard I try I can't seem to understand how pagination works

Paul, here is how I would approach your problem (if I have understood 
it correctly. I'm sure the experts on the list will be
able to improve this code, but it does work:

INDEX.PHP
session_start();

// get stuff from the db, unshown
$pagePointer = 0;
while( $row = mysql_fetch_row( $result ) {
   // store each row in the session
   $_SESSION['pages'][$pagePointer] = $row;
   $pagePointer ++;
}
// count how many rows we have
$_SESSION['pageCount'] = count( $_SESSION['pages'];
// redirect to the display
header( Location:pages.php?start=0 );

PAGES.PHP
<?php
session_start();

// orient ourselves
$start = $_GET['start']; // error checking skipped
$last = $_SESSION['pageCount'] - 1; // arrays start at 0, count starts at 1
$previous = $start - 1;
if ( $previous < 0 ) $previous = $last;
$next = $start + 1;
if ( $next > $last ) $next = 0;

// display this one somehow
print_r( $_SESSION['pages'][$start] );
// provide links to move through each page
?>
<br />
<a href="pages.php?start=<?= $previous ?>">previous</a><br />
<a href="pages.php?start=<?= $next ?>">next</a>




>I created a MySQL database called "test_db" and then  set up the demo table
>"products". the products.php is just a dummy.
>
>nate.php is the coding.
>
>I am trying to paginate through the records one by one.(view each record, in
>this case).
>
>Thanks
>Paul
>
>
>
>
>
>_______________________________________________
>New York PHP Community Talk Mailing List
>http://lists.nyphp.org/mailman/listinfo/talk
>
>NYPHPCon 2006 Presentations Online
>http://www.nyphpcon.com
>
>Show Your Participation in New York PHP
>http://www.nyphp.org/show_participation.php

Michael Southwell, Vice President for Education
New York PHP
http://www.nyphp.com/training - In-depth PHP Training Courses





More information about the talk mailing list