I basically have a form to search a catalog.
The user can select a manufacturer, search by Item# or a Description and enter the text or number to search for.
Well everything works fine (i see my results, generates the next and page links with correct starting limits and even the back link) until the next or page links are pressed. Then i get this error.
It seems to me that my variables in my sql query are being nuked somewhere when the script is passed again with new limits and therefore give me these errors.
Warning: Supplied argument is not a valid MySQL result resource in /var/www/html/php/T51/catalogsearch2.php on line 19
Results 51 to of
Page 2 of 1.
matches found for your search of in the catalog!
Warning: Supplied argument is not a valid MySQL result resource in /var/www/html/php/T51/catalogsearch2.php on line 63
BACK - 1 2
...is there anyway to make the variables defined in the form by the user to search by static or stay in the script so that they can search to the next pages without losing the values or the variables again?.....
Here is the code:
<?
$db = mysql_connect("localhost", "sam", "sam") or die ("Unable to connect!");
mysql_select_db ("T51partsDB");
$row_color1 = "#99CCCC";
$row_color2 = "#999999";
$newArray_count = 0;
$limit=50; // rows per page
if (!isset($page))
{
$page = 0;
}
$query = "select count(*) from $option1 where $option2 like '%$search%'";
$numresults=mysql_query($query, $db);
if (mysql_num_rows($numresults) == 1)
{
list($numrows) = mysql_fetch_row($numresults);
}
$query = "select ITEM, DESCRIPTION, UOM, QTY_PER_PCKG, LIST_PRICE, NOTES from $option1 where $option2 like '%$search%' limit $page,$limit";
$result = mysql_query($query, $db);
$pages=intval($numrows/$limit);
$current = ($page/$limit) + 1;
if ($numrows%$limit) {
$pages++;
}
if (($pages < 1) || ($pages == 0)) {$total = 1;}
else {$total = $pages;}
$first = $page+1;
if (!( ( ($page+$limit) / $limit) >= $pages) && $pages!=1) {
$last = $page+$limit;
}
else
{
$last = $numrows;
}
echo "Results <b>$first</b> to <b>$last</b> of <b>$numrows</b><br>";
echo "<br>";
echo "Page <b>$current</b> of <b>$total</b>.<br>";
echo "$numrows matches found for your search of <b>$search</b> in the <b>$option1</b> catalog!";
$result_id = 0;
echo "<TABLE BORDER=0 WIDTH=60% CELLPADDING=0>";
if ($numrows > 0)
{
echo "<TR><TD><b>Item #</b></td>
<TD><b>Description</b></td>
<TD><b>UOM</b></td>
<TD><b>Qty</b></td>
<TD><b>List Price</b></td>
<TD><b>Notes</b></td>
</tr>";
}
while ($data=mysql_fetch_array($result))
{
$id = $data['ID'];
$item = $data['ITEM'];
$desc = $data['DESCRIPTION'];
$uom = $data['UOM'];
$qty = $data['QTY_PER_PCKG'];
$price = $data['LIST_PRICE'];
$notes = $data['NOTES'];
$newArray_color = ($newArray_count%2)?$row_color1:$row_color2;
$result_id++;
echo "<TR><tr bgcolor= $newArray_color ><TD> $item </TD> <TD> $desc </TD> <TD> $uom </TD> <TD> $qty </TD> <TD> $price </TD> <TD> $notes </TD></TR>";
$newArray_count++;
}
if ($page != 0) { // bypass back link if page is 0
$prevpage=$page - $limit;
print "<a href=\"$PHP_SELF?page=$prevpage\">BACK</a> - \n";
}
$groupoffset = 10;
$maxgroups = round($total / $groupoffset);
$pagemax = (($total * $limit)- $limit);
$currentgrp = ($page / $limit);
$currentgrp = ($currentgrp / $groupoffset);
$currentgrp = floor($currentgrp);
$d_maxgroups = 6;
$d_grps = 0;
if ($currentgrp > 2) :
$d_start = $currentgrp - 2;
else :
$d_start = 0;
endif;
if (($d_start + $d_maxgroups) < $maxgroups) :
$d_end = $d_start + $d_maxgroups;
else :
$d_end = $maxgroups;
endif;
$grpstart = ($groupoffset * $d_start) + 1;
$grpend = $grpstart + 9;
$grpno = $d_start;
for ($y = 0; $y <=$maxgroups; $y+=1) :
if (($d_grps >= $d_start) and ($d_grps <= $d_end)) :
if ($grpno == $currentgrp) :
$x = $grpstart;
while ($grpend >= $x) :
// display page hyperlink
$newpage=$limit*($x-1);
if ($newpage == $page)
{
print "<font class=\"small\"><b>$x</b></font> \n";
}else{
if ($newpage <= $pagemax) :
print "<a href=\"$PHP_SELF?page=$newpage\">$x</a> \n";
endif;
}
$x++;
endwhile;
else:
$newpage=($grpstart-1) * $limit;
if ($grpend >= $total) :
if ($grpstart <= $total) :
print " <a href=\"$PHP_SELF?page=$newpage\">$grpstart-$total,</a> \n";
endif;
else :
print " <a href=\"$PHP_SELF?page=$newpage\">$grpstart-$grpend,</a> \n";
endif;
endif;
$grpno+=1;
$grpstart+=$groupoffset;
$grpend = $grpstart + 9;
endif;
$d_grps++;
endfor;
if (!( ( ($page+$limit) / $limit) >= $pages) && $pages!=1) {
$newpage=$page+$limit;
print " - <a href=\"$PHP_SELF?page=$newpage\">NEXT</a></td></tr>";
}
?>
</table>
</BODY>
</HTML>
...i know its alot to look through but any help to get this fixed i very very much appreciated.
NOTE: if i hard code the options and search string in the sql statement everything works great i image since it loops through the beginning of the code again with the search options and the search string and just adds the new limits to start at. Thx again.