2014-03-28 2 views
2

일부 사용자 지정 값에 의해 데이터베이스에서 정보를 얻기위한 검색 양식이 있습니다. 텍스트 는 데이터베이스 쿼리에서 텍스트 값을 허용하지 않지만 숫자 만 허용합니다.

  • category id : int로
  • price : 쿼리를 할 때, 이제
  • 을 int로 는 exampe를 들어, 내 양식은

    • search keyword 검색 키워드 및 카테고리 ID 및 가격을 포함 데이터베이스에서 일부 데이터를 가져 오려면 search keyword과 같은 문자를 포함하는 값을 허용하지 않지만 category id, price과 같은 숫자 만 허용합니다.

      템플릿 페이지의 HTML

      <form action="<?= base_url() ?>classifieds/search/result/" method="get"> 
            <input type="text" name="search_keyword" id="search_keyword" /> 
            <span id="price_fromto"> 
             <input type="text" name="price_from" id="price_from" /> 
             <input type="text" name="price_to" id="price_to" /> 
            </span> 
            <select name="currency" id="currency"> 
             <option value="">currency</option> 
               <option value=""></option> 
            </select> 
            <select name="service" id="service"> 
             <option value="">Offer type</option> 
             <option value="wanted"></option> 
             <option value="sale">for sale</option> 
            </select> 
            <input type="submit" name="sbmtSearch" id="sbmtSearch" value="search" /> 
           </form> 
      

      컨트롤러 코드

      $this - > load - > model('classifieds'); // load model 
      $q_s = array(
          'name' = > $this -> input -> get('search_keyword', true), 
          'category_id' = > $this -> input -> get('search_category', true), 
          'type' = > $this - > input -> get('type', true), 
          'price >' = > $this -> input -> get('price_from', true), 
          'price <' = > $this -> input -> get('price_to', true),); 
      
      // configartion 
      $output['query'] = $this->classifieds->get_serach_classifieds($q_s); // get content in this model 
      } 
      $data['content'] = $this -> load -> view('category', $output, true); // append model content in content variable 
      $data['title'] = 'search result'; // model model name the page title 
      $this -> load -> view('index', $data); // load master page (Container) 
      

      분류한다 모델

      function get_serach_classifieds($q_s) { 
           $this->db->where('state', 1); 
      
           foreach ($q_s as $key => $val) { 
            if ($val != "" && $val != 0) { 
             $this->db->where($key, $val); 
            } 
           } 
      
           return $this->db->get('content'); 
          } 
      

      SELECT * FROM (`content`) WHERE `state` = 1 AND `category_id` = '4' AND `price` > '100' AND `price` < '800' LIMIT 15 
      

      SELECT * FROM (`content`) WHERE `state` = 1 AND 'name' = 'bmw' AND 'type' = 'wanted' AND `category_id` = '4' AND `price` > '100' AND `price` < '800' LIMIT 15 
      

      텍스트를 사용하지 않는 쿼리를 만들어 내 코드에 문제가 무엇입니까 (올바른) 이런 식으로 될 수있는 쿼리를 세웠 죠 (잘못을) 생성 쿼리 가치?

    +0

    당신은 얻을 통해 데이터를 받고 있습니까? get_serach_classifieds 함수에서'var_dump ($ q_s)'를 확인할 수 있습니까? – Rikesh

    +0

    네, 모든 것이 괜찮습니다. 모든 것을 테스트했습니다. 데이터베이스에서 데이터를 가져올 때의 문제점. –

    답변

    1

    $val != 0이 문제 인 것 같습니다. 문자열과 비교할 때 사실이 아닙니다. 이 같은

    뭔가 작업을해야합니다 :

    foreach ($q_s as $key => $val) { 
         if ((is_string($val) && $val != "") || (is_int($val) && $val !=0)) { 
          $this->db->where($key, $val); 
         } 
        } 
    
    관련 문제