Hi everyone,
First of all I want to wish all a happy and prosperous new year. May 2012 bring you the best.
I have a page which reads from a mysql DB then displays the results. since there could be many results I have implemented pagination from an example I found online. Everything works fine, however, when I perform a search and use the pagination controls, it ignores the search.
I know why, but I don’t know how to fix it as I believe it needs a URL function of some sort. Basically my URL needs to be …/welcome.php?search=416&submit=Search&page=3 after performing the search, but I can’t get &page=3 after the search url only when displaying all records.
Wondering if any good souls out there can help me out with this? Much appreciated.
[php]
<?php include "include/session.php"; include "include/config.php"; ?> <? ////////////////////////////// BEGIN PAGINATION ////////////////////////// $tbl_name="myform"; //your table name // How many adjacent pages should be shown on each side? $adjacents = 3; /* First get total number of rows in data table. If you have a WHERE clause in your query, make sure you mirror it here. */ $query = "SELECT COUNT(id) as num FROM $tbl_name where active='True'"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages[num]; /* Setup vars for query. */ $targetpage = "welcome.php"; //your file name (the name of this file) $limit = 5; //how many items to show per page $page = $_GET['page']; if($page) $start = ($page - 1) * $limit; //first item to display on this page else $start = 0; //if no page var is given, set start to 0 // First check if a form was submitted. // Since this is a search we will use $_GET if (isset($_GET['search'])) { $searchTerms = trim($_GET['search']); $searchTerms = strip_tags($searchTerms); // remove any html/javascript. if (strlen($searchTerms) < 1) { $error[] = "Search terms must be longer than 3 characters."; }else { $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection. } // If there are no errors, lets get the search going. if (count($error) < 1) { $query = "SELECT * FROM $tbl_name WHERE "; // grab the search types. //$types = array(); //$types[] = isset($_GET['first_name'])?"first_name LIKE '%{$searchTermDB}%'":''; //$types[] = isset($_GET['last_name'])?"last_name LIKE '%{$searchTermDB}%'":''; //$types[] = isset($_GET['home_phone'])?"home_phone LIKE '%{$searchTermDB}%'":''; //$types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked) if (count($types) < 1) $types[] = "active='True' and first_name LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked $types[] = "active='True' and last_name LIKE '%{$searchTermDB}%'"; $types[] = "active='True' and home_phone LIKE '%{$searchTermDB}%'"; $andOr = isset($_GET['matchall'])?'AND':'OR'; $query .= implode(" {$andOr} ", $types) . " order by id desc LIMIT $start, $limit"; // $result = mysql_query($searchSQL) or trigger_error("There was an error." . mysql_error() . "
SQL Was: {$searchSQL}"); if (mysql_num_rows($searchResult) < 1) { $error[] = "The search term provided {$searchTerms} yielded no results."; } } }else { $query=" SELECT * FROM $tbl_name where active='true' order by id desc LIMIT $start, $limit"; } $result = mysql_query($query); /* Setup page vars for display. */ if ($page == 0) $page = 1; //if no page var is given, default to 1. $prev = $page - 1; //previous page is page - 1 $next = $page + 1; //next page is page + 1 $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ $pagination = ""; if($lastpage > 1) { $pagination .= "
" . implode("
", $error) . "
":""; echo "
Search For: | "; echo "
", $searchTerms) . "
":""; echo "
"; /////////// Now let us print the table headers //////////////// $bgcolor="#f1f1f1"; echo "
"; echo " | "; echo " | "; echo " | ID | "; echo "Entered [Y-M-D] | "; echo "First Name | "; echo "Last Name | "; echo "Age | "; echo "Home Phone | "; echo "Address | "; echo "City | "; echo "Postal Code | "; echo "
![]() | ";
echo "";
if($row[retrieved] == "")
{ echo "![]() ![]() | ";
echo ""; echo " | $row[id] | "; echo "$row[entered] | "; echo "$row[first_name] | "; echo "$row[last_name] | "; echo "$age | "; echo "$row[home_phone] | "; echo "$row[address] $row[address2] | "; echo "$row[city] | "; echo "$row[postal_code] | "; } echo "
"; ////////////////////////////// End of displaying the table with records //////////////////////// ////////////////////////// BEGIN PAGINATION //////////////////////////// /****** build the pagination links ******/ ?> <?=$pagination?> <?php ////////////////////////// END PAGINATION //////////////////// echo "
"; echo ""; echo " |