簡體   English   中英

如何使用php將XML值插入html

[英]How to to insert XML values to html with php

我需要制作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. 1區

1)
-地址-電話-工作時間
2)
-地址-電話-工作時間
3)
-地址-電話-工作時間

  1. 二區

1)
-地址-電話-工作時間
2)
-地址-電話-工作時間
3)
-地址-電話-工作時間

  1. 第三區

1)
-地址-電話-工作時間
2)
-地址-電話-工作時間
3)
-地址-電話-工作時間

在需要找到唯一區域列表$ 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; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM