Før du opbygger listen, bør du gruppere dem i overensstemmelse hermed først (som din struktur viser), derefter kan du bygge listen. Overvej dette eksempel:
<?php
$original_data = array();
$link = new MySQLI('localhost', 'username', 'password', 'database');
// normal select
$query = mysqli_query($link, 'SELECT * FROM Sheet1 order by country, state, city');
while($row = $query->fetch_assoc()) {
$original_data[] = $row;
}
$ordered_data = array();
foreach($original_data as $key => $value) {
// group them
$ordered_data[$value['country']][$value['state']][$value['city']][] = $value;
}
?>
<!-- print them accordingly -->
<?php foreach($ordered_data as $country => $state_values): ?>
<ul>
<li><?php echo $country; ?></li>
<?php foreach($state_values as $state => $city_values): ?>
<ul>
<li><?php echo $state; ?></li>
<ul>
<?php foreach($city_values as $city => $value): ?>
<li><?php echo $city; ?></li>
<ul>
<?php foreach($value as $index => $element): ?>
<li><?php echo $element['Business'] . ' | ' . $element['Phone']; ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</ul>
</ul>
<?php endforeach; ?></li>
</ul>
<?php endforeach; ?>