简体   繁体   中英

Multiple foreach loops statements

How do I handle using multiple foreach loops? However, it gives 16 rows when only it should give 4 - since it repeats the entries many times over. In few words, I read from MySQL all subcategories and mark which have been previously checked by checkbox and then update all.

What am I doing wrong here - any help is appreciated.Thanks.

Currently I have:

<div class="form-group">
    <div class="col-xm-12">
        <div class="table-responsive">
            <table class="table table-bordered table-striped" role="grid">
                <tbody>
                <tr>
                    <td>
                        <?php $valuesub = ($page->subcat_recip_id);
                        $array_of_values = explode(",", $valuesub);

                        foreach ($menu_links as $item):

                        if ($item['parent_id'] != "0" && $item['subcat_recip_id'] == "0"):?>
                        <?php foreach($array_of_values as $dt):  ?>

                        <input type="checkbox" name="subcat_recip_id" class="square-purple"
                               value="<?php echo html_escape($item["title"]); ?>" <?=(in_array($dt, $item)) ? "CHECKED" : ""?>> <?=html_escape($item["title"]);  ?>
                        &nbsp;
                        <?php  endforeach;endif;endforeach; ?>

                    </td><?php echo html_escape($valuesub); ?>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

An example array $array_of_values contains:

array(4) { [0]=> string(10) "Appetizers" [1]=> string(9) "Beverages" [2]=> string(7) "Dessert" [3]=> string(5) "Bread" } 

aboot $dt must be loop in input checkbox, look this code:

<td> 
<?php foreach ($menu_links as $item) :
$valuesub = explode(",",$page->subcat_recip_id); ?>
<input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>" <?php foreach($valuesub as $dt): echo ($dt==$item['title'])?"CHECKED":""; endforeach;?> > <?=html_escape($item["title"]);?>&nbsp;
<?php endforeach; ?> 
</td>

thanks

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