I'm confused at what you mean in the dropdown - are both the
"keywords" textfield and "places" dropdown on the same form?
In that case, you can't get the number of people to display on each
option, because that information would be dependant on the keywords.
The only way to do that would be through AJAX, to dynamically update
the select box when the keywords field changes - is that what you're
trying to do?
Assuming you're not, and the keywords have already been input, and you
want to display the selectbox to narrow the results (on a subsequent
page):
// Assumes $keywords is a simple array like array('skating', 'ice');
// Get the people, joined on hobby and place
$people = $this->Person->find('all', array('conditions' =>
array('Hobby.name' => $keywords), 'recursive' => 0));
$placesList = array();
$placesPeopleCount = array();
foreach ($people as $row) {
// For clarity
$state = $row['Place']['state'];
$placeId = $row['Place']['id'];
if (!isset($placesList[$state][$placeId])) {
// First entry into this option
$placesList[$state][$placeId] = $row['Place']['name'];
$placesPeopleCount[$placeId] = 1;
}
else {
// Subsequent entry, just increment count
$placesPeopleCount[$placeId]++;
}
}
// Now append the count onto the end of the option
foreach ($placesList as $state => $entries) {
foreach ($entries as $id => $name) {
$entries[$id] = sprintf('%s [%s]', $name,
$placesPeopleCount[$id]);
}
$placesList[$state] = $entries;
}
$this->set(compact('placesList'));
// -------------- in the view
echo $form->select('Search.places', $placesList, null,
array('showParents' => true));
Of course, it might be more efficient to query the places/hobby
directly, using COUNT() and GROUP BY, but this will work quite well.
You could also restrict the fields returned by the findAll call,
because you don't need most of them.
On Dec 7, 8:17 am, AD7six <[EMAIL PROTECTED]> wrote:
> On Dec 7, 2:53 am, kiger <[EMAIL PROTECTED]> wrote:
>
> > EDIT: I figured out a better way to describe step 3c)...
>
> > For each 'Place.name' I count up the number of people having a
> > 'Hobby.name' matching the selected keywords. I display this total in
> > parenthesis after the name of the place (e.g., Winter Ice Rink (2))
>
> I drifted off, as I'm sure many will after reading "so here is what I
> got" and not seeing anything clear. What sql do you want to achive?
>
> AD
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---