如何在一行中组合相同id的行数据

时间:2015-12-08 08:00:11

标签: php codeigniter mysqli

我的控制器功能:

function index()
{
    $data['user_clicks'] = $this->common_model->get_userClicks();
    $this->load->view('common/header');
    $this->load->view('content_pages/user_clicks',$data);
    $this->load->view('common/footer'); 
}

我的模特

function get_userClicks()
{
    $this->db->select('agent_id,click_type,click_count');
    $this->db->select_sum('click_count');
    $this->db->from('daily_clicks');
    $this->db->group_by('click_type,agent_id');
    $this->db->order_by('agent_id');
    $q = $this->db->get();
    return $q->result_array();
}

this is i am getting, what i want is to get the click_count of phone click, email click,offline click with respect to their columns and their total clicks is already done

the database looks like this:

这是我得到的,我想要的是获得click_count的手机点击,电子邮件点击,离线点击他们的列,他们的总点击已经完成。 数据库图像是最后一个klindly帮助我完成它。 谢谢

1 个答案:

答案 0 :(得分:0)

试试这个,

$resarr=count($resultarray);
<table>
<tr>
<th>Agent_id</th>
<th>phone_click</th>
<th>email_click</th>
<th>offline_click</th>
<th>total_click</th>
</tr> 
for($i=1; $i<100; $i++){ //agent id
 for($j=1; $j<=$resarr; $j++) //number of records
 { 
   echo "<tr>";
   if($arr['user_clicks'][$j]['agent_id'] == $i)
   {
     if(!empty($arr['user_clicks'][$j]['agent_id']){
       echo "<td">
       echo $arr['user_clicks'][$j]['agent_id'];
       echo "</td>";  
     }

     if($arr['user_clicks'][$j]['click_type']=='phone'){
       echo "<td">
       echo $arr['user_clicks'][$j]['click_type'];
       echo "</td>";  
     }
     else 
     {
       echo "<td">
       echo '';
       echo "</td>";  
     }

     if($arr['user_clicks'][$j]['click_type']=='email'){
       echo "<td">
       echo $arr['user_clicks'][$j]['click_type'];
       echo "</td>";  
      }else 
      {
         echo "<td">
         echo '';
         echo "</td>";  
       }

       if($arr['user_clicks'][$j]['click_type']=='offline'){
           echo "<td">
          echo $arr['user_clicks'][$j]['click_type'];
          echo "</td>";  
        }
          else 
        {
        echo "<td">
         echo '';
        echo "</td>";  
         }

          if(!empty($arr['user_clicks'][$j]['click_count']){
             echo "<td">
            echo $arr['user_clicks'][$j]['click_count'];
             echo "</td>";  
           }
           else 
          {
          echo "<td">
          echo '';
              echo "</td>";  
           }


     }
       echo "</tr>";  
   }
}