Search code examples
phpxmlsimplexml

How to to insert XML values to html with php


I need to make put values as shown in html but there is problem with dublicates. when i loop through xml and insert district all districts appear. but i need to remove same districts and leave one.

 <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 there are many records in this file here is one:

    <?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);
}

this is list of stores is accordion header and when you click one the list of stores appear in this district

the output should look like this:

  1. District 1

1)
- Address - Phone - Work Hours
2)
- Address - Phone - Work Hours
3)
- Address - Phone - Work Hours

  1. District 2

1)
- Address - Phone - Work Hours
2)
- Address - Phone - Work Hours
3)
- Address - Phone - Work Hours

  1. District 3

1)
- Address - Phone - Work Hours
2)
- Address - Phone - Work Hours
3)
- Address - Phone - Work Hours


Solution

  • Before you need to find a list of unique districts $list_unique_district as follows:

    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);
    }
    

    After that loop all districts and where you print the table rows go through every element in the original list of "m" and check if its district is equal the currently processed one.

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