PHP foreach从数组创建菜单 [英] PHP foreach create menu from array
问题描述
这是在我的模型文件中:
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();
返回$ menu_items;
$ / code $ / pre
在前端我有:
<?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; ?>
并且返回:
非洲
南非
非洲
莫桑比克
或者如果我打印出这个数组:
$ b $数组([0] => stdClass对象([country_name] =>南非[country_code] => ZA [continent_name] => Africa)[1] b => stdClass Object([country_name] => Mozambique [country_code] => MZ [continent_name] => Africa))1
现在最后一个问题,我将如何分类,以便非洲( continent_name
)不被重复,而是让所有拥有非洲的大陆名称列表在它下面?
请记住北美等也将进来玩。
汇总的问题 - >如何排序国家下面的联合大陆喜欢数组。
任何帮助非常感谢。
我会如何分类这一切
解决方案简单解决方法是通过 ORDER
查询结果,然后在你的foreach循环中添加一些逻辑
$ query-> order('a.continent_name ASC,t.country_name ASC') ;
在您的foreach循环中记住前一个大陆是什么
<?php
$ lastcontinent =;
foreach($ menus = $ k => $ menu):
if($ lastcontinent!= $ menu-> continent_name){
if($ k> 0){/ /如果这不是第一个大陆
//为前一个大陆关闭标签
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> <! - 尚未关闭的最后一个大陆的结束标记 - >
I am currently creating a joomla component, and i am currently stuck with the menu part.
this is in my model file:
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;
}
and on the front end i have:
<?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; ?>
and that returns:
Africa
South Africa
Africa
Mozambique
or if i print out the array 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
Now Finally the question, how would i sort it so that Africa (continent_name
) not be repeated but rather have all the countries that have the continent_name of Africa list underneath it?
Keep in mind North America and such will also come in to play..
Summarized question -> How would I sort countries underneath there associated continents fond in the array.
Any Help Greatly Appreciated thanks.
how would i sort it that everything
解决方案 The easy fix is by ORDER
ing your query results and then add some logic in your foreach loop
$query->order('a.continent_name ASC, t.country_name ASC');
In your foreach loop remember what the previous continent was
<?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 -->
这篇关于PHP foreach从数组创建菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文