【问题标题】:how to send array of values to a query dynamically in codeigniter如何在codeigniter中动态地将值数组发送到查询
【发布时间】:2013-01-24 03:15:04
【问题描述】:

模型中的代码是 公共函数 get_report6_8($type, $filter_id=NULL) { $values = 数组($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。当我从包含所有其他值的下拉列表中选择总计时,就会发生这种情况。

    【问题讨论】:

      标签: codeigniter


      【解决方案1】:

      您的下拉菜单是多选的,因此它将传递一个包含值的数组。

      使用

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

      【讨论】:

      • 谢谢。它有效,但我必须在此之前添加代码以从数据发布数组中捕获数组中的键值。我是这样做的。
      • 公共函数 get_report6_10($type, $data) { $dropdownSelections = array(); foreach ($data['filter'] as $key => $value) { if ($key != ''){ array_push($dropdownSelections, $key); } } $dropdownSelections = implode(",", $dropdownSelections); $选择 = ''; if ( $type == 'natbuild') { $select = 'and natbuild_principal.id in ('.$dropdownSelections.')'; } //回显 $select;
      猜你喜欢
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多