04-08-2009, 10:29 PM
|
#1 (permalink)
|
|
The Contributor
Join Date: Mar 2009
Location: Springfield, IL USA
Posts: 75
Thanks: 3
|
Php & MySql Pagination Script
Code:
<?php
$newp = $_GET['p'];
$plimit = "10";
$strSQL = mysql_query("SELECT * FROM `your_table` WHERE(`field1` LIKE \"$variable%\" AND `field2`=\"whatever\")");
$totalrows = mysql_num_rows($strSQL);
$pnums = ceil ($totalrows/$plimit);
if ($newp==''){ $newp='1'; }
$start = ($newp-1) * $plimit;
$starting_no = $start + 1;
if ($totalrows - $start < $plimit) { $end_count = $totalrows;
} elseif ($totalrows - $start >= $plimit) { $end_count = $start + $plimit; }
?>
<div>viewing <?php print_r("$starting_no");?> - <?php print_r("$end_count");?> of <?php print_r("$totalrows");?> entries</div>
<?php
if ($totalrows - $end_count > $plimit) { $var2 = $plimit;
} elseif ($totalrows - $end_count <= $plimit) { $var2 = $totalrows - $end_count; }
?>
<ul>
<li id="pages">pages: </li>
<?php if ($newp>1) { ?>
<li><a href="<?php echo "http://yourdomain.com/page.php?p=".($newp-1);?>">«</a></li>
<?php } for ($i=1; $i<=$pnums; $i++) { if ($i!=$newp){ ?>
<li><a href="<?php echo "http://yourdomain.com/page.php?p=$i";?>"><?php print_r("$i");?></a></li>
<?php } else { ?>
<li id="current"><?php print_r("$i");?></li>
<?php }} if ($newp<$pnums) { ?>
<li><a href="<?php echo "http://yourdomain.com/page.php?p=".($newp+1);?>">»</a></li>
<?php } ?>
</ul>
<ul>
<?php
$strSQL = mysql_query("SELECT `field1`, `field2`, `field3`, `field4` FROM `your_table` WHERE(`field1`=\"whatever\" AND `field2`=\"that\") ORDER BY `field1` DESC, `field2` ASC LIMIT $start,$plimit");
while ($row = mysql_fetch_array($strSQL)) {
$field1 = $row["field1"];
$field2 = $row["field2"];
$field3 = $row["field3"];
$field4 = $row["field4"];
?>
<li>This is all I know about <?=$field1?> and <?=$field2?> ... though <?=$field3?> and <?=$field4?> interest me too.</li>
<?php
}//END SQL
?>
</ul>
|
|
|
|