Forum Moderators: coopster
<?php
$search = @$_GET['q'] ;
mysql_connect("localhost","username","password");
mysql_select_db("database") or die("Unable to select database"); if($search)
{
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page
$max_results = 30;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
$sql = mysql_query("SELECT * FROM ads WHERE ad_number LIKE '%$search%' LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){
// Build your formatted results here.
// replace title with desired output
echo $row['ad_number']."<br />";
} break;
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ads WHERE ad_number LIKE '%$search%'"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
echo "<center>$total_results Total results<br />";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['php_SELF']."?page=$prev&search=$search\">Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "[$i] ";
} else {
echo "<a href=\"".$_SERVER['php_SELF']."?page=$i&search=$search\">$i</a> ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['php_SELF']."?page=$next&search=$search\">Next</a>";
}
echo "</center>";
}
?>
Your pagination code is alright, I have tested it and it doesn't sprout any mistakes.
First of all there shouldn't be break after while loop. Otherwise it works perfectly fine with me.
if that doesn't help, then see the source code that is being written. And turn on showing errors and warnings. i think that you have it off.
Best regards!
Michal Cibor
PS I would add a limit on showed pages, because if you get more than 20 it may get a little dizzy. Also good idea is to put [First] and [Last] button as well.
Cya!
I just changed:
$search = @$_GET['q'] ; to
$search = @$_GET['search'] ;
I then went to my search.html page and changed the name of the search filed from q to search.
I guess this is what I get for trying to learn by taking bits and pieces of other people's code and making it work. I just wish all the books I have explained some of this coding in there books, but they don't.