我有一个样本数据:
mobile(id, name, os)
(1, "samsung 1", "android")
(2, "samsung 2", "android")
(3, "iPhone 3", "OS")
(4, "iPhone 4", "OS")
(5, "samsung 3", "android")
并查询:
$db =& JFactory::getDBO();
$query = "SELECT id, name, os
FROM `mobile` order by os";
$db->setQuery($query);
$rows = $db->loadObjectList();
$i=0;
$data = array();
foreach ($rows as $row) {
if(empty($data[$row->os])) {
$data[$row->os] = array();
}
if(empty($data[$row->os][$row->name])) {
$data[$row->os][$row->name] = array();
}
$data[$row->os][$row->name] = $row->id;
}
<?php foreach ($data as $os => $names) {?>
<li>
<?php echo $os; ?>
<ul>
<?php
foreach ($names as $name => $id) {
?>
<li>
<?php echo $name; ?>
</li>
<?php
}
?>
</ul>
</li>
<?php $i++; }?>
当我运行代码时,结果不会显示在此:
android
-samsung 1
-samsung 2
-samsung 3
OS
-iPhone 3
-iPhone 4
如何解决?