phpsqlzend-frameworkpaginationzend-paginator

Applying pagination to search results using Zend Paginator


I've searched through similar questions but to no avail, so hopefully someone can help.

I'm having an issue paginating the search results on a site I've built using the Zend Framework (1.12). I can retrieve the items and also the pagination is formed but when I click to go to the next page of pagination, all the search results are lost. The code appends ?page=2 e.t.c to the URL but the code then loses the searchitems.

I read in one question about possibly using sessions or Zend_Session in particular but I'm a little lost at present! Relatively new to PHP.

Firstly here is my search form - search-form.php (XHTML)

<?php
// Search Form
?>
<div id="search">
    <h4 style="margin-bottom:20px;">Search</h4>
    <form name="prodSearch" action="listing-search.php" method="post">
        <input type="text" name="searchitems" id="searchitems" />
        <input type="submit" name="search_items" id="search_items" value="Go" />
    </form>
</div>

This is included in my other pages. listing-search.php is then as follows

<?php 
require_once('admin/scripts/library.php');
require_once('admin/scripts/db_definitions.php');
try {
?>
<!-- HTML Doc Type, Head removed e.t.c -->
<div id="listing-col-main">
<div id="all-cars">
  <h4>Search Results</h4>
</div>
<!-- Listings -->
<?php
    $query = $_POST['searchitems'];
    $min_length = 3;
    if (strlen($query) >= $min_length) {
        $query = htmlspecialchars($query);
        $query = mysql_real_escape_string($query);
        $raw = searchItems($dbread, $query);  
        $paginator = Zend_Paginator::factory($raw);
            if (isset($_GET['page'])) {
                $paginator->setCurrentPageNumber($_GET['page']);
            }
            $paginator->setItemCountPerPage(8);
            $paginator->setPageRange(3); 
            $count = $paginator->getTotalItemCount();
            if ($count > 0) {         
              foreach ($paginator as $results) { // repeat all results ?>
<?php 
              $item_id = $results['item_id'];
              $photos = getRelatedPhotos2($dbread, $item_id);
              ?>
<div class="product-listing">
  <div class="product-listing-image">
    <?php foreach ($photos as $photo) { //repeat ?>
    <img src="image_upload/<?php echo $photo['filename']; ?>" width="75" alt="" />
    <?php } ?>
  </div>
  <div class="product-listing-title">
    <h4><?php echo $results['type_name']; ?> - <?php echo $results['item_make'] . ' ' . $results['item_model']; ?> </h4>
  </div>
  <div class="product-listing-details">
    <p>YEAR: <?php echo $results['item_year']; ?> FUEL: <?php echo $results['item_fuel']; ?> TRANS: <?php echo $results['item_transmission']; ?> MILEAGE: <?php echo $results['item_mileage']; ?> </p>
  </div>
  <div class="product-listing-viewmore"><a href="itempage.php?item_id=<?php echo $results['item_id']; ?>">More Info</a></div>
</div>
<?php } ?>
<!-- 8 products per page -->
<div id="product-listing-pagination">
  <?php
  $pages = $paginator->getPages('Elastic');
  if (isset($pages->previous)) {
echo '<a href="' . $_SERVER['PHP_SELF'] . '?page=' . $pages->previous . '">Prev</a>';
  }
  foreach ($pages->pagesInRange as $page) {
if ($page != $pages->current) {
    echo " <a href='" . $_SERVER['PHP_SELF'] . "?page={$page}'>$page</a>";
}   else {
    echo ' ' . $page;
}
   }
   if (isset($pages->next)) {
echo ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $pages->next . '">Next</a>';
   }

  ?>
</div>
<?php } else { ?>
<div id="noresults"> <?php echo "No results found for " . $query; ?> </div>
<?php }
    } ?>
</div>
<div id="col-right">
<?php include("search-usedcars.php"); ?>
<!-- End of Main Search -->
<div id="latest-news">
  <h3>Latest News</h3>
  <?php include("latestnews.php"); ?>
</div>
 </div>
 <div class="clear"></div>
 <div id="footer-bar"> <a href="#">Designed by</a> </div>
 </div>
 </body>
</html>
<?php 
} catch (Exception $e) {
echo $e->getMessage();
}
?>

Solution

  • It is because your query is passed in $_POST, when you click your nav links away you loose the data stored in $_POST and only keep what is in $_GET. Either change your search form method to $_GET and add &searchitems="'.$query.'" to both your nav links, or store the query in $_SESSION, or you can change your nav links to buttons wrapped in forms with method post and include hidden fields for searchitems and page. I would advise changing the search form method to get as it allows the bookmarking of searches and avoids complexity.