2016-11-20 12 views
-2

タイプに基づいて、私は次の表カウントし、表示結果CodeIgniterの

|id|user_type| 
|1|Customers| 
|2|Suppliers| 
|3|Customers| 
|4|Suppliers| 
|5|Suppliers| 
|6|Employees| 

を持っている私は、総顧客数をカウントしたい、サプライヤー、employess

  • お客様(2)
  • サプライヤー(3)
  • (1)ここで
  • は私のhtml

    ある従業員
     <a href="#" class="customers list-group-item"> 
          <span class="badge badge-info"> 
           2 
          </span> 
          Customers 
         </a> 
         <a href="#" class="employees list-group-item"> 
          <span class="badge badge-danger"> 
           3    
          </span> 
          Employees   
         </a> 
         <a href="#" class="suppliers list-group-item"> 
          <span class="badge badge-success"> 
           1    
           </span> 
          Suppliers   
         </a> 
    

    マイクエリコードは、このコードが動作しません。この

     <?php 
           $this->load->model("User_model"); 
           $query = $this->db->query('SELECT user_type, COUNT(Customers) AS total FROM `users` GROUP BY user_type'); 
    
         if ($query->num_rows() > 0) 
        foreach ($query->result() as $row) 
         $data['users'] = array(
          'total' => $row->total, 
         ); 
    
    echo $data['users']; 
    

    のように見えます。あなたの時間をありがとう。

    +0

    で実行されます。各データの繰り返しで$ data ['users'] 'が上書きされます。 1054 不明列USER_TYPE BY users' GROUP 'FROM jobcount AS 'フィールドリスト' SELECT USER_TYPE、COUNT(顧客)の「お客様が –

    答えて

    1

    foreachの各反復で変数$data['users']が上書きされます。 何をする必要がありますか?$data['users']に追加してください。配列への追加は[]

    foreach ($query->result() as $row) { 
        $data['users'][] = array(
         'type_name' => $row->user_type, 
         'total' => $row->total, 
        ); 
    } 
    
    // `echo` will no work with `arrays`, use `print_r` instead 
    print_r($data['users']); 
    
    +0

    卿は、私がerrorAデータベースエラーが エラー番号を発生した取得していますnameは 'user_type'なので' COUNT(user_type)as total'ですので注意してください。 – TPLMedia24

    +0

    あなたのコラム: –

    +0

    私はuser_typeを数えたくありませんが、顧客、サプライヤー、従業員の列をカウントしたい – TPLMedia24

    関連する問題