<?php // 获取多值字段数据,$navinfor为帝国CMS内置信息数组 $facility_data = $navinfor['facility']; // 初始化配套数据数组,按分类存储 $facility_arr = array( '教育配套' => array(), '医疗配套' => array(), '商业配套' => array() );
// 解析多值字段数据 if (!empty($facility_data)) { // 按行分割多值字段(帝国CMS多值字段每行一条数据) $facility_lines = explode("\n", $facility_data); foreach ($facility_lines as $line) { // 去除空行和首尾空格 $line = trim($line); if (empty($line)) continue; // 按分隔符分割字段(这里用|分隔,需和后台录入格式一致) $field = explode("|", $line); // 数据验证 if (count($field) >= 3) { $type = trim($field[0]); // 配套分类 $name = trim($field[1]); // 名称 $distance = trim($field[2]);// 距离 $remark = ''; // 备注(可选) if (isset($field[3])) $remark = trim($field[3]); // 将数据存入对应分类数组 if (isset($facility_arr[$type])) { $facility_arr[$type][] = array( 'name' => $name, 'distance' => $distance, 'remark' => $remark ); } } } } ?>
<!-- 周边配套 --> <div class="tab-pane fade" id="facility"> <?php foreach ($facility_arr as $type_name => $items): ?> <?php if (!empty($items)): // 只显示有数据的分类 ?> <h4 class="mt-3"><?php echo $type_name; ?></h4> <div class="facility-list"> <?php foreach ($items as $item): ?> <div class="facility-item"> <h6><?php echo $item['name']; ?></h6> <p class="text-muted mb-1">距离:<?php echo $item['distance']; ?></p> <?php if (!empty($item['remark'])): ?> <small><?php echo $item['remark']; ?></small> <?php endif; ?> </div> <?php endforeach; ?> </div> <?php endif; ?> <?php endforeach; ?> </div>
|