codeigniter multiple where子句不起作用

时间:2016-10-23 22:48:55

标签: database codeigniter

我的控制器(我想根据某些条件选择记录):

public function promotion()
{
    if(!$this->user_permission->check_permission())return;

        $data['startDate'] = $this->input->post('input_date_from');
        $data['endDate'] = $this->input->post('input_date_to');
        $email = $this->input->post('email');

    var_dump($email);
    var_dump($data['startDate']);
    var_dump($data['endDate']);

    $this->db->select('a.booking_id as booking_id, a.from_email as from_email, a.booking_date as booking_date, a.status as status, b.vendor_airway_bill as awb, b.tariff as tariff');
    $this->db->where('from_email', $email);
    $this->db->where('booking_date >=', date('Y-m-d',strtotime($data['startDate'])));
    $this->db->where('booking_date <=', date('Y-m-d',strtotime($data['endDate'])));
    $this->db->where('status = 1');
    $this->db->or_where('status = 2');
    $this->db->from('booking as a');
    $this->db->join('shipment as b','a.booking_id = b.booking_id','left');      
    $query = $this->db->get();
    $data['result'] = $query->result_array();

    $this->template->render('admin/promotion',$data,'admin');

}

我的观点(我想根据用户输入显示基于from_email的预订货件。我将表格设置为display:none,当用户在from_email输入内输入内容时,请点击view,它显示数据表):

<form action="" method="post" id="cashback">
    User Email :
    <input type="text" name="email" id="email">
    Booking Date :
    <div id="daterange" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; display:inline">
     <span></span> <b class="caret"></b>
    </div>
    <input type="hidden" name="input_date_from" id="input_date_from" value="">
    <input type="hidden" name="input_date_to" id="input_date_to" value="">
   <button type="button" class="btn btn-primary" onclick="promotion();">View
   </button>
</form>

<table class="table table-bordered table-striped table-condensed" id="promotion" style="display:none">
        <thead>
            <tr>
                <th>BookingDate</th>
                <th>UserEmail</th>
                <th>AirwayBill</th>
                <th>Tariff</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($result as $r) : ?>
            <tr>
                <td>
                    <?php echo $r['booking_date'];?>
                </td>
                <td>
                    <?php echo $r['from_email'];?>
                </td>
                <td>
                    <?php echo $r['awb'];?>
                </td>
                <td>
                    <?php echo $r['tariff'];?>
                </td>
                <td>
                    <?php echo $r['status'];?>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</template>

<template block="custom_js">

<script type="text/javascript" src="https://cdn.datatables.net/r/bs/dt-1.10.9/datatables.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/2.15.1/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/bootstrap.daterangepicker/2.1.24/daterangepicker.js"></script>
<script src="<?php echo base_url('asset/js/typeahead.bundle.min.js') ?>"></script>
<script type="text/javascript">base_url='<?php echo base_url()?>'</script>
<script>
    <?php if(isset($startDate) && isset($endDate)): ?>
      var startDate = '<?php echo $startDate ?>';
      var endDate   = '<?php echo $endDate ?>';
    <?php endif; ?>

$(function() {
    $('#promotion').DataTable();

    var start = typeof(startDate) === 'string' ? moment(startDate) : moment().startOf('month');
    var end = typeof(endDate) === 'string' ? moment(endDate) : moment();
    $('#input_date_from').val(start.format('YYYY-MM-DD'));
    $('#input_date_to').val(end.format('YYYY-MM-DD'));


    function cb(start, end) {
        $('#daterange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
    }

    $('#daterange').daterangepicker({
        startDate: start,
        endDate: end,
        ranges: {
            'Today': [moment(), moment()],
            'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            'Last 7 Days': [moment().subtract(6, 'days'), moment()],
            'Last 30 Days': [moment().subtract(29, 'days'), moment()],
            'This Month': [moment().startOf('month'), moment().endOf('month')],
            'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        }
    }, cb);

    cb(start, end);

    $('#daterange').on('apply.daterangepicker', function(ev, picker) {
        $('#input_date_from').val(picker.startDate.format('YYYY-MM-DD'));
        $('#input_date_to').val(picker.endDate.format('YYYY-MM-DD'));
    });

});

function promotion() {

    input_date_to = $('#input_date_to').val();
    input_date_from = $('#input_date_from').val();
    email = $('#email').val();

    $.ajax
    ({
        url: "<?php echo site_url('admin/promotion')?>",
        type: "POST",
        dataType: "text",
        data: {email: email, input_date_from: input_date_from, input_date_to: input_date_to},
        success: function(data)
        {
            $('#promotion').show();
            console.log(email);
            console.log(input_date_from);
            console.log(input_date_to);
        }
    })
}

</script>

我尝试根据from_emaildate_range_fromdate_range_to过滤数据。当我var_dump输入帖子时,它会显示我预期的值,但它没有运行this->db->where('from_email',$email),所以我得到了所有结果..

2 个答案:

答案 0 :(得分:0)

尝试替换

$this->db->where('status = 1');
$this->db->or_where('status = 2');

成像:

$this->db->where('(status = 1 OR status = 2)');

注意括号对于使OR运算符获得比AND运算符更高的优先级非常重要。还要打开查询日志并检查将哪些实际SQL发送到数据库服务器以更好地理解原因。

答案 1 :(得分:0)

[解决]。使用where_in()代替or_where