简体   繁体   中英

PHP and MYSQLi for <select> drop down

I have inserted group of values on MySQL column and select on php as results like

Array ( [0] => Array ( [custom_options] => 1,2,3,4 ) ) 

I need to insert this values <select> drop down now. What do I need to use foreach , for , while to get results in <select> drop down?

you can make another array depends on your value by explode() Example :

$arrayVarible = Array ( [0] => Array ( [custom_options] => 1,2,3,4 ) )
$explodeId = explode(',',$arrayVarible[0]['custom_options']);
$explodeName = explode(',',$arrayVarible[1]['custom_options']);

echo '<select>';
   for($i=0;$i<count($explodeId);$i++){
      echo '<option>'.$explodeId[$i].'</option>';
   }
echo '</select>';

Update

echo '<select>';
   for($i=0;$i<count($explodeName);$i++){
      echo '<option>'.$explodeName[$i].'</option>';
   }
echo '</select>';

First You Need to explode the string with comma(,) then print it with a loop.

$op=array(array('custom_options'=>'1,2,3,4'));
$val=explode(',',$op[0]['custom_options']); // Explode the string 
echo '<select>';
for($i=0;$i<count($val);$i++){
    echo '<option>'.$val[$i].'</option>';
}
echo '</select>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM