簡體   English   中英

具有分頁的Php搜索代碼(使用php)由於警告而不生成導航鏈接。 我該如何糾正錯誤?

[英]Php search code with pagination(Using php) is not producing the navigation links because of a warning. How do i correct the error?

我是PHP編程的新手,我編寫了我的PHP代碼來搜索數據庫中的數據然后用分頁鏈接顯示它,以便在搜索結果太多的情況下輕松導航。 但由於上述警告,分頁鏈接不會顯示。 查詢未生成Count結果以使$ limit變量為true,從而僅顯示第一頁。 這是下面的代碼,請你幫我糾正錯誤,以便分頁鏈接顯示。

<?php
include_once("dbconnect.php");

if(isset($_GET["search"]))  
                 {  
                    $condition = '';  
                     $search_query = explode(" ", $_GET["search"]);  
                      foreach($search_query as $text)  
                      {  
                           $condition .= "search LIKE '%".mysqli_real_escape_string($dbconnect, $text)."%' OR ";  
                      }  


$sql = "SELECT COUNT(stockID) FROM stock WHERE search LIKE ". $condition ."GROUP BY stockID";
$query = mysqli_query($dbconnect, $sql);
$row = mysqli_fetch_row($query);

$rows = $row[0];

$page_rows = 12;

$last = ceil($rows/$page_rows);

if($last < 1){
$last = 1;
}

$pagenum = 1;

if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}

if ($pagenum < 1) { 
$pagenum = 1; 
} else if ($pagenum > $last) { 
$pagenum = $last; 
}

$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
 $condition = ''; 
                      $search_query = explode(" ", $_GET["search"]);  
                      foreach($search_query as $text)  
                      {  
                           $condition .= "search LIKE 
'%".mysqli_real_escape_string($dbconnect, $text)."%' OR ";  
                      }  
                      $condition = substr($condition, 0, -4);  
                      $sql= "SELECT * FROM stock WHERE " . $condition." 
ORDER BY stockID ASC $limit";  

$query = mysqli_query($dbconnect, $sql);

$textline1 = "(<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
$paginationCtrls = '';
$search_query = $_GET['search'];
if($last != 1){

if ($pagenum > 1) {
    $previous = $pagenum - 1;
    $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?
pn='.$previous.'&search='.$search_query.'">Previous</a> &nbsp; &nbsp; ';

    for($i = $pagenum-4; $i < $pagenum; $i++){
        if($i > 0){
            $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?
pn='.$i.'">'.$i.'</a> &nbsp; ';
        }
    }
}

$paginationCtrls .= ''.$pagenum.' &nbsp; ';

for($i = $pagenum+1; $i <= $last; $i++){
    $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?
pn='.$i.'&search='.$search_query.'">'.$i.'</a> &nbsp; ';
    if($i >= $pagenum+4){
        break;
    }
}

if ($pagenum != $last) {
    $next = $pagenum + 1;
    $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?
pn='.$next.'&search='.$search_query.'">Next</a> ';
}
}
$list = '';
$lists = '';

while($row = mysqli_fetch_array($query)){
$id = $row["stockID"];
$name = $row["stockName"];
$image = $row["thumbnail"];
$description = $row["description"];
$topline = $row['topline'];

$lists .= '<div class="col-md-3" style="margin-bottom:10px;">    
        <div class="panel panel-info"><a href="index.php?
page=item&stockID='.$id.'&sub_category="> 

    <div class="panel-body">

    <img class="img-responsive" style="height:100px;" 
src="resources/stocks_images/'.$image.'"  />
     </div>
    <div class="panel-footer" style="height:100px;">'.$name.'

    </div></a>
    </div>
    </div>';


                 }
                 }

mysqli_close($dbconnect);
?>

您的腳本中未定義$dbconnect 還要重寫您的查詢:

$sql = "SELECT COUNT(stockID) FROM stock WHERE search LIKE ". $condition ." GROUP BY stockID"; 我在“分組依據”之前添加了一個空格。 希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM