-1

我需要用我的 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>

随机显示四个列表项

4

5 回答 5

2

您没有在任何地方使用数组中的 img 。

于 2013-08-13T07:14:46.013 回答
0
change your function to the and make sure to check the folder where the images are located

function HTMLMenu($menu, $parentid = 0)  {


$results = "";  
$results .= "<div id=\"recommended-links\">\n";
$results .= "<ul>\n";

foreach ($menu as $item) 
   {


       if ($item["parentid"] == $parentid) 
       {
        $results .= "<li>\n";
        $results .= "<img src='".$item["img"]."' alt='' title='' class='imagecache' height='104' width='102'>\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;

}

于 2013-08-13T07:30:05.820 回答
0

只需将您的 foreach 更改为 for 循环,就像这样

此循环将运行 4 次,并将从$menu数组中获取随机项

<?php
function HTMLMenu($menu, $parentid = 0)
{
   $results = "";

   if ($menu.count() > 0) {
   $results .= "<div id=\"recommended-links\">\n";
   $results .= "<p>Recommended</p><ul>\n";
   $menu_keys = array_rand($menu, 4);
   for ($i=0; $i<4; $i++)
   {
        $item = $menu[$menu_keys[$i]];
        $results .= "<li>\n";
        $results .= "<img src='".$item["img"]."' alt='' title='' class='imagecache' height='104' width='102'>\n";
        $results .= "<p>\n";
        $results .= "<a href='".$item["url"]."'>".$item["title"]."</a>\n";
        $results .= "</p>\n";
        $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);
?> 

注意:我已删除该行$results .= HTMLMenu($menu, $item["id"])。不需要那条线来获得 4 个随机项目

于 2013-08-13T07:35:28.040 回答
0
<?php
function HTMLMenu($menu, $parentid = 0)
{
   $results = "";

   if (count($menu) > 0) {
   $results .= "<div id=\"recommended-links\">\n";
   $results .= "<p>Recommended</p><ul>\n";
    $_menu = array_rand($menu, 4);
   foreach ($_menu as $item)
   {
        $results .= "<li>\n";
        $results .= "<img src='".$menu[$item]["img"]."' alt='' title='' class='imagecache' height='104' width='102'>\n";
        $results .= "<p>\n";
        $results .= "<a href='".$menu[$item]["url"]."'>".$menu[$item]["title"]."</a>\n";
        $results .= "</p>\n";
        $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);
?>  

现在这应该生成随机列表项。

于 2013-08-13T07:16:39.163 回答
0

对于图像部分,检查它们是否在正确的目录中(在您的情况下是您的文档根目录)

对于第二种情况,如果您只需要 4 个数组条目,您可以从数组中删除其余条目

或者您可以使用For 循环 insted 来foreach循环访问仅四个项目。

于 2013-08-13T07:16:50.483 回答