Welcome Guest, Not a member yet? Register   Sign In
Running a Basic Search
#5

[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.


Messages In This Thread
Running a Basic Search - by El Forum - 09-12-2010, 11:15 PM
Running a Basic Search - by El Forum - 09-13-2010, 03:28 AM
Running a Basic Search - by El Forum - 09-13-2010, 05:49 AM
Running a Basic Search - by El Forum - 09-13-2010, 07:00 AM
Running a Basic Search - by El Forum - 09-13-2010, 08:29 AM
Running a Basic Search - by El Forum - 09-13-2010, 06:50 PM



Theme © iAndrew 2016 - Forum software by © MyBB