Page 1 of 1

Search Pagination

Posted: Fri Apr 03, 2009 11:08 am
by eschuppe
I've been trying to write a php script that paginations the search results. It works and displays the results however, when I click on the next page it resets the forum. The example can be seen here: http://www.ericschuppe.com/searchtest2.php ( a valid search for users would be Admin or eric).

Code: Select all

<link href="styles.css" rel="stylesheet" type="text/css">
<form action="searchtest2.php" method="post">
Search - <input type="text" name="search" value="<?php if($_POST['search']) echo $_POST['search']; ?>" /><br />
<input type="submit" name="searchbtn" value="Search" />
</form>
<?php
 
 
include ('dbc.php'); // Connect to the data base.
 if($_POST['searchbtn']) {
        echo '<br /><br />';
        if(!get_magic_quotes_gpc()) {
            $search = addslashes($_POST['search']);
        } else {
            $search = $_POST['search'];
        }
        $query = "SELECT full_name,user_idnum,DATE_FORMAT(`date`, '%m/%d/%Y %I:%i %p'),book,returned, MATCH (full_name) AGAINST ('".$search."' IN BOOLEAN MODE) AS full_name FROM `checkout` WHERE MATCH (full_name) AGAINST ('".$search."' IN BOOLEAN MODE) ORDER BY `full_name` DESC";
        $result = mysql_query($query);
        $num = mysql_num_rows($result);
    }
 
 
 
 
$max = 10; //amount of articles per page. change to what to want
 
$p = $_GET['p'];
 
if(empty($p))
 
{
 
$p = 1;
 
}
 
$limits = ($p - 1) * $max;
 
//view the news article!
 
if(isset($_GET['act']) && $_GET['act'] == "view")
 
{
 
$id = $_GET['id'];
 
$sql = mysql_query("SELECT * FROM checkout WHERE id = '$id'");
 
while($r = mysql_fetch_array($sql))
 
{
 
$full_name = $r['full_name'];
 
$user_idnum = $r['user_idnum'];
 
$date = $r['date'];
 
$book = $r['book'];
 
$returned = $r['returned'];
 
echo "<div><p>$full_name</p><p>$user_idnum</p><p>$date</p><p>$book</p><p>$returned</p></div>";
 
}
 
 
 
}else{
 
 
 
//view all the news articles in rows
 
$sql = $result;
 
//the total rows in the table
 
$totalres = ($num);
 
//the total number of pages (calculated result), math stuff...
 
$totalpages = ceil($totalres / $max);
 
//the table
 
echo '<table align="center" cellspacing="2" cellpadding="2"><tr><td align="left"><b>Name</b></td><td align="center"><b>ID</b></td><td align="center"><b>Date</b></td><td align="center"><b>Book</b></td><td align="center"><b>Returned</b></td></tr><tr>';
 
   while ($r = mysql_fetch_array($sql, MYSQL_NUM)) {
         echo "<tr><td align=\"left\">" .
         stripslashes($r[0]) . "</td>
         <td align=\"left\">$r[1]</td>
         <td align=\"left\">$r[2]</td>
         <td align=\"left\">$r[3]</td>
         <td align=\"left\">$r[4]</td><tr>\n";
 
  }
 
}
 
//close up the table
 
echo "</tr></table>";
 
echo( "<div style='text-align: center;'>" );
 
for($i = 1; $i <= $totalpages; $i++){
 
//this is the pagination link
 
$counter = "<a href='searchtest2.php?p=$i'>$i</a>";
echo "<span class=\"csstables\">$counter</span>";
 
}
 
echo( "</div>" );
 
 
?>
I tried adding
if ($_POST['submit'] == 'search') {
at the beginning of the script but that causes it not load anything when I push the search button.
Any ideas on how I could fix this so that you can actually view the results onn the next page instead of resetting the forums?

Re: Search Pagination

Posted: Fri Apr 03, 2009 10:17 pm
by thinsoldier
What I do is save the search criteria in $_SESSION['searchterms']['site_section'];

When loading the next page (searchtest.php?page=2) my logic checks for the search terms in session, if found it re-queries the database
using those terms and adds an offset to the query so that I only select the rows to be shown on the current paginated page (it calculates this using the value of ?page and the total number of records that would have been found if I didn't LIMIT/offset the query).

I've heard some other people do it my way but instead of doing a query to find the total amount of matching records on every page load they just save that total_found value in session as well. This way you don't need 2 queries per page load, only 1. I'm sure people who know a lot about caching stuff probably could find a way to cache the whole result of all found rows.

I could give you a copy of my pagination class (but it's poorly written and is entirely dependent on mysql)
Example of usage:

Code: Select all

 
// beginning of sql
$sql = "SELECT * FROM news WHERE state='public' ";
 
// logic to build the rest of the query
  if(!empty($query))
  { 
    $query_array=explode(',',$query);
    foreach ($query_array as $q)
      {$sql .= " AND (body LIKE '%".$q."%' OR title LIKE '%".$q."%')";}
  }
 
 
$pgn8 = new pagination($sql, 10, "pgn8_news_sql"); // query string, per-page, session var name
$pgn8->appendUrlString('cmd=searchresults&'); // make sure every page number link calls the same command/action
$pgn8->findPageResult();
 
if (!empty($pgn8->foundRecords))
{
  foreach ($pgn8->foundRecords as $details)
  {
    $urlstr = thispage().'?cmd=view&id=%s';
    $urlstr = sprintf($urlstr , $details['id']);
    $linkstr = '<a href="%s">%s</a>';
    $data[1] = convert_date($details['published']);
    $data[2] = sprintf($linkstr, $urlstr, $details['title']);
    $tabledata[] = $data;
  }
 
    echo $pgn8->showingFromTo(); // Showing 40-49 of 200 Found
      include_once('class.ResultsTable.php');
      $x = new ModuleResultsTable($tabledata);
      $x->theads = array('Published', 'Title');
    echo $x; // common markup for table of search results in most areas of site
    echo $pgn8->displayPagination(); // unordered list of page number links
}
My earlier experiments in pagination involved actually saving all the search results data into $_SESSION. That was a bad idea.

Re: Search Pagination

Posted: Sat Apr 04, 2009 1:17 am
by temidayo
Check out this thread:
viewtopic.php?f=1&t=97168