开发者

How to count rows zend framework?

开发者 https://www.devze.com 2023-03-07 07:15 出处:网络
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

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.


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 .


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号