【问题标题】:how could i get search results to work with the pagination?我怎样才能让搜索结果与分页一起使用?
【发布时间】:2016-07-11 04:23:07
【问题描述】:

嗨,朋友们,所以我得到了这个搜索条件,它可以选择一个值或选择所有选项,但这里的问题是我不知道如何在分页发生时让结果保持不变?

到目前为止,如果单击下一步或任何页面,它将显示空白页面,因为除了页码之外没有任何内容被传递。

这是我的代码

<?php

  $title = clean($_REQUEST['title']);
  $name = clean($_REQUEST['name']);
  $description = clean($_REQUEST['description']);

  $criteria = array();

            if($title !='')
            {
                $criteria[] = "title = '".$title."'";
            }elseif($title =='All')
                criteria[] = "title = ''";
            }

            if($name !='')
            {
                $criteria[] = "name = '".$name."'";
            }elseif($name =='All')
                criteria[] = "name = ''";
            }

            if($description !='')
            {
                $criteria[] = "description = '".$description."'";
            }elseif($description =='All')
                criteria[] = "description = ''";
    }

   try {

    $total = $db->query('SELECT COUNT(*) FROM table WHERE '.implode(' AND ', $criteria).'')->fetchColumn();
    global $per_page;

    $pages = ceil($total / $per_page);

    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
        'options' => array(
            'default'   => 1,
            'min_range' => 1,
        ),
    )));


    $offset = ($page - 1)  * $per_page;
    $start = $offset + 1;
    $end = min(($offset + $per_page), $total);


   $stmt = $db->prepare('SELECT * FROM table WHERE '.implode(' AND ', $criteria).' ORDER BY id DESC LIMIT :per_page OFFSET :offset'); 

    $stmt->bindParam(':per_page', $per_page, PDO::PARAM_INT);
    $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();

    if ($stmt->rowCount() > 0) {
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($stmt);
        foreach ($iterator as $row) {           

                echo $row['id'];
        }

    echo '<div id="pagination">
    <div id="pagiCount">';
        $prevlink = ($page > 1) ? '<span id="prev"><a href="?page=1" title="First page">First</a></span> <span id="prev"><a href="?page=' . ($page - 1) . '" title="Previous page"><<</a></span>' : '';
        $nextlink = ($page < $pages) ? '<span id="next"><a href="?page=' . ($page + 1) . '" title="Next page">>></a></span> <span id="next"><a href="?page=' . $pages . '" title="Last page">Last</a></span>' : '';
        echo '<div id="paging"><p><small>', $prevlink, ' Page ', $page, ' of ', $pages, '', $nextlink, '</small></p></div>';        
    echo '</div></div>';
    } else {
        echo '<p>Nothing found.</p>';
    }
} catch (Exception $e) {
    echo '<p>Nothing found.</p>';
}
?>

如果您能在这方面提供帮助,我真的会非常感激。干杯

【问题讨论】:

  • 您应该首先将 GET 变量传递给 prev 和 next 链接。现在您只传递页码,而缺少描述、标题和名称变量。
  • @JethroVanThuyne 你的意思是这样吗?

标签: php mysql search pdo pagination


【解决方案1】:

您必须使用除页面之外的相同参数重新生成查询字符串。

所以我认为这样的事情会起作用:

//regenerate query string with new page 
$cur_params = $_GET;
unset($cur_params["page"]);
$cur_url = strtok($_SERVER["REQUEST_URI"],"?") . "?" . http_build_query($cur_params, "", "&");
$page_back_url = $cur_url . (empty($cur_params) ? "" : "&") . "page=" . ($page - 1);
$page_next_url = $cur_url . (empty($cur_params) ? "" : "&") . "page=" . ($page + 1);
$page_last_url = $cur_url . (empty($cur_params) ? "" : "&") . "page=$pages";    

【讨论】:

  • 兄弟,但是下一个第一页和最后一页选项会发生什么?
  • 将我的链接附加到这些链接的锚点。
猜你喜欢
  • 2020-02-03
  • 2020-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多