sanjay
sanjay

Reputation: 61

php search from mysql database showing only one page

The following script is showing only first page and link to next page is there but is not leading anywhere. Can someone help me?

$var = @$_GET['q'] ;
$trimmed = trim($var);
$limit = 10;

if ($trimmed == "")
{
    echo "<p>What are you looking for?...</p>";
    exit;
}

if (!isset($var)) 
{   
    echo "<p>We dont seem to have a search parameter!</p>";
    exit;
}

mysql_connect('xxx', 'yyy', 'zzz');
mysql_select_db('yyy') or die('Unable to select database');
$query = "select * from table  where NAME like '%$trimmed%' order by NAME";
$numresults = mysql_query($query);
$numrows = mysql_num_rows($numresults);

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

if (empty($s)) 
{
    $s = 0;
}

$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
echo "<p>You searched for: &quot;" . $var . "&quot;</p>";
echo "Results";
$count = 1 + $s;

while ($row= mysql_fetch_array($result)) 
{
    $title = $row["NAME"];
    echo "$count.-&nbsp;$title" ;
    $count++ ;
}

$currPage = (($s/$limit) + 1);
echo "<br />";

if ($s >= 1) 
{ 
    // bypass PREV link if s is 0
    $prevs = ($s - $limit);
    print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;
    Prev 10</a>&nbsp;";
}

$pages = intval($numrows/$limit);

if ($numrows % $limit) 
{
    $pages++;
}  

if (!((($s+$limit)/$limit) == $pages) && $pages != 1) 
{
    $news = $s + $limit;
    print "<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
} 

$a = $s + ($limit);

if ($a > $numrows) 
{ 
    $a = $numrows; 
}

$b = $s + 1;
echo "<p>Showing results $b to $a of $numrows</p>";

Upvotes: 1

Views: 498

Answers (2)

Jason
Jason

Reputation: 814

In your code, $s gets reset every time the page is reloaded or the next page link is clicked. You should have $s = $_REQUEST['s'] at the beginning of your code.

Upvotes: 1

dtbarne
dtbarne

Reputation: 8200

PHP_SELF is a $_SERVER variable. Regardless, you should use $_SERVER['SCRIPT_NAME'] here.

echo "<a href=\"{$_SERVER['SCRIPT_NAME']}?s=$news&q=$var\">Next 10 &gt;&gt;</a>";

Among a few notable improvements, you should really look into sanitizing the variables you put in the querystring with urlencode() as well.

Upvotes: 0

Related Questions