我怎么能得到搜索结果与分页一起工作?

时间:2016-07-10 19:38:08

标签: php mysql search pdo pagination

嗨朋友们,所以我得到了这个搜索标准的东西,它可以选择一个值或者选择所有但是任何方式这里的问题是我没有想知道如何在分页时让结果保持不变?

截至目前,如果单击下一个或任何页面,它将显示空白页面,因为除了页码之外没有其他任何内容通过。

这是我的代码

<?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>';
}
?>

我真的非常感谢你对此的友好帮助。欢呼声

1 个答案:

答案 0 :(得分:0)

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

所以我认为类似的东西会奏效:

//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";