
August 15th, 2007, 12:33 AM
|
|
Registered User
|
|
Join Date: Aug 2007
Posts: 1
Time spent in forums: 37 m 54 sec
Reputation Power: 0
|
|
|
Paging -
From the few posts I've read here about paging, it would appear that someone has written a tutorial on how to do that. Not having followed that however and I think I'm fairly close to getting this working with a wee bit of help - here goes.
It seems that my very last bit of code I'm using is somehow jacked as it's not creating the paging links, though it does create the placeholder divs.
Code is as follows:
Code:
<?php
require "config.php";
$recordsPerPage = 4;
$pageNum = 1;
if(isset($_GET['p'])) {
$pageNum = $_GET['p'];
settype($pageNum, 'integer');
}
$offset = ($pageNum - 1) * $recordsPerPage;
if ($_GET['s'] != "") // perform search only if a string was entered.
{
mysql_connect('localhost', $username, $password) or die ( "Unable to select database");
mysql_select_db($database);
$s = mysql_real_escape_string($_REQUEST['s']);
$srch="%".$s."%";
$query = "select * FROM asl_apartments WHERE name LIKE '$srch' || address LIKE '$srch' || city LIKE '$srch' || zip LIKE '$srch' || phone LIKE '$srch' LIMIT $offset, $recordsPerPage";
$result = mysql_query($query);
if ($result)
{
echo "
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />
<title></title>
<link href=\"/css/default.css\" rel=\"stylesheet\" type=\"text/css\" media=\"all\" />
<link href=\"/css/print.css\" rel=\"stylesheet\" type=\"text/css\" media=\"print\" />
</head>
<body>
<div id=\"pWrap\">
<form method=\"get\" action=\"search.php\">
<h1 id=\"header\">
<span>Apartments</span>
<span class=\"search\"><input type=\"text\" id=\"srchbx\" name=\"s\" />
<a href=\"javascript:document.terrsrch.submit();\" title=\"Search the Database\">Search</a>
</span>
</h1>
</form>
<ul id=\"navigation\">
<li><a href=\"#\" title=\"\">All Results</a></li>
<li><a href=\"#\" title=\"\">New Entry</a></li>
<li id=\"next\"><a href=\"#\" title=\"\">Next Page</a></li>
<li id=\"prev\"><a href=\"#\" title=\"\">Previous Page</a></li>
</ul>
<div class=\"clr\"></div>
<div class=\"resultsHeader\">
<ul>
<li class=\"name\">Name</li>
<li class=\"address\">Address</li>
<li class=\"city\">City</li>
<li class=\"zip\">Zip</li>
<li class=\"phone\">Phone</li>
<li class=\"completed\">Completed</li>
<li class=\"edit\"></li>
</ul>
</div>
";
while ($r = mysql_fetch_assoc($result)) { // Begin while
$name=$r['NAME'];
$address=$r['ADDRESS'];
$city=$r['CITY'];
$zip=$r['ZIP'];
$phone=$r['PHONE'];
echo "
<div class=\"results\">
<ul>
<li class=\"name\">$name</li>
<li class=\"address\">$address</li>
<li class=\"city\">$city</li>
<li class=\"zip\">$zip</li>
<li class=\"phone\">$phone</li>
<li class=\"completed\"><input type=\"checkbox\" /></li>
<li class=\"edit\"><a href=\"#\" title=\"Edit\">Edit</a></li>
</ul>
</div>
";
} // end while
$query = "select * FROM asl_apartments WHERE name LIKE '$srch' || address LIKE '$srch' || city LIKE '$srch' || zip LIKE '$srch' || phone LIKE '$srch' LIMIT $offset, $recordsPerPage";
$result = mysql_query($query) or die('Mysql Err. 2');
$r = mysql_fetch_assoc($result);
$numrows = $r['dt']; # 4
$maxPage = ceil($numrows/$recordsPerPage);
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= "<div class=\"pNo\">$page</div>";
}
else
{
$nav .= "<div class=\"pNo\"><a href=\"?p=$page\">$page</a></div>";
}
}
if ($pageNum > 1) {
$page = $pageNum - 1;
$prev = "<a href=\"?p=$page\"><strong> Prev </strong></a>";
$first = "<a href=\"?p=1\"><strong> First </strong></a>";
}
else {
$prev = '<strong> </strong>';
$first = '<strong> </strong>';
}
if ($pageNum < $maxPage) {
$page = $pageNum + 1;
$next = "<a href=\"?p=$page\"><strong> Next </strong></a>";
$last = "<a href=\"?p=$maxPage\"><strong> Last </strong></a>";
}
else {
$next = '<strong> </strong>';
$last = '<strong> </strong>';
}
echo $data;
echo '<br />';
echo "
<div class=\"pagingDiv\">
<div class=\"pNo\">$first</div>
<div class=\"pNo\">$prev</div>
$nav
<div class=\"pNo\">$next</div>
<div class=\"pNo\">$last</div>
</div>";
} else { echo "<h1>Captain! I'm giving her all I've got! We need more power!</h1>"; }
} else {
echo "
<h1><span>Apartments</span> <span class=\"search\"><input type=\"text\" id=\"srchbx\" /> <a href=\"javascript:document.terrsrch.submit();\" title=\"Search the Database\">Search</a></span></h1>
<div class=\"results\">
<p>Search string is empty. <br /> Go back and type a string to search</p>
</div>
";
}
echo "
</div>
</body>
</html>
"
?>
I'm fairly new to PHP / MySQL so I apologize in advance if any of this code is old or just generally whacked. Been trying to piece it together from various tutorials and what not.
J
|