如何在codeigniter中动态地向查询发送值数组

时间:2013-01-24 03:15:05

标签: codeigniter

模型中的代码是 public function get_report6_8($ type,$ filter_id = NULL){     $ values = array($ filter_id);

$select = '';       
if ($filter_id && $type == 'jh') {
    $select = 'and natbuild_rep.jh_rep_id = ?';
}
else if ($filter_id && $type == 'natbuild') {
    $select = 'and natbuild_principal.id = ?';
}

$sql = "select sum(total) total
    from (
        select value_of_sale total
        from lead
        left join natbuild_rep
        on natbuild_rep.mobile = lead.mobile
        left join natbuild_store
        on natbuild_store.id = natbuild_rep.store_id
        left join natbuild_principal
        on natbuild_principal.store_group = ifnull(natbuild_store.store_group2, natbuild_store.store_group)
        where (status = 1 OR status = 2)
        and value_of_sale is not null
        {$select}
        group by lead.id
    ) temp";

return $this->db->query($sql, $values)->row();
}

控制器中的代码是 $ data ['report8'] = $ this-> lead_report_model-> get_report6_8($ type,$ this-> input-> post('filter'));

视图中的代码是

  • 总计:$ total))? $ report8->总计:0;?>
  • 如果我运行像

    这样的查询
    select sum(total) total
    
    from (
    
        select value_of_sale total
        from lead
        left join natbuild_rep
        on natbuild_rep.mobile = lead.mobile
        left join natbuild_store
        on natbuild_store.id = natbuild_rep.store_id
        left join natbuild_principal
        on natbuild_principal.store_group = ifnull(natbuild_store.store_group2, natbuild_store.store_group)
    where (status = 1 OR status = 2)
        and value_of_sale is not null
        and natbuild_principal.id in (18, 30, 31, 35, 33, 25, 23, 15, 8, 6, 5, 29, 7, 3, 2, 1, 24, 27, 22, 21, 20, 26, 36)
    group by lead.id ) temp
    

    结果是对的。请帮助我如何向$ select发送值数组。当我从包含所有其他值的下拉列表中选择总计时,会发生这种情况。

    1 个答案:

    答案 0 :(得分:0)

    你的下拉列表是多选的,所以它将传递一个带有值的数组。

    使用

     implode(",", $dropdownSelections);
    
     $select = 'and natbuild_principal.id in ('.implode(",", $dropdownSelections).')';