使用Zend Paginator将分页应用于搜索结果

时间:2013-03-27 20:36:26

标签: php sql zend-framework pagination zend-paginator

我搜索过类似的问题,但无济于事,所以希望有人可以提供帮助。

我在使用Zend Framework(1.12)构建的网站上对搜索结果进行分页时遇到了问题。我可以检索项目并且也会形成分页但是当我点击进入下一页分页时,所有搜索结果都将丢失。代码将?page = 2 e.t.c附加到URL,但代码丢失了搜索项。

我在一个问题中读到了关于可能使用会话或Zend_Session的问题,但我目前有点迷失了!相对较新的PHP。

首先,这是我的搜索表单 - 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>

这包含在我的其他页面中。 listing-search.php如下

<?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();
}
?>

1 个答案:

答案 0 :(得分:0)

这是因为您的查询是在$ _POST中传递的,当您单击导航链接时,您将丢失存储在$ _POST中的数据并仅保留$ _GET中的内容。将搜索表单方法更改为$ _GET并将&searchitems="'.$query.'"添加到导航链接,或将查询存储在$ _SESSION中,或者您可以将导航链接更改为包含在带有方法发布的表单中的按钮,并包含隐藏字段searchitemspage。我建议更改搜索表单方法,因为它允许搜索书签并避免复杂性。