Part of the EllisLab Network
   
 
sql to filter data from dropdown boxes
Posted: 08 February 2010 06:21 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  61
Joined  10-30-2009

I have 2 dropdown boxes called country and category. Default value for Both text boxes is ‘All’. I have a problem with writing the query.

If a country is selected(eg:USA) but All from category then USA results should display without considering the category. If a category is selected (eg: phone) but All from country then results about phone should display without considering the country

How can do this. How can I write the sql query

Thanx

Profile
 
 
Posted: 08 February 2010 09:00 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  101
Joined  08-09-2009

Im not sure i entirely understand your question but ill give it a shot anyway.

Your looking for a way to add a search function into a site, and add ‘default’ values into your dropdown boxes that don’t necessarily appear in your database? The first bit is relatively easy. In your controller simply add an extra arbitrary value onto your array of values. 0 or -1 is a good idea as it doesn’t need to be changed if you find yourself getting more results than you anticipated. (the -1 idea came from facebook). The sql query itself is a bit more complicated. In your model if you check that the returned value does not equal your default value, then add the relavant ‘where’ clause onto your query, and build it as a string. I wrote a very similar function for a project we were working on:

function db_search ($search_array)
        
{
            $start_date
= $search_array['start_date'];
            
$end_date = $search_array['end_date'];
            
            
$query_string = "SELECT issues.*, assignments.assignment_user_id, priorities.priority_code, status_list.status_code
                                   FROM issues
                                   LEFT JOIN assignments ON issues.issue_id = assignments.assignment_issue_id
                                   LEFT JOIN priorities ON issues.issue_priority_id = priorities.priority_id
                                   LEFT JOIN status_list ON  issues.issue_status_id = status_list.status_id
                                   WHERE issue_raised > "
.mysql_real_escape_string($start_date) ."
                                   AND issue_raised < "
. mysql_real_escape_string($end_date);
            
            if(
$search_array['raised_by'] != 0)
            
{
                $query_string
.= " AND issue_raised_by = ".mysql_real_escape_string($search_array['raised_by']);
            
}
            
            
if($search_array['open_closed'] == 1)
            
{
                $query_string
.= " AND issue_closed_by IS NULL";
            
}
            
elseif($search_array['open_closed'] == 2)
            
{
                $query_string
.= " AND issue_closed_by IS NOT NULL";
            
}
            
            
if($search_array['priority'] != 0)
            
{
                $query_string
.= " AND issue_priority_id = ".$search_array['priority'];
            
}
            
            
if($search_array['status'] != 0)
            
{
                $query_string
.= " AND issue_status_id = ".$search_array['status'];
            
}
            
            
if($search_array['assigned'] != 0)
            
{
                $query_string
.= " AND assignments.assignment_user_id = ".$search_array['assigned'];
            
}
            
            $result
= $this->db->query($query_string);
            
            
$data = $result->result();
            
            return
$data;
        
}
 Signature 

FastFramework - Coming Soon

Profile
 
 
Posted: 09 February 2010 08:17 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  61
Joined  10-30-2009

This is my code. The conditions inside the if statements are not working. It means the filtering doesn’t happen

  function search_vacancies($data)
  {
      $key_word=$data[‘key_word’];
      echo $category=$data[‘category’];
      $city_state=$data[‘city_state’];

      $sql = ” SELECT tt_vac_vacancies.s_vac_designation,tt_rec_recruiters.s_rec_company_name,city_states.city_name,countries.country_name, tt_vac_vacancies.end_date, tt_vac_vacancies.i_vac_ID FROM tt_vac_vacancies
      LEFT JOIN tt_rec_recruiters ON tt_vac_vacancies.l_vac_account_ID = tt_rec_recruiters.l_rec_ID
      LEFT JOIN city_states ON tt_vac_vacancies.city_id = city_states.city_id
      LEFT JOIN countries ON city_states.country_id = countries.country_id “;
     
      if($category!=0)
        $sql = $sql. “AND tt_vac_vacancies.l_vac_job_category = “. $category;
      if($city_state!=0)
        $sql = $sql. “AND tt_vac_vacancies.city_id = “. $city_state;
       
       
      $query=$this->db->query($sql); 
     
      return $query;     
  }

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120504 Total Logged-in Users: 40
Total Topics: 126572 Total Anonymous Users: 4
Total Replies: 665455 Total Guests: 382
Total Posts: 792027    
Members ( View Memberlist )