I have successfully coded search page that builds an sql string dynamically based on what items you chose/leave out. But now I've hit a snag again and haven't had any luck trying to fix it on my own in the past 36 hours. I've searched around for paging tutorials for sql results and found a couple, pretty much all giving the same instruction. I tried to follow it, but I think the reason it's not working is because the paging doesn't support dynamic queries? Here's an example of one of the paging tutorials I've had little success. And here's my attempt to use it...though I know it's incorrect. Just not how:
<?php
// CONNECT BEGIN
mysql_connect("localhost","thoerr","*****");
mysql_select_db("thoerr_nuke1");
// CONNECT END
$rowsPerPage = 10; // RESULTS PER PAGE
$pageNum = 1; // START ON PAGE 1
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
$offset = ($pageNum - 1) * $rowsPerPage;
// SEARCH QUERY BEGIN
$search1=$_POST["search1"];
$search2=$_POST["search2"];
$search3=$_POST["search3"];
$sql = "select * from Master_Table where 1";
if(!empty($search1))
{
$sql .= " and (Common_Name LIKE '%". $search1."%' || Botanical_Name LIKE '%". $search1."%')";
}
if(!empty($search2))
{
$sql .= " and Light_Conditions LIKE '%". $search2."%'";
}
if(!empty($search3))
{
$sql .= " and Flower_Color LIKE '%". $search3."%' ";
}
$sql .= " LIMIT $offset, $rowsPerPage";
$result = mysql_query("$sql") or die('Error, query failed');
// SEARCH QUERY END
// DISPLAY RESULTS BEGIN
while($r=mysql_fetch_array($result))
{
$Botanical_Name=$r["Botanical_Name"];
$Common_Name=$r["Common_Name"];
$Light_Conditions=$r["Light_Conditions"];
$Flower_Color=$r["Flower_Color"];
echo "<b>Common Name:</b> $Common_Name <br> <b>Botanical Name:</b> $Botanical_Name <br> <b>Light Conditions:</b> $Light_Conditions <br><b>Flower Color:</b> $Flower_Color <br><br>";
}
$anymatches=mysql_num_rows($result);
if ($anymatches == 0)
{
echo "No Matches";
}
// DISPLAY RESULTS END
echo "$sql"; // FINAL SEARCH QUERY
echo "<br><br>";
// PAGINATION BEGIN
$query2 = "SELECT COUNT(*) AS numrows FROM Master_Table";
$result2 = mysql_query($query2) or die('Error, query failed');
$row = mysql_fetch_array($result2, MYSQL_ASSOC);
$numrows = $row['numrows'];
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"javascript:ajaxpage('$self?page=$page', 'content');\">[Prev]</a> ";
$first = " <a href=\"javascript:ajaxpage('$self?page=1', 'content');\">[First Page]</a> ";
}
else // PAGE 1 DISABLE LINKS
{
$prev = ' [Prev] ';
$first = ' [First Page] ';
}
if ($pageNum < $maxPage) // EVERYTHING IN BETWEEN
{
$page = $pageNum + 1;
$next = " <a href=\"javascript:ajaxpage('$self?page=$page', 'content');\">[Next]</a> ";
$last = " <a href=\"javascript:ajaxpage('$self?page=$maxPage', 'content');\">[Last Page]</a> ";
}
else // LAST PAGE DISABLE LINKS
{
$next = ' [Next] ';
$last = ' [Last Page] ';
}
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last; // PAGE NAV LINK
// PAGINATION END
?>
I'm open to anything... a different tutorial, suggestions to fix code- I appreciate the help! Thanks.