m7md
m7md

Reputation: 231

Multi select dropdown list

I have a search page with multi input search field in one of this input i use a multi select dropdown list in my status show it below i can select many word to do the search on it this multi select dropdown list is from Here

The form is like this:

    <form class="form form-inline form-multiline" role="form" name="form" method="post" action="<?php $_PHP_SELF ?>">
      <div class="form-group">
        <label for="pwd">Type</label>
        <select id="selectbasic" name="txt_type" class="form-control">
      <option value="">--SELECT--</option>
          <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Inspection') { ?>selected="true" <?php }; ?>value="Inspection">Inspection</option>
          <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Service') { ?>selected="true" <?php }; ?>value="Service">Service</option>
          <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Project') { ?>selected="true" <?php }; ?>value="Project">Project</option>
          <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Appointment') { ?>selected="true" <?php }; ?> value="Appointment">Appointment</option>
          <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Email') { ?>selected="true" <?php }; ?> value="Email">Email</option>
           <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Phone Call') { ?>selected="true" <?php }; ?> value="Phone Call">Phone Call</option>
           <option<?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Service Activity') { ?>selected="true" <?php }; ?> value="Service Activity">Service Activity</option>
        <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Task') { ?>selected="true" <?php }; ?> value="Task">Task</option> 
        </select>
      </div>
     <div class="form-group">
        <label for="pwd">Status</label>
        <select  name="txt_status[]"  multiple id="langOpt">
          <option <?php if (isset($_POST['txt_status']) && $_POST['txt_status'] == 'Scheduled') { ?>selected="true" <?php }; ?>value="Scheduled">Scheduled</option>
          <option <?php if (isset($_POST['txt_status']) && $_POST['txt_status'] == 'Cancelled') { ?>selected="true" <?php }; ?>value="Cancelled">Cancelled</option>
          <option <?php if (isset($_POST['txt_status']) && $_POST['txt_status'] == 'Pending') { ?>selected="true" <?php }; ?>value="Pending">Pending</option>
           <option <?php if (isset($_POST['txt_status']) && $_POST['txt_status'] == 'Done') { ?>selected="true" <?php }; ?>value="Done">Done</option>
            <option <?php if (isset($_POST['txt_status']) && $_POST['txt_status'] == 'In Progress') { ?>selected="true" <?php }; ?>value="In Progress">In Progress</option>
        </select>
      </div>
</form>

The Multi select is on the status field

Know on PHP search code

if(isset($_POST['submit'])) {
$q = array(); 
    $sql = "";
      if(isset($_POST['txt_type']) && !empty($_POST['txt_type'])){  
      $type =  mysqli_real_escape_string($conn,$_POST['txt_type']); 
    $q[] = " db_type='".$type."' ";      
    } 
    if(isset($_POST['txt_status']) && !empty($_POST['txt_status'])){ 
    $status = $_POST['txt_status'];
    $a=implode(",",$status);       
    $q[] = " db_status='".$a."' ";      
    } 
    $first = true;     
    foreach($q as $qu){  
        if($first){  
        $sql .= " where ".$qu;      
        $first = false;  
        }else{  
        $sql .= " and ".$qu;          
        } 
    } 
    $result=mysqli_query($conn,"SELECT * FROM tbl_staff {$sql}  ")or die(mysqli_error($conn));
}

Know if i print the query the query will be like this

SELECT * FROM tbl_staff where db_status='Scheduled,Cancelled'

The problem is i have a "," in db_status='Scheduled,Cancelled'

But it should be like this

SELECT* FROM TABLE WHERE db_status='Scheduled' and db_status='Cancelled'

in this case i choose to things from the status maybe i choose one or three or more

How Can i correct The query to have the exact result I want?

Upvotes: 1

Views: 1008

Answers (2)

B. Desai
B. Desai

Reputation: 16436

try this solution:

if(isset($_POST['txt_status']) && !empty($_POST['txt_status'])){ 
    $statuses = $_POST['txt_status'];
    foreach ($statuses as $status) {
        $q[] = " db_status='".$status."' ";      
    }

}

Upvotes: 0

Suchit kumar
Suchit kumar

Reputation: 11859

convert your implode to this and use in and try:

$a="'". implode("', '", $status) ."'";    // gives like '1', '2'

like :

$q[] = " db_status in($a) ";      

BUT:

if you want to use and you can do like this:

foreach($status as $k=>$v){   
     $q[] = " db_status='".$v."' ";      //use this in your other loop.
    } 

Upvotes: 1

Related Questions