MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 15th, 2007, 12:33 AM
holejazel holejazel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 1 holejazel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Paging -


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT