如何使用 php 将 XML 值插入到 html
How to to insert XML values to html with php
我需要按照 html 中所示的方式设置 put 值,但是重复存在问题。当我遍历 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>
Model/Controller
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);
}
这是手风琴式的商店列表 header 当您单击其中一个时,商店列表会出现在该地区
输出应如下所示:
- 1区
1)
- 地址
- Phone
- 工作时间
2)
- 地址
- Phone
- 工作时间
3)
- 地址
- Phone
- 工作时间
- 2区
1)
- 地址
- Phone
- 工作时间
2)
- 地址
- Phone
- 工作时间
3)
- 地址
- Phone
- 工作时间
- 3区
1)
- 地址
- Phone
- 工作时间
2)
- 地址
- Phone
- 工作时间
3)
- 地址
- Phone
- 工作时间
之前你需要找到独特的地区列表$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);
}
循环之后,所有区域和打印 table 行的位置遍历 "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; ?>
我需要按照 html 中所示的方式设置 put 值,但是重复存在问题。当我遍历 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>
Model/Controller
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);
}
这是手风琴式的商店列表 header 当您单击其中一个时,商店列表会出现在该地区
输出应如下所示:
- 1区
1)
- 地址
- Phone
- 工作时间
2)
- 地址
- Phone
- 工作时间
3)
- 地址
- Phone
- 工作时间
- 2区
1)
- 地址
- Phone
- 工作时间
2)
- 地址
- Phone
- 工作时间
3)
- 地址
- Phone
- 工作时间
- 3区
1)
- 地址
- Phone
- 工作时间
2)
- 地址
- Phone
- 工作时间
3)
- 地址
- Phone
- 工作时间
之前你需要找到独特的地区列表$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);
}
循环之后,所有区域和打印 table 行的位置遍历 "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; ?>