Forum Moderators: coopster

Message Too Old, No Replies

Pagination no longer working

Server upgrade stopped script from passing next and previous results

         

kcal08

5:22 pm on Mar 8, 2008 (gmt 0)

10+ Year Member



I found and old script no longer working and trying to find the bug. Works on my local server but not on remote. Can someone please take a peek. I am new to the forum but plan on contributing back often.

<?php

// Get the search variable from URL
$var = @$_GET['id'] ;
$trimmed = trim($var); //trim whitespace from the stored variable

// check for a search parameter
if (!isset($var))
{
echo "<p>Something here</p>";
exit;
}

$db_name = "blah";
$table_name = "blah";
$connection = @mysql_connect("localhost", "", "")
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());

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

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

// rows to return
$limit=20;

// 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>";
}

// 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, $connection) or die (mysql_error());

// display what the person searched for

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

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

// now you can display the results returned
$item_list .= "<ul>";
while ($row = mysql_fetch_array ($result)) {
$id = $row['id'];
$item_name = $row['item_name'];
echo "$count.)&nbsp;<a href=\"somepage.php?id=$id\">$item_name</a><br>";
$count++ ;

$item_list .= "</ul>";

}

$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&id=$var\">&lt;&lt;
Prev 20</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&id=$var\">Next 20 &gt;&gt;</a>";
}

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

?>

cameraman

6:10 pm on Mar 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, kcal08.

It looks like your script is depending on register_globals [us2.php.net] being enabled:
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

Look for $_GET['s']

kcal08

8:38 pm on Mar 8, 2008 (gmt 0)

10+ Year Member



Cameraman. Thanks for the help.
if (isset($_GET['s'])) {
$s = $_GET['s'];
} else {
$s=0;
}
Worked like a charm :)