CodeIgniter Forums
ci search based on parameters user selects - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: ci search based on parameters user selects (/showthread.php?tid=34505)



ci search based on parameters user selects - El Forum - 10-01-2010

[eluser]ravindral[/eluser]
Hello all CI lovers,
Sorry but it is urgent, i have to deliver the project. As this is my first project in CI, i dont know how to do search engine. Lets say i have country, city and subject parameters. So based on selection of combinations, i have to show search result of teachers.
Lets say user selects UK and Workshire as city OR UK and maths as 2 parameters. I dont know which combination out of 3 user will select. So how should i handle search?

Thanks,
Ravindra


ci search based on parameters user selects - El Forum - 10-01-2010

[eluser]JuanitoDelCielo[/eluser]
Fetch the get_where with an array with the filters.


ci search based on parameters user selects - El Forum - 10-01-2010

[eluser]techgnome[/eluser]
Make sure that on the search form the default unselected value is something that is testable (like 0, or -1, or blank). Then you test the values from the post ($this->input->post('name of field)Wink ... after the form is submitted... if it isn't the default value you specified, then it the value is selected. From there you can incorporate it into your query.

Or was the question more from a "How do I build the query?" kind of a question?

I have an example of that... but I can't get to it right this second... I'll post it when I get home.

-tg


ci search based on parameters user selects - El Forum - 10-01-2010

[eluser]JuanitoDelCielo[/eluser]
[quote author="techgnome" date="1285975109"]Make sure that on the search form the default unselected value is something that is testable (like 0, or -1, or blank)[/quote]

The $this->input->post() an get() functions return false if the value isn't set.

Code:
<select name="country">
    <option value="0">Select a Country...</option>
    <option value="1">UK</option>
</select>

<select name="city">
    <option value="0">Select a City...</option>
    <option value="1">London</option>
</select>

&lt;input name="data" value="value" &gt;

Code:
$country = $this->input->post('country');
$city = $this->input->post('city');
$data = $this->input->post('data');

if ( $country ) {

$this->db->where('country', $country);

}

if ( $city ) {

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

}

if ( $data ) {

//...

}



ci search based on parameters user selects - El Forum - 10-01-2010

[eluser]ravindral[/eluser]
Thats good idea what you said. It didnt came in my mind at all Smile
Also i would like to see how to build the query depending upon number of parameters but please note any parameter can be or cant be selected by user.

Thank you.




[quote author="techgnome" date="1285975109"]Make sure that on the search form the default unselected value is something that is testable (like 0, or -1, or blank). Then you test the values from the post ($this->input->post('name of field)Wink ... after the form is submitted... if it isn't the default value you specified, then it the value is selected. From there you can incorporate it into your query.

Or was the question more from a "How do I build the query?" kind of a question?

I have an example of that... but I can't get to it right this second... I'll post it when I get home.

-tg[/quote]


ci search based on parameters user selects - El Forum - 10-01-2010

[eluser]JuanitoDelCielo[/eluser]
The db class build the query you don't need to worry about that. If the user SET a VALUE for the param codeigniter will add it to the query. If you want to know your last query use the last_query(); function to know if the query is what you want.