我需要用我的 php 代码输出以下 html;代码有效,但图像未呈现。我还想随机显示四个列表项,而不是全部。我需要您的帮助来重新构建代码和数组。请帮忙。
<?php
function HTMLMenu($menu, $parentid = 0)
{
$results = "";
if ($menu.count() > 0) {
$results .= "<div id=\"recommended-links\">\n";
$results .= "<ul>\n";
foreach ($menu as $item) if ($item["parentid"] == $parentid)
{
$results .= "<li>\n";
$results .= "<p>\n";
$results .= "<a href='".$item["url"]."'>".$item["title"]."</a>\n";
$results .= "</p>\n";
$results .= HTMLMenu($menu, $item["id"])
$results .= "</li>\n";
}
$results .= "</ul>\n";
$results .= "</div>\n";
}
return $results;
}
$menu = array(
'1' => array('id' => 1, 'parentid' => 0, 'title' => 'Apple', 'url' => 'url-1', 'img' => 'img-1'),
'2' => array('id' => 2, 'parentid' => 0, 'title' => 'Banana', 'url' => 'url-2', 'img' => 'img-2'),
'3' => array('id' => 3, 'parentid' => 0, 'title' => 'Tangerine','url' => 'url-3', 'img' => 'img-3'),
'4' => array('id' => 4, 'parentid' => 3, 'title' => 'Pear', 'url' => 'url-4', 'img' => 'img-4'),
'5' => array('id' => 5, 'parentid' => 4, 'title' => 'Walnut', 'url' => 'url-5', 'img' => 'img-5'),
'6' => array('id' => 6, 'parentid' => 4, 'title' => 'Ice Cream', 'url' => 'url-6', 'img' => 'img-6'),
'7' => array('id' => 7, 'parentid' => 4, 'title' => 'Candy', 'url' => 'url-7', 'img' => 'img-7'),
'8' => array('id' => 8, 'parentid' => 3, 'title' => 'Nectar', 'url' => 'url-8', 'img' => 'img-8'),
'9' => array('id' => 8, 'parentid' => 3, 'title' => 'Honey', 'url' => 'url-9', 'img' => 'img-9')
);
echo HTMLMenu($menu);
?>
所需的 html 显示
<div id="recommended-links">
<p>Recommended</p>
<ul>
<li>
<img src="image-1" alt="" title="" class="imagecache" height="104" width="102">
<p> <a href="url-1">Apple</a></p>
</li>
<li>
<img src="image-2" alt="" title="" class="imagecache" height="104" width="102">
<p> <a href="url-2">Banana</a></p>
</li>
<li>
<img src="image-3" alt="" title="" class="imagecache" height="104" width="102">
<p> <a href="url-3">Tangerine</a></p>
</li>
<li>
<img src="image-4" alt="" title="" class="imagecache" height="104" width="102">
<p> <a href="url-4">Pear</a></p>
</li>
</ul>
</div>
想随机显示四个列表项