使用foreach [duplicate]创建动态链接列表
问题描述:
This question already has an answer here:
- The 3 different equals 5 answers
I got a sitemap in my footer that links to all my pages, some pages have different urls because of a rewrite. For example, pages with id 14 have /projects/ before them, and pages with id 16 have /actueel/. How can I check on that and build the list?
This doesn't work (it adds projects to the entire list):
<?
foreach($menufootercr as $menufooterlist){
if($menufooterlist['id'] != ''){
if($menufooterlist['id'] = '14'){
$menufooteroverzicht .= '<li class="menu-item"><a href="projects/'.$menufooterlist['alias'].'.html">'.$menufooterlist['title'].'</a></li>';
}else if($menufooterlist['id'] = '16'){
$menufooteroverzicht .= '<li class="menu-item"><a href="actueel/'.$menufooterlist['alias'].'.html">'.$menufooterlist['title'].'</a></li>';
}else{
$menufooteroverzicht .= '<li class="menu-item"><a href="info/'.$menufooterlist['alias'].'.html">'.$menufooterlist['title'].'</a></li>';
}
}
}
echo $menufooteroverzicht;
?>
</div>
此问题已经存在 这里有一个答案: p>
-
3个不同的等于
5 answers
span>
li> \ r
ul>
div>
我的页脚中有一个站点地图链接到我的所有页面,有些页面由于重写而有不同的URL。 例如,id为14的页面前面有/ projects /,id为16的页面有/ actueel /。 如何检查并构建列表? p>
这不起作用(它将项目添加到整个列表中): p>
&lt;? foreach($ menufootercr as $ menufooterlist){ if($ menufooterlist ['id']!=''){ if($ menufooterlist ['id'] ='14'){ $ menufooteroverzicht。='&lt; li class =“menu-item”&gt;&lt; a href =“projects /'.$ menufooterlist ['alias']。'。html”&gt;'。$ menufooterlist ['title'] 。'&lt; / a&gt;&lt; / li&gt;'; } else if($ menufooterlist ['id'] ='16'){ $ menufooteroverzicht。='&lt; li class =“menu-item” &gt;&lt; a href =“actueel /'.$ menufooterlist ['alias']。'。html”&gt;'。$ menufooterlist ['title']。'&lt; / a&gt;&lt; / li&gt;'; \ n} else { $ menufooteroverzicht。='&lt; li class =“menu-item”&gt;&lt; a href =“info /'.$ menufooterlist ['alias']。'。html”&gt;'。$ menufooterlist ['title']。'&lt; / a&gt;&lt; / li&gt;'; } } } echo $ menufooteroverzicht; ?&gt; code> pre> \ n div>
答
Change
if($menufooterlist['id'] = '14'){
to
if($menufooterlist['id'] == '14'){
Do the same for 16. == is used for comparison not =