Hello all,
I have this script that works for the most part, it has one issue that I don't know how to fix
it is when a person click the links page number and it should give them the next screen full of results, well it does for number 1 and if you click 2 it works but as soon as you hit 3 it returns you back to 1, it also does strange things like if you was to type it into the address bar like: index.php?sq=dog&page=3 this appears not to work it shows the same information as page 1
What am I doing wrong with this?
<?php
if(empty($_GET['sq'])) // if there is no ?q=... whatever then this displays
{
include '/home/dev/www/search/search_form.php';
}
else // if there is ?q=... whatever then this displays
{
include '/home/dev/www/search/search_form.php';
$search_query = $_GET['sq'];
$perpage = 20; if(isset($_GET["perpage"])) { $perpage =$_GET["perpage"];}
$pagenum = 1; if(isset($_GET["page"])) { $pagenum =$_GET["page"];}
include '/home/dev/www/search/lib/db_config_ro-dev.php';
include '/home/dev/www/search/lib/db_conn-select.php';
$offset = ($pagenum - 1) * $perpage;
$field1 = "PNO";
$field2 = "SN_SNL";
$query = " SELECT * FROM query WHERE $field1 LIKE '%$search_query%' OR $field2 LIKE '%$search_query%' LIMIT $offset, $perpage";
$result = mysql_query($query) OR die("Was unable to get the results!" . mysql_error());
$rows = mysql_num_rows($result) OR die("Was unable to get number of rows" . mysql_error());
$num = mysql_affected_rows();
// how many rows we have in database
$query = " SELECT * FROM query WHERE $field1 LIKE '%$search_query%' OR $field2 LIKE '%$search_query%'";
$result = mysql_query($query) OR die("Was unable to get the results!" . mysql_error());
$numrows = mysql_num_rows($result) OR die("Was unable to get number of rows" . mysql_error());
// $num = mysql_affected_rows();
// how many pages we have when using paging?
$maxPage = ceil($numrows/$perpage);
// print the link to access each page
$self = $_SERVER['HTTP_REFERER'];
$page_num_nav = "";
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pagenum)
{
$page_num_nav .= " $page "; // no need to create a link to current page
}
else
{
$page_num_nav .= " <a href=\"$self?page=$page&sq=$search_query\">$page</a> ";
}
}
if ($pagenum > 1)
{
$page = $pagenum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ""; // we're on page one, don't print previous link
$first = ""; // nor the first page link
}
if ($pagenum < $maxPage)
{
$page = $pagenum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ""; // we're on the last page, don't print next link
$last = ""; // nor the last page link
}
$result_bar = $first . $prev . $page_num_nav . $next . $last;
echo " Your search for ". $search_query ." resulted in : ". $rows ."<br /><br />";
echo "<center>$result_bar</center>";
$i=0;
while ($i < $rows) {
$ein=mysql_result($result,$i,"EIN");
$pno=mysql_result($result,$i,"PNO");
$state=mysql_result($result,$i,"STATE");
$ntee_code=mysql_result($result,$i,"NTEE_Code");
$activity_code=mysql_result($result,$i,"Activity_Code");
$sn=mysql_result($result,$i,"SN_SNL");
$rn = $i + 1;
echo "Record #: ".$rn.": ". $pno ." -- ".$state." -- ".$ein."<br />";
echo $ntee_code ." -- ". $activity_code ." -- ". $sn ."<br /><br />";
$i++;
}
echo "<center>$result_nav</center>";
include '/home/dev/www/lib/db_close.php';
} // close the else statement for displaying the search box with results
?>
Thanks ahead of time for any and all help!
Sincerely,
Christopher