Jump to content


Photo

Search results pagination


  • Please log in to reply
6 replies to this topic

#1 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 30 June 2006 - 10:50 AM

Hi,

I have a search on my website which is working perfectly thanks to the help I have received on this site :)  The only problem is that some seaches could return hundreds of results so I would really like a way of splitting them up into pages.  I deally I would like something like [1] [2] [3] with the current page number not clickable.  I don't know how to do this though.  I have tried adapting some stuff I found online but I can't get any of it to work ???

Here is my search processing and results page:

<? 
//This is only displayed if they have submitted the form 
if ($_POST['searching'] =="yes") 
{ 

//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

// Otherwise we connect to our Database 
mysql_connect("localhost", "USER", "PASS") or die(mysql_error()); 
mysql_select_db("pagelinks") or die(mysql_error()); 

// We preform a bit of filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

//Now we search for our search term, in the field the user specified 
$data = mysql_query("SELECT * FROM links WHERE keywords LIKE '%$find%'"); 

//And we display the results 
while($result = mysql_fetch_array( $data )) 
{ 

echo "<hr /><br />";
if ($result['new_window'] == 1) {
  echo "<b><a href=\"".$result['url']."\" target=\"_new\">".$result['link_text']."</a></b>"; 
} else {
  echo "<b><a href=\"".$result['url']."\">".$result['link_text']."</a></b>"; 
} 
echo "<br />"; 
echo $result['description']; 
echo "<br />"; 
if ($result['new_window'] == 1) {
  echo "<a href=\"".$result['url']."\" target=\"_new\">".$result['url']."</a>"; 
} else {
  echo "<a href=\"".$result['url']."\">".$result['url']."</a>"; 
}
echo "<br /><br />";  
} 

echo "<hr /><br />";

//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 

//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find; 
} 
?>


Thanks,

John
"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 30 June 2006 - 11:35 AM

Lots of them out there:
http://www.google.co...p paging script

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 30 June 2006 - 03:45 PM

I have had a few attempts and can now get the page numbers to display the way I want them.  Unfortunately I no longer have any results displayed and I get the message error message "Sorry, but we can not find an entry to match your query"

Please can someone look at the code below and tell me where I have gone wrong. 

<? 
//This is only displayed if they have submitted the form 
if ($searching =="yes") 
{ 

//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

// Otherwise we connect to our Database 
mysql_connect("localhost", "USER", "PASS") or die(mysql_error()); 
mysql_select_db("pagelinks") or die(mysql_error()); 


$pageSize = 5;

$pageNumber = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;


// We preform a bit of filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

//Now we search for our search term, in the field the user specified 
$query="SELECT * FROM links WHERE keywords LIKE '%$find%'".(($page-1) * $pageSize).','.$pageSize; 
$result=mysql_query($query);

//And we display the results 
while ($row = mysql_fetch_assoc($result)) 
{ 

echo "<hr /><br />";
if ($result['new_window'] == 1) {
  echo "<b><a href=\"".$result['url']."\" target=\"_new\">".$result['link_text']."</a></b>"; 
} else {
  echo "<b><a href=\"".$result['url']."\">".$result['link_text']."</a></b>"; 
} 
echo "<br />"; 
echo $result['description']; 
echo "<br />"; 
if ($result['new_window'] == 1) {
  echo "<a href=\"".$result['url']."\" target=\"_new\">".$result['url']."</a>"; 
} else {
  echo "<a href=\"".$result['url']."\">".$result['url']."</a>"; 
}
echo "<br /><br />";  
} 

echo "<hr /><br />";

//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches=mysql_num_rows($result); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 

//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find. "<br/><br />"; 
}

$numberOfRecords = mysql_result(mysql_query("SELECT count(*) FROM links WHERE keywords LIKE '%$find%'"),0,0);
for($i = 0; $i < $numberOfRecords; $i += $pageSize) {
  $page = 1 + ceil($i/$pageSize);
  echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$page.'"> [ '.$page.' ] </a>';
}
 
?>


Thanks,

John
"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking

#4 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 30 June 2006 - 04:27 PM

You are forgeting the limit in the query

keywords LIKE '%$find%' limit ".(($page-1) * $pageSize).','.$pageSize

NoGray.com


#5 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 30 June 2006 - 04:34 PM

Thanks, but the results are still not showing up  ???

John
"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking

#6 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 30 June 2006 - 06:56 PM

try to add mysq_error() to your query to find out if you have any errors

$result=mysql_query($query) or die(mysql_error());

NoGray.com


#7 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 30 June 2006 - 10:53 PM

Yay, its sorted  ;D  Pages needed to be set up as an array.

John
"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users