1

PHP array explode result shows only last value in form select while print function shows all values. The HTML part should display all values in drop down. Currently, it's showing only the last value.

MYSQL

Table Name: op_subject_combination
Id = 1 and its op_subject_combo_sub1 values are (chemistry, physics, mathematics)

PHP Part

<?php   

 $select_enquiry="SELECT * FROM op_subject_combination where id = '1' order by id desc limit 20";
 $sql=$dbconn->prepare($select_enquiry);
 $sql->execute();
 $wlvd=$sql->fetchAll(PDO::FETCH_OBJ);
 if($sql->rowCount() > 0){  
   foreach($wlvd as $rows){
      $str = $rows->op_subject_combo_sub1;
      $arr = explode( ",", $str ) ;
      foreach ($arr as $row1)
?>

HTML Part

<div class="form-group">
   <label for="exampleInputEmail1">Session Name</label>
   <select class="form-control" id="op_subject_combo_session" name="op_subject_combo_session" >
      <option selected><?php echo $rows4->st_center; ?></option>
      <option>--------</option>
      <option><?php  echo  $row1   ;?></option>
      <?php }} ?>
   </select>
</div>
James Z
  • 12,209
  • 10
  • 24
  • 44

2 Answers2

1

Your nesting is wrong. The foreach should enclose only the <option> like below. You should also set selected on a single option, based on some criteria. And value="" for every option. Kindof, take a paper and pen and set down in natural language what you are trying to do and what the output should be.

<?php   
    $select_enquiry="SELECT * FROM op_subject_combination where id = '1' order by id desc limit 20";
    $sql=$dbconn->prepare($select_enquiry);
    $sql->execute();
    $wlvd=$sql->fetchAll(PDO::FETCH_OBJ);
    if($sql->rowCount() > 0){  
        foreach($wlvd as $rows){
?>
<div class="form-group">
   <label for="exampleInputEmail1">Session Name</label>
   <select class="form-control" id="op_subject_combo_session" name="op_subject_combo_session" >
<?php
            $str = $rows->op_subject_combo_sub1;
            $arr = explode( ",", $str ) ;
            foreach ($arr as $row1) {
?>
      <option <?=$row1==$rows4->st_center?' selected':''?>>
        <?php echo $row1; ?>
      </option>
<?php
            }
?>
      <option>--------</option>
   </select>
</div>
<?php 
        } 
    }
?>
Dinu
  • 1,374
  • 8
  • 21
0

You foreach, then echo after the loop has completed, thus only printing the last value. What you want is:

<?php
foreach ($arr as $row1)
{
   echo "<option>" .$row1 . "</option>";
}
?>
</select>

FWIW, I'm guessing that's happening because you're not understanding what's happening when you omit the curly brace after the foreach declaration. Spoiler alert: the foreach loop has completed before ?> This might help make it more clear: PHP - If/else, for, foreach, while - without curly braces?

Chris Ostmo
  • 1,202
  • 1
  • 10
  • 21
  • 1
    I support deprecating block-less branch syntax for all curly-brace languages. This feature has probably cost the world enough. – Dinu Mar 24 '19 at 09:07
  • @Dinu Amen, brother (or sister). I've disallowed the practice in my presence for many years. – Chris Ostmo Mar 24 '19 at 09:12