[eluser]Aken[/eluser]
Ok. Let's get involved here.
First, your problem aside for a moment, let's go back to the $post array. If you're generating it directly from the default $_POST array, then you're setting yourself up for possible issues. If I wanted to, I could create whatever $_POST keys I wanted on your page and submit them. The way you have your code now, any "hacked" keys would screw up your application and throw errors. That's not good.
Since your keys should relate to columns in the database, your client_search() method should have a list of acceptable keys (columns that are actually in the DB). Your method should then check the keys in the $post array against this list, and ignore any that do not exist.
Second, the coding style for checking the default values for your keys is kind of redundant and unnecessary. You're setting single variables like $name and $sector, and then doing nothing with them. So they're pretty unnecessary.
Third, I'm not sure if you're doing partial search matches or not, but if you're matching items directly in the database, you should use $this->db->where() instead of like().
I'd recommend doing something along these lines:
- Set a list of acceptable keys, and their default values.
- Loop through the $post array.
- Verify it's a valid field.
- Check for a default value.
- Add to query if not a default value.
- Perform the query and return the result.
This code is untested, but should give you a starting point for what I would recommend you use:
Code:
public function client_search($post = array())
{
// Default $post keys that should related to DB columns,
// and their default form values to check against.
$defaults = array(
'name' => 'All Companies',
'sector' => 'All Sectors',
'location' => 'All Locations',
'first_contact_phone' => 'All Phone Numbers',
'postcode' => 'All Postcodes',
);
$this->db->select()
->order_by('name');
// Loop our $post array. $pk = post key, $pv = post value
foreach ($post as $pk => $pv)
{
// Check if the key exists in our defaults.
if (array_key_exists($pk, $defaults))
{
// Check if the post value is the default value.
if ($pv !== $defaults[$pk])
{
// Add to our query.
$this->db->like($pk, $pv);
}
}
}
$query = $this->db->get('tbl_clients');
return $query->result();
}