Hi, could someone give me some advice regarding limiting results per page from a MySQL query. I have created a simple multiple select search engine on a single table from a dropdown form which works a treat but now I have been trying to limit the results returned per page with next/previous links. I don抰 have any problem returning all results on one page or limiting results from a simple select query but when I insert the more complicated multiple query using the coding below I can only view the 1st page of results and no further. Any ideas?
<?php
$link= mysql_connect("localhost","username","password");
mysql_select_db("database", $link);
if ($value1 == $value2)
$sql = "SELECT * FROM table WHERE col3 LIKE '$value3' " ;
elseif($value1 == 'any')
{$sql = "SELECT * FROM table WHERE col2 LIKE '$value2' AND col3 LIKE '$value3' ";}
elseif ($value2 == 'any')
{$sql = "SELECT * FROM table WHERE col1 LIKE '$value1' AND col3 LIKE '$value3' ";}
else
{$sql = "SELECT * FROM table WHERE col1 LIKE '$value1' AND col2 LIKE '$value2' AND col3 LIKE '$value3' "; }
$results=mysql_query($sql);
$numrows=mysql_num_rows($results);
$limit=5; // rows to return
if (!($offset)) {
$offset=0;
}
$sql=$sql."LIMIT $offset, $limit";
$result=mysql_query($sql);
if ($numrows <>1)
print "Your search returned $numrows matches, select BACK to search again<p>";
elseif ($numrows = 1)
{print "Your search returned $numrows match, select BACK to search again<p>";}
print "<center><table border=5 cellspacing=5 cellpadding=5>
\ n";
while(list$value1, $value2, $value3) = mysql_fetch_array($result)) {
print "\t<tr>\n";
print "<td>$value1</td>\n<td>$value2</td>\n<td>$value3</td>\n ";
print "\t</tr>\n";
}
print "</table border=10></center>\n";
if ($offset==1) { // bypass PREV link if offset is 0
$prevoffset=$offset-5;
print "<a href=http://www.phpbuilder.com/board/archive/index.php/\"$PHP_SELF?offset=$prevoffset\">PREV</a> \n";
}
$pages=intval($numrows/$limit);
if ($numrows%$limit) {
$pages++;
}
for ($i=1;$i<=$pages;$i++) { // loop thru
$newoffset=$limit*($i-1);
print "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> \n";
}
if ((($offset/$limit)==$pages) && $pages!=1) {
$newoffset=$offset+$limit;
print "<a href=\"$PHP_SELF?offset=$newoffset\">NEXT</a><p>\n";
}
?>
<?php
$link= mysql_connect("localhost","username","password");
mysql_select_db("database", $link);
if ($value1 == $value2)
$sql = "SELECT * FROM table WHERE col3 LIKE '$value3' " ;
elseif($value1 == 'any')
{$sql = "SELECT * FROM table WHERE col2 LIKE '$value2' AND col3 LIKE '$value3' ";}
elseif ($value2 == 'any')
{$sql = "SELECT * FROM table WHERE col1 LIKE '$value1' AND col3 LIKE '$value3' ";}
else
{$sql = "SELECT * FROM table WHERE col1 LIKE '$value1' AND col2 LIKE '$value2' AND col3 LIKE '$value3' "; }
$results=mysql_query($sql);
$numrows=mysql_num_rows($results);
$limit=5; // rows to return
if (!($offset)) {
$offset=0;
}
$sql=$sql."LIMIT $offset, $limit";
$result=mysql_query($sql);
if ($numrows <>1)
print "Your search returned $numrows matches, select BACK to search again<p>";
elseif ($numrows = 1)
{print "Your search returned $numrows match, select BACK to search again<p>";}
print "<center><table border=5 cellspacing=5 cellpadding=5>
\ n";
while(list$value1, $value2, $value3) = mysql_fetch_array($result)) {
print "\t<tr>\n";
print "<td>$value1</td>\n<td>$value2</td>\n<td>$value3</td>\n ";
print "\t</tr>\n";
}
print "</table border=10></center>\n";
if ($offset==1) { // bypass PREV link if offset is 0
$prevoffset=$offset-5;
print "<a href=http://www.phpbuilder.com/board/archive/index.php/\"$PHP_SELF?offset=$prevoffset\">PREV</a> \n";
}
$pages=intval($numrows/$limit);
if ($numrows%$limit) {
$pages++;
}
for ($i=1;$i<=$pages;$i++) { // loop thru
$newoffset=$limit*($i-1);
print "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> \n";
}
if ((($offset/$limit)==$pages) && $pages!=1) {
$newoffset=$offset+$limit;
print "<a href=\"$PHP_SELF?offset=$newoffset\">NEXT</a><p>\n";
}
?>