0

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

So how can I use array_count_values to dynamically pass values in CodeIgniter.

1
  • The array_count_values function will not work with multidimensional arrays. You will most likely need to find another solutions or make your own Commented Jan 13, 2014 at 9:21

4 Answers 4

2
SELECT
    country,
    count(country) as `Total`
FROM list
GROUP BY country
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

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

Also you can find a full and detailed answer here

1 Comment

This query will execute total number of distinct country in the table. But I need for India - 5, Mexico- 2 like wise i need
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');
    }

Comments

0

You can use num_row() function for this

  $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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.