problem: you have a series of records to traverse inside your CMS (customers, users, blog entries, whatever). in managing this content sometimes records can get deleted, so you have to traverse records and not record IDs. so you build an array of the entries with the IDs as values and traverse that.

firstly the paginate function, usage: $pager=paginate(transaction, $pid);, where transaction is the table name and $pid is the current page number.

based upon the total number of records found, this will return $pager[0] and $pager[1] for the record IDs that you should use in SQL to select the right set of records, i.e. where id between $pager[0] and $pager[1] order by id desc, as the selection criteria. it will also return $pager[2] as the next page, $pager[3] as the previous page and $pager[4] as the last page and you can then use them as values for hidden form elements posting back to the admin.php page (or whatever you call your CMS page) for subsequent processing. (the first page value will obviously be 1), e.g.:

<form name="listtnf" method="post" action="admin.php"><input type="hidden" name="action" value="rtrans" /><input type="hidden" name="pid" value="<?php echo $pager[2] ?>" /><a href="admin.php" onclick="document.listtnf.submit(); return false">Next</a></form>

here is the function (based on 20 records per page):

function paginate($table, $pid) {

include 'includes/dbconn.php';
$ppx=$db->query("select id from $table order by id desc");
$xc=1;
$pc=1;
while ($xid=$ppx->fetch_row()) {
if ($xc%20==0) $page[]=$pc++;
$stack[]=$xid[0];
$xc++; }
$xc--;
$np=floor($xc/20);
if (!$np) $np=1;
if (($xc/20) > $np) { $np=$np+1; $page[]=$pc++; }

if ($pid==1) {
$pager[0]=$stack[19];
$pager[1]=$stack[0];
} else {
if ($stack[($pid*20)]) {
$pager[0]=$stack[($pid*20)-1];
$pager[1]=$stack[($pid*20)-20];
} else {
$pager[0]=$stack[$xc-1];
$pager[1]=$stack[$pid*20-20];
}
}
$ppid=$pid-2;
if (($pid+1)>$np) $pager[2]=$page[0]; else $pager[2]=$page[$pid];
if (($pid-1)<1) $pager[3]=$page[$np-1]; else $pager[3]=$page[$pid-2];
$pager[4]=$np;
return $pager;
}

secondly we have individual record traversal. once we've clicked on the individual record in the records overview, we go to view the individual record and we want to be able to 'next' and 'previous' to other records.

usage: $nxtid=nextid(transaction, $bid);, where transaction is the table name and $bid is the current record being viewed.

this will return $nxtid[0] as the next record ID and $nxtid[1] as the previous one. example of use:

<form name="vsfn" method="post" action="admin.php" ><input type="hidden" name="action" value="vsale" /><input type="hidden" name="bid" value="<?php echo $nxtid[0] ?>" /><a href="admin.php" onclick="document.vsfn.submit(); return false">Next</a></form>

..and here is the function:

function nextid($table, $bid) {

include 'includes/dbconn.php';
$ppx=$db->query("select id from $table order by id");
$xc=0;
while ($xid=$ppx->fetch_row()) {
$stack[]=$xid[0];
if ($xid[0]==$bid) $cpa=$xc;
$xc++; }
$xc--;
if (($cpa+1)>$xc) $nextid[0]=$stack[0]; else $nextid[0]=$stack[$cpa+1];
if (($cpa-1)<0) $nextid[1]=$stack[$xc]; else $nextid[1]=$stack[$cpa-1];
return $nextid;
}

(obviously these two functions would sit in an external file and get called)


any suggestions to make this simpler or better i'm happy to hear!