1

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>";
4
  • your method of pagination is very primary. Try building classes and functions. Commented Apr 22, 2012 at 5:28
  • 1
    In addition, this code is vulnerable to SQL injection attacks. See mysql_real_escape_string(). Commented Apr 22, 2012 at 5:32
  • @GarrettAlbright Not if magic_quotes is enabled. Commented Apr 22, 2012 at 5:34
  • Is magic_quotes enabled on the host? What if they disable it? What if you decide to move to another host and forget to check that they have it enabled? It's nuts to expect magic configuration you may have no control over to make your code safe when it's really not that hard to just ensure its safety yourself. Commented Apr 22, 2012 at 5:37

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.