【问题标题】:php mysqli prepared statement search result pagination, link to next page show emptyphp mysqli准备好的语句搜索结果分页,链接到下一页显示为空
【发布时间】:2013-08-25 21:26:46
【问题描述】:

解决了..基本上正如YourCommonSense所暗示的那样,使用get变量来传递搜索查询,所以我在顶部添加了这些代码块并编辑了可链接的url以传递变量。

if (isset($_GET['searchquery'])){
    $searchquery=$_GET['searchquery'];
$stmt = $myConnection->prepare('SELECT COUNT(id) FROM products WHERE product_name LIKE ? OR details LIKE ? OR category LIKE ? OR subcategory LIKE ? OR price LIKE ?');
        $param = "%$searchquery%";
        $stmt->bind_param('sssss', $param, $param, $param, $param, $param);
        $stmt->execute();
        /* store result */
    $result=$stmt->bind_result($id);
    $rows=array();
    while ($stmt->fetch()){
        $rows[]=array(
            'id' => $id
        );
    }
    $rows=$id;
    // This is the number of results we want displayed per page
    $page_rows = 4;
    // This tells us the page number of our last page
    $last = ceil($rows/$page_rows);
    // This makes sure $last cannot be less than 1
    if($last < 1){
        $last = 1;
    }
    // Establish the $pagenum variable
    $pagenum = 1;
    // Get pagenum from URL vars if it is present, else it is = 1
    if(isset($_GET['pn'])){
        $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
    }
    // This makes sure the page number isn't below 1, or more than our $last page
    if ($pagenum < 1) { 
        $pagenum = 1; 
    } else if ($pagenum > $last) { 
        $pagenum = $last; 
    }

    $stmt=$myConnection->prepare('SELECT id,product_name,price FROM products WHERE product_name LIKE ? OR details LIKE ? OR category LIKE ? OR subcategory LIKE ? OR price LIKE ? LIMIT ?,?');
$begin= ($pagenum - 1) * $page_rows;
$end= $page_rows;
$stmt->bind_param('sssssii', $param, $param, $param, $param, $param,$begin,$end);
$stmt->execute();
/* store result */
        $stmt->store_result();
        /* get the row count */
        $count = $stmt->num_rows;
    if ($count >= 1) {
        $stmt->bind_result($id, $product_name, $price);
            $search_output = "<hr />$count results for <strong>$searchquery</strong><hr />";       

$textline1 = "<h4><center>Total of <b>$rows</b> items in this section</center></h4>";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
    /* First we check if we are on page one. If we are then we don't need a link to 
       the previous page or the first page so we do nothing. If we aren't then we
       generate links to the first page, and to the previous page. */
    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="http://www.chenlikonlinestore.com/search.php?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';
        // Render clickable number links that should appear on the left of the target page number
        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
    $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?searchquery='.$searchquery.'&pn='.$i.'">'.$i.'</a> &nbsp; ';
            }
        }
    }
    // Render the target page number, but without it being a link
    $paginationCtrls .= ''.$pagenum.' &nbsp; ';
    // Render clickable number links that should appear on the right of the target page number
    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?searchquery='.$searchquery.'&pn='.$i.'">'.$i.'</a> &nbsp; ';
        if($i >= $pagenum+4){
            break;
        }
    }
    // This does the same as above, only checking if we are on the last page, and then generating the "Next"
    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?searchquery='.$searchquery.'&pn='.$next.'">Next</a> ';
    }
}
            while ($stmt->fetch()) {
                "$id, $product_name, $price";
                $search_output .= "
                <li><div class='product'>
                <a href='product.php?id=$id' class='info'>
                <span class='holder'>
                <img src='inventory_images/$id.jpg' alt='$product_name' />
                <span class='book-name'>$product_name</span>
                </a>
                 <a href='product.php?id=$id' class='buy-btn'>RM<span class='price'>$price</span></a>
                </div>
                </li>

                ";
            }
        }
}

如问题中所述,我尝试对搜索结果进行分页。其他一切正常,但最后一点是当我单击 paginatedCrls 按钮转到下一页或特定页面时,我加载并显示为全新的搜索。我该如何解决这个问题?

<?php
session_start();

$search_output = "";
if (empty($searchquery)) 
{ $search_output = "<hr />Please enter a search term in the above search box <hr />";}

if (isset($_POST['searchquery']) && $_POST['searchquery'] != "") {
    $searchquery = preg_replace('/[^a-zA-Z0-9_ %\[\]\/\.\(\)%&-]/s', '', $_POST['searchquery']);
    if ($_POST['filter1'] == "Products") {
        require_once ("storescripts/connect_to_mysqli.php");
 $stmt = $myConnection->prepare('SELECT COUNT(id) FROM products WHERE product_name LIKE ? OR details LIKE ? OR category LIKE ? OR subcategory LIKE ? OR price LIKE ?');
        $param = "%$searchquery%";
        $stmt->bind_param('sssss', $param, $param, $param, $param, $param);
        $stmt->execute();
        /* store result */
    $result=$stmt->bind_result($id);
    $rows=array();
    while ($stmt->fetch()){
        $rows[]=array(
            'id' => $id
        );
    }
    $rows=$id;
    // This is the number of results we want displayed per page
    $page_rows = 4;
    // This tells us the page number of our last page
    $last = ceil($rows/$page_rows);
    // This makes sure $last cannot be less than 1
    if($last < 1){
        $last = 1;
    }
    // Establish the $pagenum variable
    $pagenum = 1;
    // Get pagenum from URL vars if it is present, else it is = 1
    if(isset($_GET['pn'])){
        $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
    }
    // This makes sure the page number isn't below 1, or more than our $last page
    if ($pagenum < 1) { 
        $pagenum = 1; 
    } else if ($pagenum > $last) { 
        $pagenum = $last; 
    }

    $stmt=$myConnection->prepare('SELECT id,product_name,price FROM products WHERE product_name LIKE ? OR details LIKE ? OR category LIKE ? OR subcategory LIKE ? OR price LIKE ? LIMIT ?,?');
$begin= ($pagenum - 1) * $page_rows;
$end= $page_rows;
$stmt->bind_param('sssssii', $param, $param, $param, $param, $param,$begin,$end);
$stmt->execute();
/* store result */
        $stmt->store_result();
        /* get the row count */
        $count = $stmt->num_rows;
    if ($count >= 1) {
        $stmt->bind_result($id, $product_name, $price);
            $search_output = "<hr />$count results for <strong>$searchquery</strong><hr />";       

$textline1 = "<h4><center>Total of <b>$rows</b> items in this section</center></h4>";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
    /* First we check if we are on page one. If we are then we don't need a link to 
       the previous page or the first page so we do nothing. If we aren't then we
       generate links to the first page, and to the previous page. */
    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="http://www.chenlikonlinestore.com/search.php?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';
        // Render clickable number links that should appear on the left of the target page number
        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
    $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            }
        }
    }
    // Render the target page number, but without it being a link
    $paginationCtrls .= ''.$pagenum.' &nbsp; ';
    // Render clickable number links that should appear on the right of the target page number
    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
        if($i >= $pagenum+4){
            break;
        }
    }
    // This does the same as above, only checking if we are on the last page, and then generating the "Next"
    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
    }
}
            while ($stmt->fetch()) {
                "$id, $product_name, $price";
                $search_output .= "
                <li><div class='product'>
                <a href='product.php?id=$id' class='info'>
                <span class='holder'>
                <img src='inventory_images/$id.jpg' alt='$product_name' />
                <span class='book-name'>$product_name</span>
                </a>
                 <a href='product.php?id=$id' class='buy-btn'>RM<span class='price'>$price</span></a>
                </div>
                </li>

                ";
            }
        } else {
            $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
        }

    } else {
        $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
    }
}
?>

【问题讨论】:

  • 即使不阅读问题正文,我也会告诉您您没有将搜索词传递到下一页
  • 安全问题和杂乱代码的数量太他妈的高了!
  • 如何将搜索词传递到下一页?会话呢?我是真正的初学者
  • 与您传递页码的方式完全相同
  • 我现在突然意识到..fixing

标签: php mysqli pagination


【解决方案1】:

从 url 传递 'searchquery' 变量并从脚本中获取它

【讨论】:

  • 一件有趣的事情。 2 个过于仓促接受来自同一用户的答案...尤其是这个,因为它的质量非常出色
  • 哈哈,我的帐户被阻止向我的第一个帐户提问,因此在新帐户中提问并认为,erm 可能通过接受答案可以恢复此帐户
【解决方案2】:
  1. 学习 MVC
  2. 您只将页码传递给分页控件,而不是搜索查询
  3. 不要使用$_SERVER['PHP_SELF']
  4. 使用正确的转义方法或验证器代替preg_replaces 来过滤输入

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多