0
votes

Am new to php programming and I have written my php code to search data from database then displays it with pagination links for easy navigation in the event that the search result will be too many. But the pagination links will not show because of the above warning. The query is not producing the Count result to make my $limit variable true thereby displaying the first page only. This is the code below, Please can you help me correct the error so that the pagination links show.

<?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);
?>
1
it is producing this error -->> Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\HTML\searchresult.php on line 17Christopher Nyandoro

1 Answers

0
votes

$dbconnect is undefined in your script. Also rewrite your query:

$sql = "SELECT COUNT(stockID) FROM stock WHERE search LIKE ". $condition ." GROUP BY stockID"; I have added a space just before "Group By". Hope this will help.