简体   繁体   English

如何在codeigniter中使用array_count_values()

[英]How to use array_count_values() in the codeigniter

I need to get the count of each countries by the database. 我需要通过数据库获取每个国家的数量。

Here my coding is 这是我的编码

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

The select query returns object as result and array_count_values need array in the parameter 选择查询返回对象作为结果,并且array_count_values需要参数中的数组

So how can I use array_count_values to dynamically pass values in CodeIgniter. 因此,如何使用array_count_values在CodeIgniter中动态传递值。

Try this: 尝试这个:

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

Also you can find a full and detailed answer here 您也可以在这里找到完整和详细的答案

SELECT
    country,
    count(country) as `Total`
FROM list
GROUP BY country
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');
    }

You can use num_row() function for this 您可以num_row()使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM