0

I have a Table ANKETADATA in my database which containts a column PROVO. I want to have output like this:

povremeno : 15. (The number show how many times found this row in column PRVO) nikad : 40.

MODEL:

$anketadb = $this->load->database('anketa',TRUE);

    $anketadb->select('prvo');
    $anketadb->from('anketadata');
    $anketadb->group_by('prvo');
$result =   $anketadb->get();

$result =   $result->result_array();

$count  =   array('povremeno' => 0, 'nikad' => 0, 'svakibroj' => 0, 'prodajemkupujem' => 0);

foreach($result as $row){
    switch($row){
        case 'povremeno' :  $count['povremeno']++; break;
        case 'nikad' : $count['nikad']++; break;
        case 'svakibroj' :  $count['svakibroj']++; break;
        case 'prodajemkupujem' : $count['prodajemkupujem']++; break;
    }
}
return $count;

CONTROLLER:

$this->load->model('anketerezultati_model');
    $data['count'] = $this->anketerezultati_model->prvo();
    $this->load->view('ankete/rezultatiankete', $data);

VIEW:

echo "povremeno".$count['povremeno'];
echo "nikad".$count['nikad'];

The result is 0 for both. I cannot find the mistake.

1
  • I SOLVED THE PROBLEM : I put out row $anketadb->group_by('prvo'); than it counted everything like I wanted Commented Oct 19, 2012 at 7:07

2 Answers 2

0

Get rid of this:

$anketadb->group_by('prvo');

Your foreach:

foreach($result as $row){
   $val = $row['prvo'];
   $count[$val]++;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Now I have for some 1 an 0.... but i have 80 rows... so i not understand how... something is wrong
I just updated my code snippet to avoid the switch statement. See how that works out.
I this way its looks like, that it will count number of all rows. Am I wrong?
I SOLVED THE PROBLEM : I put out row $anketadb->group_by('prvo'); than it counted everything like I wanted
0

Wouldn't it be faster to do this with a SQL query like this:

SELECT COUNT(*) as `count`, `prvo` FROM `anketadata` GROUP BY `prvo`

3 Comments

I SOLVED THE PROBLEM : I put out row $anketadb->group_by('prvo'); than it counted everything like I wanted
It works like that yeah, but if you ever get more values in the prvo column, your code stop working, and if you have loads of records, it wil be alot slower then implementing the right query
In this table, I can not have another value, because there is a radio buttons in form. But thanks for advice!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.