1

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

我在使用 Zend Framework (1.12) 构建的网站上对搜索结果进行分页时遇到问题。我可以检索项目并且分页已形成,但是当我单击转到下一页分页时,所有搜索结果都丢失了。该代码将 ?page=2 等附加到 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();
}
?>
4

1 回答 1

0

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

于 2013-03-27T21:04:55.490 回答