Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

php - print values using for loop within foreach loop

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

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

1 Reply

0 votes
by (71.8m points)

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*/ ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...