Welcome Guest, Not a member yet? Register   Sign In
Weird problem with If vs ternary operator unexpected results
#11

[eluser]edjon2000[/eluser]
Hi Aken,

Thanks for your response, hmm, you set me thinking about this.

So, off the top of my head it seems that perhaps this is the better approach,

instead of
Code:
$name = ($post['name'] == 'All Companies') ? '' : $post['name'];
$sector = ($post['sector'] == 'All Sectors') ? '' : $post['sector'];
$location = ($post['location'] == 'All Locations') ? '' : $post['location'];
$phone = ($post['first_contact_phone'] == 'All Phone Numbers') ? '' : $post['first_contact_phone'];
$postcode = ($post['postcode'] == 'All Postcodes') ? '' : $post['postcode'];
this may work (not tested),
Code:
$post['name'] = ($post['name'] == 'All Companies') ? '' : $post['name'];
$name = $post['name'];

$post['sector'] = ($post['sector'] == 'All Sectors') ? '' : $post['sector'];
$sector = $post['sector'];

$post['location'] = ($post['location'] == 'All Locations') ? '' : $post['location'];
$location = $post['location'];

$post['first_contact_phone'] = ($post['first_contact_phone'] == 'All Phone Numbers') ? '' : $post['first_contact_phone'];
$phone = $post['first_contact_phone'];

$post['postcode'] = ($post['postcode'] == 'All Postcodes') ? '' : $post['postcode'];
$postcode = $post['postcode'];

Is this the sort of idea you were trying to put across?

if it is, then theoretically, I could do something like this (not tested),
Code:
$name = {$post['name'] = ($post['name'] == 'All Companies') ? '' : $post['name']};
$sector = {$post['sector'] = ($post['sector'] == 'All Sectors') ? '' : $post['sector']};
$location = {$post['location'] = ($post['location'] == 'All Locations') ? '' : $post['location']};
$phone = {$post['first_contact_phone'] = ($post['first_contact_phone'] == 'All Phone Numbers') ? '' : $post['first_contact_phone']};
$postcode = {$post['postcode'] = ($post['postcode'] == 'All Postcodes') ? '' : $post['postcode']};

Jon.
#12

[eluser]InsiteFX[/eluser]
Comparison with Various Types
--1------2----
array anything - array is always greater

Try this:
Code:
$value = $post['name'];
$name  = ($value == 'All Companies') ? '' : $value;
#13

[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();
}
#14

[eluser]edjon2000[/eluser]
Hi Aken

Sorry for not getting back to you earlier, thanks for the response

First of all I had not even considered that approach, it solves a number of problems in one go and from the looks of it, I could set up different defaults in the controllers and pass those as well as the $post array through to the model thus making the model method a bit more generic.

The model method I showed earlier forms part of a larger, mass emailing, website admin newsletter option where a site admin can mass email their clients, candidates or users along with the option to send attached files and use various in-house HTML email templates.

I chose to use LIKE in the query as all of this is being added to an existing set of tables so partial searches were requested e.g. all phone numbers beginning with 0161, sectors containing the word industrial, and so on.

Jon.





Theme © iAndrew 2016 - Forum software by © MyBB