doujishao8793 2015-10-22 21:22
浏览 12
已采纳

带有optlabels的动态php循环

trying to create a select list with labels. Everything with 0 belongs in the label tag with its child of listed below. Here is how my table is set up enter image description here

and the code I have which I know already is wrong

    $query = sprintf("SELECT * FROM `itemCat`");


$label = "";
while($row = $this->localDB->fetch_assoc($result))
{
    if ($row['itemCatName'] != $label && $row['itemCatChildof']=='0')
    {
    $cat .= '<optgroup label="'.$row['itemCatName'].'">';   
    }

    $cat .= '<option value="' . $row['iditemCat'] . '">' . $row['itemCatName'] . '</option>';
    $label =  $row['itemCatName'];
}
return $cat;
}
  • 写回答

2条回答 默认 最新

  • dougou8639 2015-10-22 21:45
    关注

    I would personnaly go for something like this : Creating a new sorted array where each parent element contain all his children in a sub array

    <?php
    
    $data = 
    [
        ['iditemCat' => 1, 'itemCatName' => 'Admin', 'itemCatChildof' => 0],
        ['iditemCat' => 2, 'itemCatName' => 'Admin2', 'itemCatChildof' => 1],
        ['iditemCat' => 3, 'itemCatName' => 'Admin3', 'itemCatChildof' => 1],
        ['iditemCat' => 4, 'itemCatName' => 'Admin4', 'itemCatChildof' => 1],
        ['iditemCat' => 5, 'itemCatName' => 'Admin5', 'itemCatChildof' => 0],
        ['iditemCat' => 6, 'itemCatName' => 'Admin6', 'itemCatChildof' => 5],
        ['iditemCat' => 7, 'itemCatName' => 'Admin7', 'itemCatChildof' => 5],
        ['iditemCat' => 8, 'itemCatName' => 'Admin8', 'itemCatChildof' => 5]
    ];
    
    $sortedArray = [];
    
    foreach($data as $d) {
        if($d['itemCatChildof'] == 0) {
            $sortedArray[$d['iditemCat']] = $d;
        } else {
            $sortedArray[$d['itemCatChildof']]['children'][] = $d;
        }
    }
    

    It will return you something like this:

    Array
    (
        [1] => Array
            (
                [iditemCat] => 1
                [itemCatName] => Admin
                [itemCatChildof] => 0
                [children] => Array
                    (
                        [0] => Array
                            (
                                [iditemCat] => 2
                                [itemCatName] => Admin2
                                [itemCatChildof] => 1
                            )
    
                        [1] => Array
                            (
                                [iditemCat] => 3
                                [itemCatName] => Admin3
                                [itemCatChildof] => 1
                            )
    
                        [2] => Array
                            (
                                [iditemCat] => 4
                                [itemCatName] => Admin4
                                [itemCatChildof] => 1
                            )
    
                    )
    
            )
    
        [5] => Array
            (
                [iditemCat] => 5
                [itemCatName] => Admin5
                [itemCatChildof] => 0
                [children] => Array
                    (
                        [0] => Array
                            (
                                [iditemCat] => 6
                                [itemCatName] => Admin6
                                [itemCatChildof] => 5
                            )
    
                        [1] => Array
                            (
                                [iditemCat] => 7
                                [itemCatName] => Admin7
                                [itemCatChildof] => 5
                            )
    
                        [2] => Array
                            (
                                [iditemCat] => 8
                                [itemCatName] => Admin8
                                [itemCatChildof] => 5
                            )
    
                    )
    
            )
    
    )
    

    And so you just have to explore it like this:

    <select name="foo" id="foo">
    <?php foreach($sortedArray as $value): ?>
        <optgroup label="<?= $value['itemCatName']; ?>">
            <?php foreach($value['children'] as $child): ?>
                <option value="<?= $child['iditemCat']; ?>"><?= $child['itemCatName']; ?></option>
            <?php endforeach; ?>
        </optgroup>
    <?php endforeach; ?>
    </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?