Essentially how it works is as follow:
What essentially happen is that a user search for a course, and then when they apply the optional search filter, it gets revised as to reflect the filter. For instance, they search all html, and it loads all html, and if they select duration 1 than it should only show html courses with 1 duration.
In particular, those filters are optional. when the page loads it reads the search query the user has entered and hence the result populated is based on its search. then when the user select a checkbox like category1 or category2 it goes into specifics. example. courses for html are populated, then user checks duration1 so all courses for html with only duration1 is shown if user checks duration1 and 2 then both html courses with duration1 and 2 is shown , and hence number of results shrink. at the start it doesnt take into account the filters, and hence results is larger.
I am receiving the following errors:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\trainingeddesign\fetch_pages.php on line 144
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\trainingeddesign\fetch_pages.php on line 145
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\trainingeddesign\fetch_pages.php on line 149
the results does populate for the search query, the problem is that when i hit a checkbox the above error occurs
Below is the code:
<?php
session_start();
include("includes/db.php");
global $con;
$duration1 = isset($_POST['duration1']) ? $_POST['duration1']: null;
$duration2 = isset($_POST['duration2']) ? $_POST['duration2']: null;
$duration3 = isset($_POST['duration3']) ? $_POST['duration3']: null;
$duration4 = isset($_POST['duration4']) ? $_POST['duration4']: null;
$duration5 = isset($_POST['duration5']) ? $_POST['duration5']: null;
$delivery1 = isset($_POST['delivery1']) ? $_POST['delivery1']: null;
$delivery2 = isset($_POST['delivery2']) ? $_POST['delivery2']: null;
$category1 = isset($_POST['category1']) ? $_POST['category1']: null;
$category2 = isset($_POST['category2']) ? $_POST['category2']: null;
$category3 = isset($_POST['category3']) ? $_POST['category3']: null;
$category4 = isset($_POST['category4']) ? $_POST['category4']: null;
$category5 = isset($_POST['category5']) ? $_POST['category5']: null;
$category6 = isset($_POST['category6']) ? $_POST['category6']: null;
$category7 = isset($_POST['category7']) ? $_POST['category7']: null;
$category8 = isset($_POST['category8']) ? $_POST['category8']: null;
$category9 = isset($_POST['category9']) ? $_POST['category9']: null;
$provider1 = isset($_POST['provider1']) ? $_POST['provider1']: null;
$provider2 = isset($_POST['provider2']) ? $_POST['provider2']: null;
$provider3 = isset($_POST['provider3']) ? $_POST['provider3']: null;
$provider4 = isset($_POST['provider4']) ? $_POST['provider4']: null;
$provider5 = isset($_POST['provider5']) ? $_POST['provider5']: null;
$guarantee1 = isset($_POST['guarantee1']) ? $_POST['guarantee1']: null;
$guarantee2 = isset($_POST['guarantee2']) ? $_POST['guarantee2']: null;
$city = isset($_POST['city1']) ? $_POST['city1']: null;
//sanitize post value
if(isset($_POST["page"])){
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
}else{
$page_number = 1;
}
$item_per_page = 10;
//get current starting point of records
$position = (($page_number-1) * $item_per_page);
global $con;
$p1 = 0;
$p2 = 4000;
$get_crs_mysqli = "
SELECT *
FROM courses
WHERE course_date1 >= CURRENT_DATE() ";
$get_crs_mysqli .= (!empty($_SESSION['userSearch']))?(" AND course_title like '%".$_SESSION['userSearch']."%' ") : '';
$durationArr = array();
if (!empty($duration1)) $durationArr[] = $duration1;
if (!empty($duration2)) $durationArr[] = $duration2;
if (!empty($duration3)) $durationArr[] = $duration3;
if (count($durationArr)>0) {
$get_crs_mysqli .= " AND (course_duration IN ('".implode("','", $durationArr)."') ";
}
$guaranteeArr = array();
if (!empty($guarantee1)) $guaranteeArr[] = $guarantee1;
if (!empty($guarantee2)) $guaranteeArr[] = $guarantee2;
if (count($guaranteeArr)>0) {
$get_crs_mysqli .= " AND (course_guarantee IN ('".implode("','", $guaranteeArr)."') ";
}
$fieldsArr = array();
$fieldsArr['course_delivery2'] = $delivery2;
$fieldsArr['course_city'] = $city;
$fieldsArr['course_subc1'] = $category1;
$fieldsArr['course_subc2'] = $category2;
$fieldsArr['course_subc3'] = $category3;
$fieldsArr['course_subc4'] = $category4;
$fieldsArr['course_subc5'] = $category5;
$fieldsArr['course_delivery2'] = $delivery2;
$fieldsArr['course_delivery'] = $delivery1;
foreach ($fieldsArr as $key=>$val) {
if (!empty($val)) {
$get_crs_mysqli .= " AND $key = $val ";
}
}
$get_crs_mysqli .= "
ORDER BY course_date1 ASC
LIMIT $position, $item_per_page";
$get_crs_mysqli_count = "
SELECT *
FROM courses
WHERE course_date1 >= CURRENT_DATE() ";
$get_crs_mysqli_count .= (!empty($_SESSION['userSearch']))?(" AND course_title like '%".$_SESSION['userSearch']."%' ") : '';
$durationArr = array();
if (!empty($duration1)) $durationArr[] = $duration1;
if (!empty($duration2)) $durationArr[] = $duration2;
if (!empty($duration3)) $durationArr[] = $duration3;
if (count($durationArr)>0) {
$get_crs_mysqli_count .= " AND (course_duration IN ('".implode("','", $durationArr)."') ";
}
$guaranteeArr = array();
if (!empty($guarantee1)) $guaranteeArr[] = $guarantee1;
if (!empty($guarantee2)) $guaranteeArr[] = $guarantee2;
if (count($guaranteeArr)>0) {
$get_crs_mysqli_count .= " AND (course_guarantee IN ('".implode("','", $guaranteeArr)."') ";
}
$fieldsArr = array();
$fieldsArr['course_delivery2'] = $delivery2;
$fieldsArr['course_city'] = $city;
$fieldsArr['course_subc1'] = $category1;
$fieldsArr['course_subc2'] = $category2;
$fieldsArr['course_subc3'] = $category3;
$fieldsArr['course_subc4'] = $category4;
$fieldsArr['course_subc5'] = $category5;
$fieldsArr['course_delivery2'] = $delivery2;
$fieldsArr['course_delivery'] = $delivery1;
foreach ($fieldsArr as $key=>$val) {
if (!empty($val)) {
$get_crs_mysqli_count .= " AND $key = $val ";
}
}
$get_crs_mysqli_count .= "
ORDER BY course_date1 ASC ";
$get_crs=mysqli_query($con, $get_crs_mysqli);
$get_crs_count=mysqli_query($con, $get_crs_mysqli_count);
// Get total of Num rows from the database query
$nr = mysqli_num_rows($get_crs);
$count_rows = mysqli_num_rows($get_crs_count);
echo '<p style="margin-bottom: 8px;margin-top: 8px;font-size:18px;"><b>Number of courses available: </b>' . $count_rows ;
$count_cats = mysqli_num_rows($get_crs);
if ($count_cats==0){
echo "<h2 style='padding:20px;'>No course(s) were found.</h2>";
}
//output results from database
echo '<ul class="page_result">';
while($row_crs = mysqli_fetch_array($get_crs)){
$crs_id = $row_crs['course_id'];
$crs_cat = $row_crs['course_cat'];
$crs_provider = $row_crs['course_provider'];
$crs_title = $row_crs['course_title'];
$crs_price = $row_crs['course_price'];
$crs_city= $row_crs['course_city'];
$crs_category= $row_crs['course_cat1'];
$crs_date= $row_crs['course_date1'];
$crs_sdesc= $row_crs['course_sdesc'];
$crs_shortdesc = mb_strimwidth("$crs_sdesc",0,140,"...");
$crs_image = $row_crs['course_image'];
$provider_image = $row_crs['provider_image'];
echo " <article class='search-result row'><center>
<div class='col-xs-12 col-sm-12 col-md-3' id='thumbnailContainer'>
<a href='#' title='Lorem ipsum' class='thumbnail' id='resultThumbnail'><img src='$provider_image' /></a>
<a href='searchPage.php?crs_price=$crs_price' style='color:black;'> <button id='resultprice'><span id='resultpriceText'>$ $crs_price</span></button></a>
</div>
<div class='col-xs-12 col-sm-12 col-md-2'>
<ul class='meta-search' id='listDesign'>
<a href='searchPage.php?crs_date=$crs_date' style='color:white;'> <li><button id='resultInfo'><i class='fa fa-calendar fa-1x'><span id='iconText'> $crs_date</span></i></button></li></a>
<a href='searchPage.php?crs_category=$crs_category' style='color:white;'> <li><button id='resultInfo2'><i class='fa fa fa-tags fa-1x'><span id='iconText'> $crs_category</span></i></button></li></a>
<a href='searchPage.php?crs_provider=$crs_provider' style='color:white;'><li><button id='resultInfo'><i class='fa fa-graduation-cap fa-1x'><span id='iconText'> $crs_provider</span></i></button></li></a>
<a href='searchPage.php?city=$crs_city' style='color:white;'><li><button id='resultInfo'><i class='fa fa-map-marker fa-1x'><span id='iconText'> $crs_city</span></i></button></li></a>
</ul>
</div></center>
<div class='col-xs-12 col-sm-12 col-md-7 excerpet'>
<h3 id='resultHeading'><a href='coursePage.php?crs_id=$crs_id' id='headingLinking'><b>$crs_title</b></a></h3>
<div id='courseshortDescription'>
$crs_shortdesc
<center><a href='coursePage.php?crs_id=$crs_id' style='color:white;'><button class='btn btn-danger' id='findoutBtn'>Find Out More</button></a> </center>
</div>
<span class='clearfix borda'></span>
</article>";
}
echo '</ul>';
?>
Visualize it
when i echo the statement this is what it gives:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\trainingeddesign\fetch_pages.php on line 144
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\trainingeddesign\fetch_pages.php on line 145
SELECT * FROM courses WHERE course_date1 >= CURRENT_DATE() AND course_title like '%microsoft%' AND (course_duration IN ('1') ORDER BY course_date1 ASC LIMIT 0, 10 SELECT * FROM courses WHERE course_date1 >= CURRENT_DATE() AND course_title like '%microsoft%' AND (course_duration IN ('1') ORDER BY course_date1 ASC
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\trainingeddesign\fetch_pages.php on line 149