Current location: Hot Scripts Forums » Programming Languages » PHP » Limiting search results by offset


Limiting search results by offset

Reply
  #1 (permalink)  
Old 11-20-03, 09:53 AM
johno johno is offline
New Member
 
Join Date: Nov 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Limiting search results by offset

Hi there,

I have this below code where it makes a keyword search.
It works fine but my problem is when results are shown I want to use the pagination feature. Like 10 results per page.
I am wondering how can I accomplish this task?

Many thanks in advance.

------------------------------------
<?php

require ('common.php');

PrintHead("Search Messages");

if($logged_in=='yes')
{
echo "
<center><h4>$_lang[searchmessages]</h4></center>
<center>
<form class=\"form\" method=\"post\" action=\"search.php\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td class=\"fieldtitle\">$lang[enterkeyword]&nbsp;&nbsp;</td>
<td><input class=\"textbox\" type=\"text\" name=\"keyword\" title=\"$lang[enterkeyword]\" size=\"20\" maxlength=\"20\"></td>
<td>&nbsp;&nbsp;<input class=\"databutton\" type=\"submit\" value=\"$_lang[search]\"></td>
</tr>
</table>
</form>
</center>";


if(isset($keyword))
{
$keyword = trim($keyword);

if(strlen($keyword)==1) $keyword = "";

if( $keyword != "" )
{

/*
$SQL = "
SELECT LEFT(msg,200) AS msg, id, prev, boardid, posted, subject, author
FROM bweb_messages
WHERE
subject LIKE '%$keyword%' ||
msg LIKE '%$keyword%' ||
author LIKE '%$keyword%'
ORDER BY posted
DESC LIMIT 500
";
*/

/*
$SQL = "
SELECT id, prev, boardid, posted, subject, msg, author
FROM bweb_messages
WHERE
subject LIKE '%$keyword%' ||
msg LIKE '%$keyword%' ||
author LIKE '%$keyword%'
ORDER BY posted
DESC LIMIT 50
";
*/


$SQL = "
SELECT id, subject, msg, author,
MATCH(msg, subject, author) AGAINST('$keyword') AS score
FROM bweb_messages
WHERE MATCH(msg, subject, author) AGAINST('$keyword')
ORDER BY score DESC
";
$result = mysql_query($SQL);
$foundNum = mysql_num_rows($result);


$keyword = stripslashes($keyword);
$keyword = str_replace("," , "", $keyword);

echo "
<center>
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"80%\">
<tr>
<td width=\"100%\" height=\"20\" colspan=\"2\">$_lang[search_results]</td>
</tr>
<tr>
<td width=\"100%\" height=\"1\" bgcolor=\"#808080\" colspan=\"2\"></td>
</tr>";

$i = 1;

while( $row = mysql_fetch_array( $result ) )
{
//$score = $row['score'];
$id = "{$row[ 'id' ]}";
$row[ 'subject' ] = eregi_replace( $keyword, "<b><font color=red>$keyword</font></b>", $row[ 'subject' ] );

$row[ 'msg' ] = eregi_replace( $keyword, "<b><font color=red>$keyword</b></font>", $row[ 'msg' ] );
$row[ 'author' ] = eregi_replace( $keyword, "<b><font color=red>$keyword</b></font>", $row[ 'author' ] );
$short_msg = substr_replace($row['msg'], '...', 200);
$short_msg = htmlspecialchars($short_msg);

echo "
<tr>
<td width=\"3% \" height=\"20\"><b>$i.</b> <!-- $score --></td>
<td width=\"97%\" height=\"20\"><a href=$Config[http_base]/$Config[msg_script]/$id?highlight=$keyword>{$row[ 'subject' ]}</a> by {$row[ 'author' ]} <font color=#808080><small>- " .
strftime( "%c", $row[ 'posted' ] ) . "</small></font></td>
</tr>
<tr>
<td width=\"3%\" height=\"20\"></td>
<td width=\"97%\" height=\"20\">{$short_msg}</td>
</tr>";


$i++;
}

echo "
<tr>
<td width=100% height=1 bgcolor=#808080 colspan=2>
</tr>
</tr>
</table>
</center>";
}
}
mysql_free_result($result);
}
else {
echo $_lang[have2login];
}
PrintFoot();
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-20-03, 10:21 AM
YourPHPPro's Avatar
YourPHPPro YourPHPPro is offline
Community VIP
 
Join Date: Aug 2003
Posts: 430
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by johno
It works fine but my problem is when results are shown I want to use the pagination feature. Like 10 results per page.
Pass two additional variables - offset and results. 'Offset' will tell you when to start the result and 'page' would tell you how many to retrieve:

Quote:
mysql> SELECT * FROM table LIMIT 5,10; # Retrieve rows 6-15
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SEO Expert Available nakulgoyal Job Offers & Assistance 2 08-14-04 01:38 PM
JS Page Search, repetitive? mtayon Script Requests 2 08-31-03 02:28 AM
The best Henry General HotScripts Site Discussion 1 08-19-03 08:43 PM
Easy, Quick Keyword (Quick Jump) dudeman248 Script Requests 1 08-11-03 02:20 AM
search engine question herman PHP 2 08-05-03 12:41 AM


All times are GMT -5. The time now is 06:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.