我目前正在创建一个joomla组件,但我目前被菜单部分卡住了。
这是在我的模型文件:
public function getMenu(){
$menu_id = JRequest::getInt('id');
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->select('t.country_name,t.country_code, a.continent_name');
$query->from('#__vectormap_countries AS t')
->join('LEFT', '#__vectormap_continents AS a USING(continent_id)')
->where('t.published = 1');
$db->setQuery($query);
$menu_items = $db->loadObjectList();
return $menu_items;
}
在前端我有:
<?php $menus = $this->menu ?>
<?php foreach($menus as $menu): ?>
<div><?php echo $menu->continent_name ?></div>
<li id="<?php echo $menu->country_code ?>"><?php echo $menu->country_name ?></li>
<br />
<?php endforeach; ?>
返回:
非洲南非
非洲莫桑比克或者如果我打印出数组this:
Array ( [0] => stdClass Object ( [country_name] => South Africa [country_code] => ZA [continent_name] => Africa ) [1] => stdClass Object ( [country_name] => Mozambique [country_code] => MZ [continent_name] => Africa ) ) 1
现在最后的问题是,我将如何排序,使非洲(continent_name
)不重复,而是有非洲的continent_name列表下面的所有国家?
请记住北美和这些地区也将发挥作用…
总结性问题->我将如何在数组中相关的大洲下面对国家进行排序?
任何帮助非常感激谢谢。
如果所有
简单的修复方法是通过ORDER
查询结果,然后在foreach循环中添加一些逻辑
$query->order('a.continent_name ASC, t.country_name ASC');
在foreach循环中记住前一个大陆是什么
<?php
$lastcontinent = "";
foreach($menus as $k => $menu):
if($lastcontinent != $menu->continent_name){
if($k > 0){ // if this isn't the first continent
// closing tags for the previous continent
echo '</ul></div>';
}
echo '<div>'.$menu->continent_name.'</div><ul>';
}
$lastcontinent = $menu->continent_name;
echo '<li id="'.$menu->country_code.'">'.$menu->country_name.'</li>';
endforeach;
?>
</ul></div> <!-- closing tags for the last continent which hasn't been closed yet -->
使用foreach循环创建一个新数组,并创建continent_name作为键。这样你就不会有重复的名字了