Forum Moderators: coopster

Message Too Old, No Replies

Prev and Next buttons in search results

         

jumpstartpro

9:22 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



Can anyone help me and show me what I am doing wrong with my code? I am trying to create previous and next buttons with number at the bottom of the search results. Only problem is, when you click on any of the other page links it does nothing but display a blank white page. I have spent the past 3 days trying to get this and other variations to work. So far all I have is a lot nerves that are shot and books thrown around the office. Can anyone here help me out?


<?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>&nbsp;";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "[$i]&nbsp;";
} else {
echo "<a href=\"".$_SERVER['php_SELF']."?page=$i&search=$search\">$i</a>&nbsp;";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['php_SELF']."?page=$next&search=$search\">Next</a>";
}
echo "</center>";
}
?>

mcibor

10:18 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to webmasterworld Jumpstartpro!

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!

jumpstartpro

10:48 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



I got it to work finally.

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.

mcibor

9:35 am on Apr 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



sorry for not finding that: I just put in my checkings $search = 'q'. I'm glad you found it!

Best regards and good luck writing your php
Michal Cibor