Welcome Guest, Not a member yet? Register   Sign In
search form with multiple search options(SOLVED)
#1

[eluser]tim1965[/eluser]
Apologies in advance this is more of a SQL issue than codeigniter.
I am creating a search form that allows users to search on a number of options. This is working. However i want the search criteria to be optional. Thus if a user selects the start and end date to search on but leaves the country field blank i will only bring back all results that meet the start and end date criteria and that belong to all rows in the country field i.e. the country field would essentially search on a * for everything.
My query in my model is
$start_date = ($this->input->post('start_date', TRUE) == FALSE) ? date('Y-m-d') : $this->input->post('start_date');
$end_date = ($this->input->post('end_date', TRUE) == FALSE) ? date('Y-m-d') : $this->input->post('end_date');


$current_date = date("Ymd");


$sql = "SELECT guestcal_entries_static.entry_id, guestcal_entries.object_id, guestcal_entries.class_id, guestcal_entries_static.from, guestcal_entries_static.to, master_property_details.country
FROM guestcal_entries_static join guestcal_entries
on guestcal_entries_static.entry_id = guestcal_entries.id JOIN master_property_details
on guestcal_entries.object_id = master_property_details.property_id
where guestcal_entries_static.from >= '$current_date'
and guestcal_entries.class_id in ('2', '3')
and `from` not between $start_date and $end_date
and `to`not between $start_date and $end_date
and master_property_details.country = '$country'
";


$query = $this->db->query($sql);
if($query->num_rows() >0)
{
$row=$query->result_array();
return $row;
Iam populating default values for the start and end date if they are not set (thnks to help from this forum), however i need help with this line
Quote:
and master_property_details.country = '$country'
Quote:
I have to have thiis line doing an = search for the query to work, but i dont know what to susbstitute the $country variable with to equal everything in the column.
Essentially i need a wildcard like = '*'
I thought about splitting this into multiple queries or transactions but that wont work. As always any help appreciated.
#2

[eluser]tim1965[/eluser]
Solved i was having a bad hair day. Use like instead of = and default/replace if POST not set with a wildcard %




Theme © iAndrew 2016 - Forum software by © MyBB