我需要在html中显示put值,但是dublicates存在问题。当我循环遍历xml并插入区域时,所有区域都会出现。但我需要删除相同的区域并留下一个。
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title">
<?php foreach ($list as $record): ?>
<a role="button" data-toggle="collapse" data-parent="#accordion"
href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
<?php echo $record->district; ?>
</a>
<?php endforeach; ?>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in"role="tabpanel"
aria-labelledby="headingOne">
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>Address</th>
<th>Phone</th>
<th>work Hours</th>
</tr>
</thead>
<tbody>
<tr>
<td> **<address>** </td>
<td> **<tel_num>** </td>
<td> **<work_range>** </td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
Store.xml这个文件中有很多记录,这里有一个:
<?xml version="1.0" encoding="utf-8"?>
<Regions>
<m>
<region>თბილისი</region>
<district>ვაკე</district>
<name>ჯიპისი 11(ვაკე)</name>
<address>თბილისი, ჭავჭავაძის გამზ. #50</address>
<coord>41.71,44.7642</coord>
<tel_num>5 95 22 88 86</tel_num>
<work_range>24 საათი</work_range>
<saw>140</saw>
<dr>2018-02-20T12:36:00+04:00</dr>
<exp_kl>false</exp_kl>
</m>
模型/控制器
public function getStores(){
$xml = simplexml_load_file('stores.xml');
$list = $xml->m;
return $list;
}
public function listAction(){
$list = $this->model->getStores();
$vars = [
'list' => $list,
];
$this->view->render('stores', $vars);
}
这是商店列表是手风琴标题,当你点击一个商店列表出现在这个区
输出应如下所示:
1)
- 地址
- 电话
- 工作时间
2)
- 地址
- 电话
- 工作时间
3)
- 地址
- 电话
- 工作时间
1)
- 地址
- 电话
- 工作时间
2)
- 地址
- 电话
- 工作时间
3)
- 地址
- 电话
- 工作时间
1)
- 地址
- 电话
- 工作时间
2)
- 地址
- 电话
- 工作时间
3)
- 地址
- 电话
- 工作时间
答案 0 :(得分:2)
在您需要找到$ list_unique_district的唯一区域列表之前,如下所示:
public function getUniqueList() {
$temp = [];
foreach ($this->model->getStores() as $record) {
$temp[] = $record->district;
}
return array_unique($temp);
}
public function listAction(){
$list = $this->model->getStores();
$unique = $this->model->getUniqueList();
$vars = [
'list_unique_district' => $unique,
'list' => $list,
];
$this->view->render('stores', $vars);
}
在该循环之后,所有区域和打印表格行的位置都会遍历原始列表“m”中的每个元素,并检查其区域是否与当前处理的区域相同。
<?php foreach ($list_unique_district as $current_district): ?>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title">
<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
<?php echo $current_district; ?>
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>Address</th>
<th>Phone</th>
<th>work Hours</th>
</tr>
</thead>
<tbody>
<?php foreach ($list as $record): ?>
<?php if ($list->district == $current_district ?>
<tr>
<td> <?php $record->address; ?> </td>
<td> <?php $record->tel_num; ?> </td>
<td> <?php $record->work_range; ?> </td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endforeach; ?>