根据数组数据动态生成手风琴 [英] Dynamically generate an accordion based on array data

查看:94
本文介绍了根据数组数据动态生成手风琴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读mysql很好以获取给定特定ID的一组数据,这将数组返回到我的脚本。多数民众赞成在容易。此处的示例存储在名为$ history

I am reading mysql fine to pull a set of data given a particular ID, this returns an array to my script. Thats the easy bit. Example of which is here an is stored in an array called $history

Array ( [serviceID] => 1 [VesselTag] => 1000001 [component] => Engine 1 [item] => Caterpillar [serial] => 123456 [comment] => Something in here like this [parts] => Oil [lat] => 50.38313740 [longitude] => -4.03461360 [engineer] => 27 [date] => 2019-05-30 19:25:56 ) 
Array ( [serviceID] => 2 [VesselTag] => 1000001 [component] => Engine 2 [item] => Caterpillar [serial] => 677889 [comment] => Did a full overhaul of top section replaced everything [parts] => everything [lat] => 50.38309180 [longitude] => -4.03468820 [engineer] => 27 [date] => 2019-05-30 19:27:29 ) 
Array ( [serviceID] => 3 [VesselTag] => 1000001 [component] => Engine 1 [item] => Caterpillar [serial] => 123456 [comment] => This seems quite usable [parts] => Oil [lat] => 50.38345892 [longitude] => -4.03475649 [engineer] => 27 [date] => 2019-05-30 19:29:23 ) 

我现在想做的是生成一个手风琴 card-body根据列表中的[component]对卡片进行分组。有时阵列可能有更多或更少,因此将显示0到7张卡[组件]。

What I would like to do is now generate an accordion 'card-body' grouping the cards based on [component]s from the list. Sometime the array may have more or less, so there is going to be anywhere from 0 to 7 cards [components] showing.

<div class="card">
  <div class="card-header">
    <a class="card-link" data-toggle="collapse" href="#collapseOne">
        $array[Component]  **<--- Guessing this would be from a ```foreach``` or ```while``` loop???**
    </a>
  </div>
  <div id="collapseOne" class="collapse" data-parent="#accordion">
    <div class="card-body">
      <?php **THIS IS WHERE I WILL DISPLAY THE ROWS OF DATA ASSOCIATED TO THE [component] IN QUESTION ?>**
    </div>
  </div>
</div>

我真的很难理解要使用哪个循环并能够驱动它

I am just really struggling getting my head around which loop to use, and to be able to drive it by the [components].

它在尽我的力量……

推荐答案

经过一段时间的研究,我似乎想出了一些行之有效的方法.....
我认为这是相对有效的,但是很高兴有人告诉我。 ...

After quite some time looking at different articles I seem to have come up with something that works..... I think it is relatively efficient, but happy for someone to tell me otherwise....

<div id="accordion">
    <?php $newArray=array();
            foreach($history as $val){
                $key=$val['component'];
                $grouped[$key][]=$val;
            }
            foreach($grouped as $group){?>
                <div class="card">
                    <div class="card-header">
                        <a class="card-link" data-toggle="collapse" href="#<?php echo str_replace(' ', '',$group[0]['component']); ?>">
                            <?php echo $group[0]['component']; ?>
                        </a>
                    </div>
                    <div id="<?php echo str_replace(' ', '',$group[0]['component']); ?>" class="collapse" data-parent="#accordion">
                        <div class="card-body">
                            <?php foreach ($group as $occurance){
                                echo $occurance['date'] . ' - ' . $occurance['comment'] . ' - ' . $occurance['parts'] . ' - ' . $occurance['engineer'];
                                echo '<br>';
                                } ?>
                        </div>
                    </div>
                </div>
        <?php }?>
</div>

这篇关于根据数组数据动态生成手风琴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆