为什么数据没有正确传递codeigniter模型

时间:2015-07-05 09:44:05

标签: php mysql codeigniter

我正在使用codeigniter框架。在那个模型文件中我想得到答案的数量,而不是answer.etc。我的模特就是这样。

     function daily_report() {
     $user = $this->session->userdata['id'];
      $date = $this->input->post('sdate');

      $sql="SELECT count(cl.`C_ansornot`) as bb, u.`E_name` 
          FROM `call` as cl
            inner join `user` as u on u.`idEmployee` = cl.`idEmployee`";


     $query = $this->db->query($sql);
    foreach ($query as $row){
       //  var_dump($query) ;
      $sql1="SELECT
               count(`C_ansornot`) answer 
               FROM `call` 
               WHERE `C_ansornot`= 'answer' and idEmployee=$user and C_date= $date" ;
            $query2 = $this->db->select($sql1);
           // $answer=$this->$query2->result();


            $sql2 = "SELECT
               count(`C_ansornot`) notAnswer
               FROM `call` 
               WHERE `C_ansornot`= 'not answer' and idEmployee=$user and C_date= $date";

        $query3 = $this->db->select($sql2);
      //  $notanswer =  $this->$query3->result() ;

      $sql3="SELECT
               count(`C_ansornot`) Rejected 
               FROM `call` 
               WHERE `C_ansornot`= 'Rejected' idEmployee=$user and C_date= $date";  
               $query4=$this->db->select($sql3);
             //   $rejected = $query4->result() ;


       $sql4="SELECT
               count(`C_ansornot`) NotReachable 
               FROM `call` 
               WHERE `C_ansornot`= 'Not Reachable'idEmployee=$user and C_date= $date";
       $query5=$this->db->select($sql4);
                // $NotReachable= $query5->result() ;

        $sql5="SELECT
               count(`C_ansornot`) SwitchedOff
               FROM `call` 
               WHERE `C_ansornot`= 'Switched off' idEmployee=$user and C_date= $date";
                 $query6=$this->db->select($sql5);
                //  $switched = $query6->result() ;

        $sql6="SELECT
               count(`C_ansornot`) WrongNumber 
               FROM `call` 
               WHERE `C_ansornot`= 'Wrong Number/Invalid Number' idEmployee=$user and C_date= $date";
                $query7=$this->db->select($sql6);
                //  $WrongNumber = $query7->result() ;

         $return_array = array(
          //  'name'=>$row->E_name,
            'answer'=>$query2,
            'notAnswer'=>$query3,
            'NotReachable'=>$query4,
            'Rejected'=>$query5,
            'SwitchedOff'=>$query6,
            'WrongNumber'=>$query7

        );
        var_dump($query2);
         return  $return_array;

         }


} 

模型文件未正确传递数据...  当我检查我的结果是这样的... enter image description here

数据显示如下。

1 个答案:

答案 0 :(得分:0)

我在这里发现了很多问题,但是为了回答你的问题,你之所以看到这一切的原因是因为你没有处理你的查询。您只是将这些SELECT字符串分配给数组。

然而,主要问题是你的代码一般。您的代码不是最佳的,您正在进行不必要的查询。这不仅在视觉上很糟糕,而且这种结构的性能会非常慢。如果可能的话,你应该总是尝试让MYSQL为你工作。我会使用SUMCASE重做您的SQL:

SELECT  
  u.E_name,
  sum(case when cl.C_ansornot = 'answer' and cl.C_date='$date' then 1 else 0 end) answer,
  sum(case when cl.C_ansornot = 'not answer' and cl.C_date='$date' then 1 else 0 end) notAnswer,
  sum(case when cl.C_ansornot = 'Not Reachable' and cl.C_date='$date' then 1 else 0 end) NotReachable,
  sum(case when cl.C_ansornot = 'Rejected' and cl.C_date='$date' then 1 else 0 end) Rejected,
  sum(case when cl.C_ansornot = 'Switched Off' and cl.C_date='$date' then 1 else 0 end) SwitchedOff,
  sum(case when cl.C_ansornot = 'Wrong Number/Invalid Number' and cl.C_date='$date' then 1 else 0 end) WrongNumber,
  count(*) total
FROM `calls` as cl 
JOIN `user` as u on u.idEmployee= cl.idEmployee
WHERE cl.idEmployee = $user_id

然后像这样建立你的模型方法:

function daily_report() {
    $user_id = $this->session->userdata('id');
    $date = $this->input->post('sdate');

    $select = "u.E_name,
               sum(case when cl.C_ansornot = 'answer' and cl.C_date='$date' then 1 else 0 end) answer,
               sum(case when cl.C_ansornot = 'not answer' and cl.C_date='$date' then 1 else 0 end) notAnswer,
               sum(case when cl.C_ansornot = 'Not Reachable' and cl.C_date='$date' then 1 else 0 end) NotReachable,
               sum(case when cl.C_ansornot = 'Rejected' and cl.C_date='$date' then 1 else 0 end) Rejected,
               sum(case when cl.C_ansornot = 'Switched Off' and cl.C_date='$date' then 1 else 0 end) SwitchedOff,
               sum(case when cl.C_ansornot = 'Wrong Number/Invalid Number' and cl.C_date='$date' then 1 else 0 end) WrongNumber,
               count(*) total";

    $this->db->select($select, false);
    $this->db->from("calls cl");
    $this->db->join("user u", "u.idEmployee= cl.idEmployee");
    $this->db->where("cl.idEmployee", $user_id);
    $query = $this->db->get();
    return $query->result();

}