2014-01-13 19 views
0

我需要通过数据库获取每个国家/地区的计数。如何在codeigniter中使用array_count_values()

这里我的编码是

$query = $this->db->query("SELECT country FROM list"); 
    echo"<pre>"; print_r($query->result()); 

的选择查询返回对象的结果,并array_count_values需要数组中的参数

因此,如何我可以用array_count_values动态传递CI中值。

+0

的array_count_values功能将不会 使用多维数组。您很可能需要找到其他解决方案或自己创建 – mic

回答

2

试试这个:

SELECT country, Count(*) FROM list GROUP BY country 

您也可以找到一个完整和详细的解答here

+0

此查询将执行表中不同国家/地区的总数。但我需要印度 - 5,墨西哥 - 2像智慧我需要 – user2451266

+2

'按国家组'# –

2
SELECT 
    country, 
    count(country) as `Total` 
FROM list 
GROUP BY country 
0
you can following code 

function fun_name() 
    { 
     $this->db->where('id !=',0);// like this you can set conditions 
     return $this->db->count_all_results('table_name'); 
    } 
+0

这将计算结果的行数.... –

0

可以使用num_row()功能这个

$this->db->select('country'); 
    $this->db->from('list'); 
    $query = $this->db->get(); 

    echo $query->num_rows(); // this will print the count of all countries 
    echo"<pre>"; print_r($query->result()); // this can be used for fetching countries name(else)