PHP Help with pagination

[email protected] (Karl DeSaulniers)
Newsgroups php.db
Message-ID <[email protected]>
Hey nagendra,
I tried to post this a while ago, but it may be too long for the list.

<?php
if ($Submit) {  // perform search only if correct form was entered.
    $db = mysql_connect ($Host, $User, $Password);
    mysql_select_db ($DBName) or die ("Cannot connect to database");

//check if the starting row variable was passed in the URL or not
if (!isset($_GET['pg']) or !is_numeric($_GET['pg'])) {
   //we give the value of the starting row to 1 because this is the  
first page
   $pg = 1;
//otherwise we take the value from the URL
} else {
   $pg = $_GET['pg'];
}
?>

<?php  // I used the results from a dropdown menu for the users  
selection from the previous page that called this one.
if ($fieldTwentyOne == 'All Users Last Login Date') {
       $fieldTwentyOne = "%";
    }

    $srch="%".$Submit."%";
?>

<?php
/// Variables

$max_results = 5; /// Number of results per page

$min = (($pg * $max_results) - $max_results);
?>

<?php
/// Count total
$query_total_users = "SELECT COUNT(*) as Num FROM users";
$totals = mysql_result(mysql_query($query_total_users, $db),0);
$total_pgs = ceil($totals / $max_results);
?>

<?php
$query_UserLastLogin = "SELECT * FROM users WHERE UserLastLogin LIKE  
'$fieldTwentyOne' ";
$UserLastLogin = mysql_query($query_UserLastLogin, $db) or die 
(mysql_error());
if(!get_magic_quotes_gpc()) { $UserLastLogin = stripslashes 
($UserLastLogin); }
$row_UserLastLogin = mysql_fetch_assoc($UserLastLogin);
?>

<?php
/// Page limiter & result builder

// Access table properties

$query_users = "SELECT * FROM users LIMIT $min, $max_results";
$result = mysql_query($query_users, $db) or die(mysql_error());
$num_sql = mysql_numrows($result);

     if  ($result) {

// Build paging
if($pg > 1) {
	$prev = ($pg - 1); // Previous Link
	$paging = "<a href='".$_SERVER['PHP_SELF']."?Submit=".$Submit."&pg=". 
$prev."'>Previous page&nbsp;</a>";
	//$paging = "<a href=\"{$_SERVER['PHP_SELF']}?pg=$prev\">Previous  
page</a>";
}


for($j = 1; $j <= $total_pgs; $j++) { /// Numbers
	$paging .= "<a href='".$_SERVER['PHP_SELF']."?Submit=". 
$Submit."&pg=".$j."'>&nbsp;".$j."&nbsp;</a>";
	//$paging .= "<a href=\"{$_SERVER['PHP_SELF']}?pg=$ij\">$j</a> "; }}
}

if($pg < $total_pgs) {
	$next = ($pg + 1); // Next Link
	$paging .= "<a href='".$_SERVER['PHP_SELF']."?Submit=". 
$Submit."&pg=".$next."'>&nbsp;Next page</a>";
}
?>

//--------------------

In my html I have a table cel displaying info

For the page numbers:

<td colspan="22" width="90%" align="left" valign="top"  
bgcolor="#5E5E5E"><font size="2" face="Verdana, Arial, Helvetica,  
sans-serif">&nbsp;&nbsp;<p align="left"><?php echo $paging?></p></td>

For showing current page and database totals:

<td colspan="22" width="10%" align="left" valign="bottom"  
bgcolor="#5E5E5E"><font size="2" face="Verdana, Arial, Helvetica,  
sans-serif">Here are the results:<br>Num_sql Rows: <?php echo  
$num_sql?><br>Number of Users: <?php echo $totals?><br>Total Pages: <? 
php echo $total_pgs?><br>Number of Results: <?php echo $totals? 
 ><br>Viewing page <?php echo $pg?> of <?php echo $total_pgs?><br></td>

Then I close the html and php

<?php
} // End if($Submit)
?>

Use this if you want, I took bits and pieces and put this together  
for you, but hope that helps.

Karl DeSaulniers
Design Drumm
http://designdrumm.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.