By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,199 Members | 1,440 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,199 IT Pros & Developers. It's quick & easy.

Search page's result

P: 51
HI All
I have a search page' s result which view 3results/per page,but my "next" link to view next page is not working.
Here is my code :


[PHP]<?php
// Get the search variable from URL
$string = @$_GET['q'] ;
$trimmed = trim($string); //trim whitespace from the stored variable
// rows to return
$limit=3;
// check for an empty string and display a message.
if ($trimmed == "")
{
echo "";
exit;
}
// check for a search parameter
if (!isset($string))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database
mysql_connect("localhost","purpasia_CMS","CMS"); //(host, username, password)

//specify database
mysql_select_db("purpasia_VFM") or die("Unable to select database"); //select which database we're using

// Build SQL Query
$query = "select * from en_c1 where t1 like \"%$trimmed%\"
order by t1"; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";

// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}else

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
//echo "<p>You searched for: &quot;" . $var . "&quot;</p>";

// begin to show results set
echo "Results";
$count = 1 + $s ;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["t1"];

echo "$count.)&nbsp;$title" ;
$count++ ;
}

$currPage = (($s/$limit) + 1);

//break before paging
echo "<br />";

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$string\">&lt;&lt;
Prev</a>&nbsp&nbsp;";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
$news=$s+$limit;

echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$string\">Next&gt;&gt;</a>";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $a of $numrows</p>";

?>[/PHP]

Any idea why is this problem?
Here is the output : http://www.purpleasia.com/clients/VF....php?s=1&q=vfm
thanks so much
Apr 28 '08 #1
Share this Question
Share on Google+
5 Replies


Markus
Expert 5K+
P: 6,050
You do realise the difference between php and html? Use the correct code tags.

[php]
<?php
// Get the search variable from URL
$string = @$_GET['q'] ;
$trimmed = trim($string); //trim whitespace from the stored variable
// rows to return
$limit=3;
// check for an empty string and display a message.
if ($trimmed == "")
{
echo "";
exit;
}
// check for a search parameter
if (!isset($string))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database
mysql_connect("localhost","purpasia_CMS","CMS"); //(host, username, password)

//specify database
mysql_select_db("purpasia_VFM") or die("Unable to select database"); //select which database we're using

// Build SQL Query
$query = "select * from en_c1 where t1 like \"%$trimmed%\"
order by t1"; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";

// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}else

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
//echo "<p>You searched for: &quot;" . $var . "&quot;</p>";

// begin to show results set
echo "Results";
$count = 1 + $s ;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["t1"];

echo "$count.)&nbsp;$title" ;
$count++ ;
}

$currPage = (($s/$limit) + 1);

//break before paging
echo "<br />";

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$string\">&lt;&lt;
Prev</a>&nbsp&nbsp;";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
$news=$s+$limit;

echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$string\">Next&gt;&gt;</a>";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $a of $numrows</p>";

?>
[/php]
Apr 28 '08 #2

rpnew
100+
P: 188
Hi,
It seems that you are not setting the variable 'S' while reloading the page.
You need to do something like this.
[PHP]
if(isset($_GET['s']))
{
$s=$_GET['s'];
}
$string = @$_GET['q'] ;
$trimmed = trim($string);
[/PHP]

So even if you are clicking NEXT you are still with s=0.
Let me know if i'm wrong or if it helps...

Regards,
RP
Apr 29 '08 #3

P: 51
Thanks rpnew
Now it s working
Pls help me next time if i have question
Thanks again
Best regards!!
Apr 30 '08 #4

rpnew
100+
P: 188
Thanks rpnew
Now it s working
Pls help me next time if i have question
Thanks again
Best regards!!
Hi,
Glad that you got it working.
Post back to forum whenever you've any problem or questions...

Regards,
RP
Apr 30 '08 #5

P: 1
although this thread has been long but this is very helpful thank you...
Aug 20 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.