1

Hi i'm trying to get a count of rows but no luck so far maybe there's something with the database becouse i get some strange numbers

here's the database egs

poll_id option_poll_id gender
1       100            male
1       100            male
1       103            femeale
1       103            male

so what i want is that for each option 100 and 103 to get the number of male or femeals

like this option 100 -> 2 male and option 103->1 male Thanks in advance.

3 Answers 3

5

The below is an example taken directly from http://framework.zend.com/manual/en/zend.db.table.html

  $table = new Bugs();

  $select = $table->select();

  $select->from($table,

                array('COUNT(reported_by) as `count`', 'reported_by'))

         ->where('bug_status = ?', 'NEW')

         ->group('reported_by');

  $rows = $table->fetchAll($select);

Same applies here . Change with your table name and fields :) .

You can also execute @Marc B 's query . This is more specific to Zend_Db_Table .

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you all for your comments however i still have a small problem
Without telling what you are facing how can we know the problem :(
2

What's your query look like? This should get you the numbers you need:

SELECT option_poll_id, gender, COUNT(gender)
FROM yourtable
GROUP BY option_poll_id, gender

Comments

2

this is the code i use now however the results are good but when i fetch them it's showing something like

Engine_Db_Table_Rowset Object ( [_rowClass:protected] => Engine_Db_Table_Row [_rowsetClass:protected] => Engine_Db_Table_Rowset [_data:protected] => Array ( [0] => Array ( [poll_option_id] => 108 [gender] => male [count] => 1 ) ) [_table:protected] => Poll_Model_DbTable_Votes Object ( [_rowClass:protected] 


foreach ($this->pollOptions as $i => $option_chart_f):

$table  = Engine_Api::_()->poll()->api()->getDbtable('votes', 'poll');
    $select = $table->select()
                   ->from($table->info('name'), array(
                      'poll_option_id','gender',
                      new Zend_Db_Expr('COUNT(gender) as count'),
                    ))
                    ->where('poll_id = ?', $option_chart_f->poll_id)
                    ->where('poll_option_id =?', $option_chart_f->poll_option_id)
                    ->group('poll_option_id')
                    ->group('gender');

$rows = $table->fetchAll($select);
print_r($rows);

endforeach;

Thank you

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.