Friday, February 25, 2011

RE: Search function

For the Alphabet I made my own custom plugin function that just runs thru
the alphabet and does a query to find each letter. My table has search_group
field which saves the first letter upper / lowercase you decide of whatever
field the alphabet is for (name, business whatever) creates links if a
letter is found if not just a letter is returned. Cache the query so it only
runs 1 time and after a new user / member / product is added delete the
cache and update with a new running of the alpha function.


public function alpha_paging () {

$alpha_links = Cache::read('alpha_nav_'. $this->alias, 'short');

if (empty($alpha_links ) ) {
$alpha_links = array();

foreach(range('A','Z') as $i):
$params = array(
'conditions' => array(
$this->alias.'.search_group' => $i),
'fields' => array(
$this->alias.'.title',
$this->alias.'.id'),
'contain' => false);

if ( $this->find('first', $params) ) {
$alpha_links[] = 1;
} else {
$alpha_links[] = 0;
}
endforeach;
Cache::write( 'alpha_nav_'. $this->alias, $alpha_links,
'short' );
}
return $alpha_links;
}

You end up with an array 0 - 25 with each having a bool 0 or 1 if letter was
found. The array is numeric but that does not matter since we know how many
letters there are in the alphabet.

0 => 0 // A has no record
1 => 0 // B has no record
2 => 1 // C has a record

_______________________________
Each array key represents the numeric count of the letter(offset by 1)
0 => A
1 => B
....
25 => Z

Hope that gets you started.

K


-----Original Message-----
From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf
Of MeatSandwich
Sent: Thursday, February 24, 2011 3:43 PM
To: CakePHP
Subject: Search function

Hi all,

I'm new to cakephp and only know a bit of php so I'm on a steep
learning curve since although I've made websites using php and mysql
before they were all a bit simple. I'm not looking for code here but
some pointers for me so I know what to learn.

The app I'm making will end up with lots of members and I'd like to
make a search function so people can find other people easily. I've
discovered searchable-behaviour-for-cakephp and hopefully that'll
provide most of what I want but as another way to search rather than a
text box, I'd also like to have the letters of the alphabet so a user
can click and go straight to that letter.

As well as that, members will say in their profiles what their
favourite animals are, ie cats/dogs/horses etc - they'll have 20 to
choose from. I'd like my search function to have checkboxes
representing each animal and someone who was searching could check the
dogs box and only people who like dogs will be listed, they could then
also check cats box and the members who like dogs and cats will be
shown. If possible I'd like the list to update automatically as each
box is clicked.

I have everything else set up and only need help with this search
part. Assuming searchable-behaviour-for-cakephp does me for my text
search box, I reckon the alphabet thing will be something like making
a link for each letter and somehow making that link run a query,
hopefully that will work.

But the checkbox thing that updates automatically I'm not sure about.
Is it ajax I need? Are there any particular methods I should be using?

--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help
others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at
http://groups.google.com/group/cake-php

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: