【问题标题】:codeigniter multiple search functioncodeigniter 多重搜索功能
【发布时间】:2015-06-09 11:05:26
【问题描述】:

我正在尝试使用 codeigniter 框架在 php 中开发搜索功能。它应该也可以使用时间和大厅进行搜索。当我只使用时间时它可以工作。但是当我使用大厅时它会变得非常错误。下面我附上了我正在使用的所有代码。

我将模型分为模型和服务两部分。在模型中有 getter 和 setter。在服务类中,我只写查询。如果有人能给我一个肯定的答案,那将非常有帮助。提前致谢。

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

【问题讨论】:

  • 什么问题,请具体说明。
  • 您使用的是哪个 codeigniter 版本?使用一次 or_where 而不是 or_like。

标签: php html codeigniter


【解决方案1】:

问题在于 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>

【讨论】:

  • 非常感谢。我会试试的。
【解决方案2】:

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);
}

【讨论】:

  • 非常感谢。这可能对我有帮助。
  • @IshaniPathinayake 很高兴,我希望如此。
猜你喜欢
  • 1970-01-01
  • 2020-07-18
  • 2022-01-02
  • 1970-01-01
  • 2018-07-25
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多