Jump to content

Search results pagination


john-formby

Recommended Posts

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:

[code]<?
//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;
}
?>
[/code]


Thanks,

John
Link to comment
Share on other sites

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. 

[code]<?
//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>';
}

?>[/code]


Thanks,

John
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.