只有从MySQL检索的值列表中的最后一个值存储在codeigniter中的变量中

时间:2015-08-17 12:01:01

标签: php mysql codeigniter

我尝试使用query1从MySQL表中获取值列表,然后在query2中使用它们来获取值。 Query1提供4个值,但Query2给出的输出与Query1的最后一个值匹配。

以下是我的控制器代码

public function example_ctl(){
  $data['result'] = $this->some_model->example();
}

以下是我的型号代码

public function example() {

        $query = "select m.queue_id from agent_queue_mapping_table as m, user_table as u where u.id=m.user_id and m.company_id = ".$this->session->userdata('user_comp_id')." and u.id = ".$this->session->userdata('user_id');

        $res = $this->db->query($query); 

        foreach ($res->result_array() as $value) {
            $queue_ids = implode(',', $value);
        }

        $query_ticket = "select * from tickets_table where company_id = ".$this->session->userdata('user_comp_id')." and ticket_status = 'New' and queue_id IN (".$queue_ids.") ORDER BY id DESC LIMIT 3";
        $res_ticket = $this->db->query($query_ticket);  
        return $res_ticket->result_array();

 }

我无法理解我哪里出错了。请帮忙。

2 个答案:

答案 0 :(得分:0)

尝试将代码更改为此

<?php 
    $queue_id = array();
     foreach ($res->result_array() as $value) {
                $queue_id[] = $value;
            }

            $queue_id = implode(",",$value);

 ?>
每次循环执行时,

$queue_id在循环内被覆盖,这就是为什么你得到最后一个值

答案 1 :(得分:0)

这就是我解决这个问题的方法。

$query = "select m.queue_id from agent_queue_mapping_table as m, user_table as u where u.id=m.user_id and m.company_id = ".$this->session->userdata('user_comp_id')." and u.id = ".$this->session->userdata('user_id');
        $res = $this->db->query($query); 

        foreach ($res->result_array() as $value) {
            $queue_ids[] = $value['queue_id'];
        }

        $queue_id = implode(',', $queue_ids);

        $query_ticket = "select * from tickets_table where company_id = ".$this->session->userdata('user_comp_id')." and ticket_status = 'New' and queue_id IN (".$queue_id.") ORDER BY id DESC LIMIT 3";
        $res_ticket = $this->db->query($query_ticket);  
        return $res_ticket->result_array();