douyouzheng2209 2015-12-15 21:14
浏览 31

Codeigniter排序数据

I have a ptoblem with sorting $_post data in codeigniter.

Controller:

public function filter(){
 ...
  $this->model->method($this->request->post);
 ...
}

Model: $keys = $_POST-data

public function($keys) {
   $this->db->select('*');
   $this->db->from('table1 t1');
   $this->db->join('table2 t2', 't2.id = t1.id', 'left');

   if(!empty($keys['value1']){
     $this->db->where('t1.column', $keys['value1']);
   }
   if(!empty($keys['value2']){
     $this->db->where('t1.column', $keys['value2']);
   }

   if($keys['value3'] || $keys['value4'] || $keys['value5']){
       $array = array($keys['value3'], $keys['value4'], $keys['value5']);
       $this->db->where_in('t2.column', $array);
   }

   $query = $this->db->get()->result();
   return $query;
}

table1

id value1 valu2
 1 23      24
 2 12      15

table2

t1_id value (this contain value of value3, value4, value5)
 2     7
 2     19
 2     13
 2     3
 1     7
 1     13
 1     3

It works, but if set a value3..value5, then selects all values, not a priority table1 value, I need to to do value1 and value2 it has been a priority before value3..value5 Sry for my bad english, tnx!

For example: products_table

product_id value1 value2
  1           13   9
  2           13   9
  3           14  10

product_to_atributes

product_id atribute_id
  1         2
  1         3
  1         4
  2         3
  2         4

product_atributes

atribute_id  atribute_name
    2            atr_name
    3            atr_name2
    4            atr_name3

I need select list of product, where value1 = '13' and value2 = '9' and product_to_atributes.atribute_id IN(2,4)

  • 写回答

1条回答 默认 最新

  • dora0817 2015-12-15 21:19
    关注

    you can use this:

    $this->db->or_where_in();
    

    your code should be:

    if($keys['value3'] || $keys['value4'] || $keys['value5']){
           $array = array($keys['value3'], $keys['value4'], $keys['value5']);
           $this->db->or_where_in('t2.column', $array);
       }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测