Click to See Complete Forum and Search --> : Simple Pagination


Markbad311
02-18-2006, 11:41 AM
I would like to limit the results to 10 and provide links for previous and next pages. I have searched a lot around the page but I am still having a tough time finding something I can use. I understand the basics of it but I don't know if I can make it without 20 errors! haha

Anyway I have the php file and the function library corresponding to the page.

Page PHP:

<?php
$page_title = "Erie Scene Home Page";
require("listingsfunc.php");
if ($_GET['id'] != "") {
dbconnect();
sidemenu($connection);
showsingle($connection);
} if ($_GET['show_all_erie_pa_bars']) {
dbconnect();
showall($connection);
$align = "<div id=\"align\">";
$alignclose = "</div>";
} else if (!$_GET['id']) {
dbconnect();
eventreg($connection);
sidemenu($connection);
$align = "<div id=\"align\">";
$alignclose = "</div>";
}
pagecount($page_title);
// assume $text has been populated with the desired text from the DB
$details = preg_replace('/\w(erie|bands?|events?)\w/i', "<strong>$1</strong>", $details);
mysql_close($connection);
?>


Func File:

function eventreg($connection) {
global $display_block;
$table_name = "events";
$sql = "SELECT date, bar, DATE_FORMAT(`updated`,'%M-%d-%Y') updated, details, map_url FROM $table_name WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 DAY) and DATE_ADD(CURDATE(), INTERVAL 30 DAY) ORDER BY 'bar'";
$result = mysql_query($sql)
or die (mysql_error());
$display_block ="";
//While loop will create an array called $row
while ($row = mysql_fetch_array($result)) {
// get individual elements from events
$date = ($row['date']);
$bar = ($row['bar']);
$updated = ($row['updated']);
$details = ($row['details']);
$map_url = ($row['map_url']);
// assume $text has been populated with the desired text from the DB
$details = preg_replace('/\b(erie|bands?|events?|bars?|drinks?|dj?|shows?)\b/i', "<strong>$1</strong>", $details);

$display_block .= "
<h4>$bar <span class=\"date\"> added on:$updated</span></h4>
<p>$details</p>
<div align=\"right\"><span class=\"map\"><a href=\"$map_url\" target=\"_blank\" title=\"Erie Bar :: $bar\">Get Directions</a></span></div>
";
}

}


This is probally gonna be real easy, but I want to be absolutely sure it is right.

bathurst_guy
02-18-2006, 07:43 PM
See if this thread (http://www.webdeveloper.com/forum/showthread.php?t=93438&highlight=previous) helps

Markbad311
02-19-2006, 12:48 AM
What do ya think look in order?


<?php
function restuarant($connection) {
$show = 20;
$start = (isset($_GET[start]))?$_GET[start]:0;
global $display_block;
/////////
$table_name = "restaurant";
$sql2 = mysql_query("SELECT COUNT(*) FROM $table_name");
$howmany = mysql_fetch_row($sql2) or die("Error Line 8." . mysql_error());
////////
$sql = "SELECT * FROM $table_name ORDER BY 'restaurant' LIMIT $start, $show";
$result = mysql_query($sql) or die("Error Line 11." . mysql_error());
////
$count = 0;
while ($row = mysql_fetch_array($result)){
$count++;
extract($row);
$display_block .= "
<h4>'restaurant'</h4>
<p>'location'</p>
<p><strong>Erie Pa</strong></p>
<div align=\"left\"><span class=\"map\">
<a href=\"'map_url'\" target=\"_blank\" title=\"Erie Restaurant Guide :: 'restaurant'\">
Get Directions
</a>
</span></div>
<hr align=\"left\" width=\"50%\">";
}
////////
$display_block .= "</table>";
////////
$total = $start + $show;
////////
if($howmany[0] > $total){
$next = $start + $show;
$prev = $start - $show;
$display_block .= "
<div align=\"center\">
<a href="'.$_SERVER['PHP_SELF'].'?start=$prev">Previous $show</a>
| <a href="'.$_SERVER['PHP_SELF'].'?start=$next">Next $show</a>
</div>
<p>Showing Listings $start to $total of $howmany[0].</p>";
} else {
$prev = $start - $show;
$display_block .= "
<div align=\"center\">
<a href="'.$_SERVER['PHP_SELF'].'?start=$prev">Previous $show</a>
</div>
<p>Showing Listings $start to $total of $howmany[0].</p>";
}
}
?>


I just got finished modifying it haven't had a chance to see if it parses correctly it is almost 2am here I got to get some shut eye. Thanks for all your help so far!!! :o

There was however a few things I added. a conditional to check if there is any more results left to display and if not then only display one link. And I didn't get the $howmany variable why you had two $howmany variables

Original Lines

$howmany = mysql_query("a query in here");
$howmany = mysql_fetch_row($howmany);


////and

if($howmany[0] > $total){
$next = $start + $show;
$prev = $start - $show;
echo '<a href="'.$_SERVER['PHP_SELF'].'?start='.$prev.'&amp;type='.$type.'">Previous '.$show.'</a> | <a href="'.$_SERVER['PHP_SELF'].'?start='.$next.'&amp;type='.$type.'">Next '.$show.'</a>';
}

Markbad311
02-19-2006, 02:54 PM
This Works good but has one flaw. How do i determine (very bottom of function) if I am at 0-10 and not show a previous link. Any Help???


function barlist($connection) {
$show = 20;
$start = (isset($_GET['start']))?$_GET['start']:0;
global $display_block;
/////////
$table_name = "map";
$sql2 = mysql_query("SELECT COUNT(*) FROM $table_name");
$howmany = mysql_fetch_row($sql2) or die("Error Line 8." . mysql_error());
////////
$sql = "SELECT * FROM $table_name ORDER BY 'bar' LIMIT $start, $show";
$result = mysql_query($sql) or die("Error Line 11." . mysql_error());
////
$count = 0;
while ($row = mysql_fetch_array($result)){
$bar = ($row['bar']);
$location = ($row['location']);
$map_url = ($row['map_url']);
$map_url = htmlspecialchars($map_url);
$count++;
$display_block .= "
<h4>$bar</h4>
<p>$location</p>
<p><strong>Erie Pa</strong></p>
<div align=\"left\"><span class=\"map\"><a href=\"$map_url\" target=\"_blank\" title=\"Erie Bar Directory :: $bar\">Get Directions</a></span></div>
<hr align=\"left\" width=\"50%\">
";
}
////////

////////
$total = $start + $show;
////////
if($howmany[0] > $total){
$next = $start + $show;
$prev = $start - $show;
$display_block .= "
<div align=\"center\">
<a href=\"$_SERVER[PHP_SELF]?start=$prev\">Previous $show</a>
| <a href=\"$_SERVER[PHP_SELF]?start=$next\">Next $show</a>
</div>
<p>Showing Listings $start to $total of $howmany[0].</p>";
} else {
$prev = $start - $show;
$display_block .= "
<div align=\"center\">
<a href=\"$_SERVER[PHP_SELF]?start=$prev\">Previous $show</a>
</div>
<p>Showing Listings $start to $total of $howmany[0].</p>";
}


}