[PHP] 'Previous' 1, 2, 3, 4, etc. 'Next' From: ctan (ctan <email protected>)
Date: 07/25/02

I seem to have a problem getting the page to display beyond the limit if a
page, i.e. if the limit if 10 rows in a page I'll only get the 1st ten rows
and then a link to further rows but when I chick on them they give me
nothing. Here's the code:

$searchword = $_POST['searchword'];
print "Your word(s) is/are: <b>$searchword</b><p>\n\n";

// Searching by keyword
if (! empty($searchword )){
   $max = 0;
   $query = "SELECT aml FROM arguments WHERE aml LIKE '%$searchword%'";
   $result1 = mysql_query($query)
                                or die ("Query failed");

// Determine the number of items containing the $searchword
         while ($line1 = mysql_fetch_array($result1)){
         $max++;
         }

// The number of results to be displayed on screen
$maxresult = 10;

$sql_text = "SELECT aml FROM arguments WHERE aml LIKE '%$searchword%'";

// When the current page is yet to be determined
if (!$page) {
         $page = 1;
         }
    
$backpage = $page - 1;
$nextpage = $page + 1;
$result2 = mysql_query($sql_text);
$start = ($maxresult * $page) - $maxresult;
$num_rows = mysql_num_rows($result2);

// When the query returns less or equal number of rows than the limit set by
$maxresult
if ($num_rows <= $maxresult) {
         $num_pages = 1;
   }

// When the query returns the exact limit set by $maxresult
else if (($num_rows % $maxresult) == 0) {
   $num_pages = ($num_rows / $maxresult);
   }

// For any other cases...
else {
   $num_pages = ($num_rows / $maxresult) + 1;
   }

// Declared as an integer
$num_pages = (int) $num_pages;

// The current page is greater than the total number of pages or
// the current page is less than 0
if (($page > $num_pages) || ($page < 0)) {
         error("You have specified an invalid page number");
   }

// Set the limit per page
$sql_text = $sql_text . " LIMIT $start, $maxresult";
$result2 = mysql_query($sql_text);

// The navigation between pages
// Ensure only display when total number of return results exceeds
$maxresult
// i.e. will not display if only 1 page
if ($max>$maxresult){
         print "<center>- ";
   if ($backpage) {
   print "<a
href=\"$PHP_SELF?searchword=$searchword&page=$backpage\">Prev</a>";
   }

// If its the first page; have 'Prev' un-clickable
else {
   print "Prev";
   }

for ($i = 1; $i <= $num_pages; $i++) {
   if ($i != $page) {
                         
      print " <a href=\"$PHP_SELF?searchword=$searchword&page=$i\">$i</a> ";
   }
         else {
      print " $i ";
   }
}
    
if ($page != $num_pages) {
   print "<a
href=\"$PHP_SELF?searchword=$searchword&page=$nextpage\">Next</a> -";
   }
else {
   print "Next -";
   }
   print "</center>";
}

print "<table border=\"1\"><th BGCOLOR=\"#ff0000\">Results</th>";
while ($line = mysql_fetch_array($result2, MYSQL_ASSOC)) {
                        print "\t<tr BGCOLOR=\"#000080\">\n";
                        // The different color, in this case light blue will
show that
                        // the particular table belong to search by
keywords
                  foreach ($line as$col_value) {
                                            print "\t\t<td>$col_value</td>\n";
      }
      print "\t</tr>\n";
}

}

I suspect the problem is because I set the limits to a page and thus
anything more is ignored. That particular area of code is highlighted in
red. Could anyone please tell me where I have gone wrong and how I can fix
this. Thank you loads in advance. Incidentally this will be use php script
to conduct search of a database, whereby the user inputs a 'searchword' to
look for in a HTML form and the php script which processes the form input.

Chia