CodeIgniter Forums
Running a Basic Search - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Running a Basic Search (/thread-33908.html)



Running a Basic Search - El Forum - 09-12-2010

[eluser]TG JoE[/eluser]
Could not find any kind of tutorial on Google or here about just doing a basic search of the database. Which, I guess I need a little more than just a basic search.

Here's my problem, I know I could write this thing the long way, but I like doing things the right way. Especially when using CodeIgniter!

I've got 4 fields in a database. fname, lname, city and state. All I want to be able to do is when someone searches on any of those things, it bounces them to a page with results. Super simple, but couldn't find anything on it.

The long way would be grabbing each field variable, running the combinations and having like 12 query strings in my model. Is there a shorter way to construct the model? The "right" way, as it were.

Any help would be greatly appreciated. Thanks!


Running a Basic Search - El Forum - 09-13-2010

[eluser]jmadsen[/eluser]
Is this a school assignment by any chance?


Running a Basic Search - El Forum - 09-13-2010

[eluser]Jelmer[/eluser]
Take a look at CI's built in query-builder: Active Record.

Here's a short example:
Code:
$fname = $this->input->post('fname');
$city = $this->input->post('city');

if ($fname)
    $this->db->where('fname', $fname);
if ($city)
    $this->db->where('city', $city);

$query = $this->db->get('table_name');

$result = $query->result();



Running a Basic Search - El Forum - 09-13-2010

[eluser]TG JoE[/eluser]
Ha! Not a school assignment. I used to do more CI development back in the day but moved to Wordpress about a year ago. So now that a buddy asked me to do this for him "real quick", I was like "yeah, sure. Should be easy enough."

The example posted is how I was thinking it would be written, the long way. Grabbing each post variable, running the query on it and having a query for each single post variable and then writing more queries to handle each combination of variables as well. Then probably calling the correct model through a switch statement depending on which form values actually had inputs.

Is that the best way? I think I was looking for some way to dynamically generate the query string so it only has to be written once and will just an accept an array of $_POST variables to generate the results.


Running a Basic Search - El Forum - 09-13-2010

[eluser]Jelmer[/eluser]
Maybe name your fields like this 'search[fname]' and then process like:

Code:
$search = $this->input->post('search');

foreach ( $search as $key => $value )
    $this->db->where( $key, $value );

$query = $this->db->get('table_name');

$result = $query->result();

But this is incredibly insecure because it's incredibly easy to add fields to the posted values! That way someone could enter fields to search you wouldn't want searchable. And to secure it might increase the code to become just like the other example. So think carefully about how you proceed and what the security risks are.

My previous example already brought your amount of code from 12 lines to 4 conditionals (one if/$db->where per searchable field). This last example is more flexible but not necessarily better, it's only better if you're expecting to be editing the fields from time to time and need this easily configured.


Running a Basic Search - El Forum - 09-13-2010

[eluser]TG JoE[/eluser]
The security explanation is EXACTLY what I was looking for. And you were right, the first example was the better way, but the second example is what I was thinking I wanted.

Thank you very much for your help, it's definitely appreciated.