未定义的变量:使用查询

时间:2015-03-26 21:52:20

标签: php mysql search mysqli filter

我收到以下错误:  未定义的变量:以下行的get_crs_mysqli:

$get_crs_mysqli .= (!empty($_SESSION['userSearch']))?(" AND course_title like '%".$_SESSION['userSearch']."%' ") : '';  

基本上它是如何工作的如下:

实质上发生的是用户搜索课程,然后当他们应用可选的搜索过滤器时,它会被修改以反映过滤器。例如,他们搜索所有html,并加载所有html,如果他们选择持续时间1,它应该只显示1个持续时间的html课程。

特别是,这些过滤器是可选的。当页面加载时,它会读取用户输入的搜索查询,因此填充的结果基于其搜索。然后,当用户选择类别category1或category2的复选框时,它会进入细节。例。填充html的课程,然后用户检查duration1,如果用户检查duration1和2,则显示仅具有duration1的html的所有课程,然后显示持续时间为1和2的html课程,因此结果数量缩小。在开始时它没有考虑过滤器,因此结果更大。

下面的图片可以帮助您将其可视化:

enter image description here

在这个社区的帮助下,我得到了这段代码:

$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_query($con, $get_crs_mysqli);

如果您需要任何问题,请告诉我

更新

完整代码;

<?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_count = " SELECT * 
    FROM courses 
    WHERE course_date1 >= CURRENT_DATE() 
       AND ( 
          (course_title like '%" . $_SESSION['userSearch'] . "%') 
          AND (
             (course_duration IN ('" . $duration1 . "','" . $duration2 . "','" . $duration3 . "','" . $duration4 . "','" . $duration5 . "') ) 
          OR (course_delivery2 = '" . $delivery2 . "' ) 
          OR (course_guarantee IN ('" . $guarantee1 . "','" . $guarantee2 . "' ) )
          OR (course_city = '" . $city . "')
          OR (course_subc1 = '" . $category1 . "')
          OR (course_subc2 = '" . $category2 . "')
          OR (course_subc3 = '" . $category3 . "')
          OR (course_subc4 = '" . $category4 . "')
          OR (course_subc5 = '" . $category5 . "')
          OR (course_delivery2 = '" . $delivery2 . "')
          OR (course_delivery = '" . $delivery1 . "' )
          )
          OR (course_duration = '" . $duration1 . "')
        ) 
    ORDER BY course_date1 ASC ";

$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_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>';





?>

0 个答案:

没有答案