Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am facing trouble in printing values using for loop within foreach loop. I am populating dropdown list using foreach loop by defining an array and then marking the values as selected which matches with values retrieved from database but the problem is values are repeating in last dropdown. Here is my code: First I am converting string values retrieved from database into an array. and the code and output is as follows:

foreach($officeDetails as $value){ 
   $str = $value['days'];  
   $arr = explode(", ", $str); 
}
print_r($arr);

Output is:

 Array ( 
        [0] => Monday 
        [1] => Tuesday 
      ) 
Array ( 
        [0] => Wednesday 
        [1] => Thursday 
        [2] => Friday 
      )

Now I am trying to populate a dropdown list using foreach loop with an array called $daysArr(see below in code) and marking the values as selected which are retrieved from database as shown above in an array called $arr. Code:

 foreach($officeDetails as $value){ 
   $str = $value['days'];  
   $arr = explode(", ", $str); 

<select id="dw" name="days[0][]" class="full-width select2-offscreen" data-init-plugin="select2" multiple="" tabindex="-1">
    <?php $daysArr = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

    foreach ($daysArr as $days) {
        for ($i = 0; $i < count($arr); $i++) {
            if ($days == $arr[$i]) {
                ?>
                <option value="<?php echo $days; ?>" selected><?php echo $days; ?></option>

            <?php } else { ?>
                <option value="<?php echo $days; ?>"><?php echo $days; ?></option>

            <?php } /* end else condition */
        }/* end for condition */
    } /*end foreach loop */
    }
    ?>

</select>

So the result is; two select boxes are printed, showing right selected values but the second select box is printing each value of "$days" three times like Monday is showing three times in the dropdown list.

I know its something minor that I am missing, please help me I am badly stuck in a loop and can't get out of it. Your suggestions are highly appreciated. Thanks in advance.

Kind Regards.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
150 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try like the following

foreach($officeDetails as $j => $value){ 
$str = $value['days'];  
$arr = explode(", ", $str);
<select id="dw" name="days[0][]" class="full-width select2-offscreen" data-init-plugin="select2" multiple="" tabindex="-1">
 <?php   $daysArr = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

        foreach($daysArr as $i => $days){


             if ($days == $arr[$i]) {
    ?>
<option value="<?php echo $days; ?>" selected><?php echo $days; ?></option>

<?php } else{ ?>
<option value="<?php echo $days; ?>" ><?php echo $days; ?></option>

<?php       } /* end else condition */
    } /*end foreach loop */
?>

</select>
<?php } /*end foreach loop*/ ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...