2015-06-09 3 views
1

Я пытаюсь разработать функцию поиска в php с использованием рамки codeigniter. Он должен иметь возможность искать, используя время и зал. он работает, когда я использую только время. Но он становится очень неправильным, когда я использую зал. После этого я привязываю все коды, которые я использую.codeigniter функция множественного поиска

Я разделил модель на две секции, такие как модель и сервис. в модели есть геттеры и сеттеры. В классе сервиса я пишу только запросы. Любой может дать мне положительный ответ, который будет очень полезен. Заранее спасибо.

Модель (reservation_service.php)

function search_reservations($time,$hall) { 
    $this->db->select('reservations.*'); 
    $this->db->from('reservations');   
    $this->db->like('time', $time); 
    $this->db->or_like('hall',$hall); 
    $this->db->where('is_deleted', '0'); 
    $query = $this->db->get(); 
    return $query->result(); 
} 

Контроллер (dashboard.php)

function search_results() { 
    $reservation_service = new Reservation_service(); 
    $time_booking = trim($this->input->post('time', TRUE)); 
    $hall_booking = trim($this->input->post('hall', TRUE)); 
    $data['search_results'] = $reservation_service->search_reservations($time_booking,$hall_booking); 
    $partials = array('content' => 'dashboard/search_results'); 
    $this->template->load('template/main_template', $partials, $data); 
} 

function search_reservations() { 
    $reservation_service = new Reservation_service(); 
    $data['search_results'] = $reservation_service->search_reservations(trim($this->input->post('time', TRUE))); 
    $partials = array('content' => 'dashboard/reservation_report'); 
    $this->template->load('template/main_template', $partials, $data); 
} 

вид

reservation_report.p л.с.

<div class="form-group"><br> 
    <input id="time" name="time" type="text" placeholder="Time Slot"> 
    <input id="hall" name="hall" type="text" placeholder="Hall"> 
    <button class="btn btn-primary" onclick="search()" >Search</button> 
</div> 
<div id="search_results"> 
    <?php echo $this->load->view('dashboard/search_results'); ?> 
</div> 

<script type="text/javascript"> 
    function search() { 
     var time = $('#time').val(); 

     $.ajax({ 
      type: "POST", 
      url: '<?php echo site_url(); ?>/dashboard/search_results', 
      data: "time=" + time, 
      success: function(msg) { 

       $('#search_results').html(msg); 

      } 
     }); 
    } 
</script> 

search_results.php

<div class="adv-table">      
    <table class="display table table-bordered table-striped" id="bookings_table"> 
     <thead> 
      <tr> 
       <th>#</th> 
       <th>Date</th> 
       <th>Hall</th> 
       <th>Time</th>      
      </tr> 
     </thead> 
     <tbody> 
      <?php 
      $i = 0; 
      foreach ($search_results as $result) { 
       ?> 
       <tr id="bookings_<?php echo $result->id; ?>"> 
        <td><?php echo ++$i; ?></td> 
        <td><?php echo $result->date; ?></td> 
        <td><?php echo $result->hall; ?></td> 
        <td><?php echo $result->time; ?></td> 
       <?php } ?> 
     </tbody> 
    </table> 
</div> 
+0

чем проблема, пожалуйста, будьте конкретны. –

+0

, какую версию кода вы используете? используйте один раз or_where вместо or_like. – Deep

ответ

0

Проблема с АЯКС функции здесь вы не посылаете значение hall variable в должности

<script type="text/javascript"> 
    function search() { 
     var time = $('#time').val(); 
     var hall = $('#hall').val();// add this to your code 

     $.ajax({ 
      type: "POST", 
      url: '<?php echo site_url(); ?>/dashboard/search_results', 
      data: {time:time,hall:hall},//pass parameter 
      success: function(msg) { 

       $('#search_results').html(msg); 

      } 
     }); 
    } 
</script> 
+0

Большое вам спасибо. Я попробую это. –

0

Ваш код кажется неправильным при загрузке модели в dashboard.php:

$reservation_service = new Reservation_service(); 

Попробуйте загрузить объект модели, как описано в CodeIgniter Documentations:

$this->load->model('reservation_service'); 

поэтому ваш dashboard.php должно быть так:

function search_results() { 
    $this->load->model('reservation_service'); 
    $time_booking = trim($this->input->post('time', TRUE)); 
    $hall_booking = trim($this->input->post('hall', TRUE)); 
    $data['search_results'] = $this->reservation_service->search_reservations($time_booking,$hall_booking); 
    $partials = array('content' => 'dashboard/search_results'); 
    $this->template->load('template/main_template', $partials, $data); 
} 

function search_reservations() { 
    $this->load->model('reservation_service'); 
    $data['search_results'] = $this->reservation_service->search_reservations(trim($this->input->post('time', TRUE))); 
    $partials = array('content' => 'dashboard/reservation_report'); 
    $this->template->load('template/main_template', $partials, $data); 
} 
+0

Большое вам спасибо. Это может мне помочь. –

+0

@ IshaniPathinayake Мое удовольствие, я надеюсь. –