php mysqli编写语句搜索结果分页,链接到下一页显示为空

时间:2013-08-24 07:40:56

标签: php mysqli pagination

解决..基本上由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 />";
    }
}
?>

2 个答案:

答案 0 :(得分:-1)

  1. 学习MVC
  2. 您只将pagenumber传递给分页控件,而不是searchquery
  3. 不要使用$_SERVER['PHP_SELF']
  4. 使用正确的转义方法或验证器代替那些preg_replaces来过滤输入

答案 1 :(得分:-1)

从网址传递'searchquery'变量并从脚本中取回