verycuriouscat
verycuriouscat

Reputation: 45

How to extract the multiple values in the array output from Jquery chosen

I'm a beginner in learning web development. I'm currently trying to do Multi-select dropdown using Chosen Jquery plugin.

The Dropdown is query from database


Below are the code that I have done so far.

Query Data from SQL

$LOH_STATEMENT = "SELECT * FROM tableA";
 $result = ociparse($live, $LOH_STATEMENT);
 ociexecute($result);

Multi-select Dropdown.

<center>Program Name
<select data-placeholder="Select" multiple class="chosen-select" tabindex="17" name="program[]">
<option value="" ></option>

<?php   
     for($i=0; $i<count($result;$i++){
       while ($row = oci_fetch_assoc($result)){
       echo '<option value="'.$row['PROGRAM'].'">'.$row['PROGRAM'].'</option>';
       }
     }      
?>          
</select>

Query table according to data selected from the dropdown

if(! empty($_POST['program'])) {

                $i = 0;
                $selectedOptionCount_PROGRAM = count($_POST['program']);
                $selectedOption_PROGRAM = "";
                while ($i < $selectedOptionCount_PROGRAM) {
                    $selectedOption_PROGRAM = $selectedOption_PROGRAM . "'" . $_POST['program'][$i] . "'";
                    if ($i < $selectedOptionCount_PROGRAM - 1) {
                        $selectedOption_PROGRAM = $selectedOption_PROGRAM . ", ";
                    }
                    $i ++;
                }
                }                   
                $query = $LOH_STATEMENT . " WHERE program = ('" . $_POST['program'] . "') ";        
                }
                echo $query;

Jquery-Chosen

$(".chosen-select").chosen({
    no_results_text: "Oops, nothing found!",
    width: "30%",
});


The result that I get from the dropdown though is an array, and my question is how to extract the values from the array so I can put them in the database https://photos.app.goo.gl/hm5wdunufps9o2Gy7

(in case if the link doesn't work - when I echo $query, its shows; SELECT * FROM tableA WHERE program = 'Array' )

my desire result is; for example when user select "dance class " and "vocal class" it will give output SELECT * FROM tableA WHERE program = 'dance class, vocal class'

Is this possible? Or is there any other alternative or easier way to do it?

~ I'm sorry that this question is too long. I don't exactly know how to summarise it. Will do better next time. thank you :) ~

Upvotes: 2

Views: 128

Answers (1)

user8034901
user8034901

Reputation:

Since $_POST['program'] is an array you need to implode() it and change your query to use IN instead of =:

Change

$query = $LOH_STATEMENT . " WHERE program = ('" . $_POST['program'] . "') ";

to

$imploded = implode("','", $_POST['program']);
$query = $LOH_STATEMENT . " WHERE program IN ('" . $imploded . "')";

Upvotes: 1

Related Questions