cake-php@googlegroups.com
[Top] [All Lists]

Re: Problem with counting records

Subject: Re: Problem with counting records
From: calvin
Date: Mon, 14 Jun 2010 07:49:54 -0700 PDT
$locations = $this->Location->find('list');
$report = array();
foreach ($locations as $locationId => $locationName) {
  foreach ($statuses as $status) {
    $report[$locationId][$status] = $this->Voter->find('count', array(
      'conditions' => array(
        'Voter.location_id' => $locationId,
        'Voter.status' => $status
      )
    ));
  }
}
$this->set(compact('locations', 'report'));

I'd recommend reading the Cookbook and doing the blog example if
you're still unsure as to how to send data from a controller to the
view.

On Jun 14, 5:04 am, Kiza <kiza2...@xxxxxxxxx> wrote:
> Hello,
>
> I'm currently working on app for Voters registering and it's pretty
> much completed.
>
> I have 5 tables in database: voters, locations, categories, users,
> statuses.
>
> Table voter is in "belongs to" relationship with other tables and
> tables have following structure:
>
> voters
>
> id int(11) NOT NULL auto_increment,
>
> firstName varchar(50) NOT NULL,
>
> lastName varchar(50) NOT NULL,
>
> phone varchar(50) NOT NULL,
>
> status_id int(11) NOT NULL,
>
> user_id int(11) NOT NULL,
>
> location_id int(11) NOT NULL,
>
> category_id int(11) NOT NULL,
>
> locations
>
> id int(11) NOT NULL auto_increment,
>
> name varchar(50) NOT NULL,
>
> statuses
>
> firstName0 int(11) NOT NULL auto_increment,
>
> firstName1 varchar(50) NOT NULL,
>
> categories
>
> firstName2 int(11) NOT NULL auto_increment,
>
> firstName3 varchar(50) NOT NULL,
>
> What I would like to do is display all locations names, each followed
> with a count of the number of voters from that locations for status 1,
> status 2 and so on
>
> .
>
> Basically I must generate report witch has following structure:
>
> Location name| Number of voter in status 1 | Number of voters in
> status 2 | etc.
>
> Location 1
>
> Location 2
>
> Location 3
>
> etc
>
> I guessing that I should use find('count') or counterCache but I just
> don't know how to write that query or how to send result to view. This
> is really major problem for me so if anyone have some suggestion it
> will be appreciated.
>
> If you need some additional information I will be happy to provide it
> in order to solve this problem.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@xxxxxxxxxxxxxxxx
To unsubscribe from this group, send email to
cake-php+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

<Prev in Thread] Current Thread [Next in Thread>