codeigniter多重搜索功能

时间:2015-06-09 11:05:26

标签: php html codeigniter

我正在尝试使用codeigniter框架在php中开发搜索功能。它应该能够使用时间和大厅进行搜索。当我只使用时间时,它会起作用。但是当我使用hall时,它会变得非常错误。接下来我已经附上了我正在使用的所有代码。

我将模型划分为两个部分,如模型和服务。在模型中有吸气剂和设定者。在服务类中,我只是在写查询。如果有人能给我一个非常有帮助的肯定答案。提前谢谢。

模型(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.php

<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>

2 个答案:

答案 0 :(得分:0)

问题在于ajax函数,你没有在帖子

中发送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>

答案 1 :(得分: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);
}