How do I change this select query to multiple choice list

Please, I want to convert this single select option to multiple choice options
<?php

                          $get_sizes = "select * from sizes";
                          $run_sizes = mysqli_query($con,$get_sizes);
                          
                          while ($row_sizes=mysqli_fetch_array($run_sizes)){
                              
                              $size_id = $row_sizes['size_id'];
                              $size_name = $row_sizes['size'];
                              
                              echo "
                              
                              <option value='$size_id'> $size_name </option>
                              
                              ";
                              
                          }
                          
                          ?>
                          
                      </select><!-- form-control Finish -->

Try a foreach loop…

while ($row_sizes=mysqli_fetch_array($run_sizes)){
    foreach ($row_sizes as $row_size) {

        echo '<option value="'.$row_size['size_id'].'">'.$row_size['size'].'</option>';

    }
}

You would make the following changes -

  1. Make the <select name='some_name' attribute into an array <select name='some_name[]'
  2. Add the multiple attribute the the <select …> tag.
  3. To pre-select existing <option choices, which you are not doing now, you would use in_array(), rather than just an equal comparison.
  4. In the php code, you will now get an array of the selected option choices, rather than a single value.

@jimbopp, a double loop is not what you want. (while, foreach)

I understand. I did forget to add Break there…

while ($row_sizes=mysqli_fetch_array($run_sizes)){
    foreach ($row_sizes as $row_size) {

        echo '<option value="'.$row_size['size_id'].'">'.$row_size['size'].'</option>';

    }
break;
}

No, you don’t understand. You still have two loops, a while loop and a for each Loop. break doesn’t belong in there at all.

Thanks very much but instead of showiing the sizes but it was showing sizes id in the select drop down menu.
2. to insert multiple size_id in the product table is difficult. do I need to change size_id type from integer to string or can it enter multiple size_id in one record on the product table

No. Don’t do that.

Different sizes of something are different items. You would have a separate row in a database table for each different size.

okay I am getting close but where I inserted the the thing in the database , like the code below but how can I chan asign echo ("$i
") in the foreach loop basck to $size variable in order for me to enter multiples sizes on the size column

if(isset($_POST['submit'])){

$size = $_POST['size'];

  foreach ($size as $i)
{
echo ("$i<br>");

} 
$insert_product = "insert into products (size) values ('$size')

thanks but can you elucidate further.
how do I change the below code for it to enter multiple sizes in the database codlumn
if(isset($_POST[‘submit’])){

$size = $_POST[‘size’];

foreach ($size as $i)
{
echo ("$i
");

}
$insert_product = "insert into products (size) values (’$size’)

You would use a prepared query, prepared once before the start of any looping, then just supply each size_id value when you execute the prepared query.

You should also not try to detect if the submit button is set. There are cases where it won’t be. Just detect that a post method form was submitted. You should also detect if any of the size choices have been selected (that $_POST[‘size’] is not empty) and you would also need to validate that each value is a permitted size_id before using it.

Sponsor our Newsletter | Privacy Policy | Terms of Service