【问题标题】:Dynamically generate an accordion based on array data根据数组数据动态生成手风琴
【发布时间】:2019-10-17 03:04:28
【问题描述】:

我正在阅读 mysql 以提取给定特定 ID 的一组数据,这将一个数组返回到我的脚本。那是容易的一点。此处的示例存储在一个名为 $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 ) 

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

<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>

我真的很难弄清楚要使用哪个循环,并且能够通过 [组件] 来驱动它。

它让我头疼......

【问题讨论】:

  • 我可能应该补充一点,我正在尝试这样做而不需要对数据库进行大量调用...想法是提取特定“VesselID”的所有历史记录,然后根据这些记录中的组件。然后每个手风琴将列出特定手风琴[组件]的这些记录
  • 试试这个应该可以把卡包在手风琴中 div

标签: php arrays dynamic bootstrap-4 accordion


【解决方案1】:

看了很长一段时间的不同文章后,我似乎想出了一些可行的方法..... 我认为这是相对有效的,但很高兴有人告诉我其他情况......

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    相关资源
    最近更新 更多