Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php foreach循环问题_Php_Html_Foreach - Fatal编程技术网

php foreach循环问题

php foreach循环问题,php,html,foreach,Php,Html,Foreach,我有这个函数来生成动态html菜单 我的问题是当每个循环上的计数超过5 ul时 我想知道如果数字超过5,ul标签只出现一次,我该怎么办 function GenerateNavHTML($rows ,$count=5) { $html = ''; $html .= '<div>'; foreach($rows as $key =>$value) { if(count($ro

我有这个函数来生成动态html菜单 我的问题是当每个循环上的计数超过5 ul时 我想知道如果数字超过5,ul标签只出现一次,我该怎么办

     function GenerateNavHTML($rows ,$count=5)
    {
        $html = '';
        $html .= '<div>';
        foreach($rows as $key =>$value)
        {
            if(count($rows) > $count)
            {
                 $html .= '<ul>';
                 $html .='<li>';
                 $html .= '<a href="' . $value['title'] . '">' . $value['title'] . '</a>';
                 $html .= '</li>';
                 $html .= '</ul>';
            }
            else
            {
                 $html .='<li>';
                 $html .= '<a href="' . $value['title'] . '">' . $value['title'] . '</a>';
                 $html .= '</li>';
            }


        }

        $html .= '</div>';
        return $html;
    }

    this the structure of html code 
                           <div>

                                   <ul>
                                        <li><a href="#"> 1</a></li>
                                    </ul>

                                    <ul>
                                        <li><a href="#"> 1</a></li>
                                    </ul>

                                </div>
函数GenerateNavHTML($rows,$count=5)
{
$html='';
$html.='';
foreach($key=>$value的行)
{
如果(计数($rows)>$count)
{
$html.='
    '; $html.='
  • '; $html.=''; $html.='
  • '; $html.='
'; } 其他的 { $html.='
  • '; $html.=''; $html.='
  • '; } } $html.=''; 返回$html; } 这是html代码的结构

    如果我理解正确,应该这样做:

    foreach($rows as $key =>$value){
        if(count($rows) > $count){
            $html .= '<ul>';
        }
    
        $html .='<li>';
        $html .= '<a href="' . $value['title'] . '">' . $value['title'] . '</a>';
        $html .= '</li>';
    
        if(count($rows) > $count){
            $html .= '</ul>';
        }
    }
    
    foreach($key=>$value的行){
    如果(计数($rows)>$count){
    $html.='
      '; } $html.='
    • '; $html.=''; $html.='
    • '; 如果(计数($rows)>$count){ $html.='
    '; } }
    只需将不希望重复的部分移动到foreach循环之外,即

    function GenerateNavHTML($rows ,$count=5)
    {
        $html .= '<div><ul>';
        ...
    
    函数GenerateNavHTML($rows,$count=5)
    {
    $html.='
      '; ...

    在循环内部,您可以从
  • 开始,
  • 只需将您的UL重新定位到每个外部

    function GenerateNavHTML($rows ,$count=5)
    {
        $html = '';
        $html .= '<div><ul>';
        $ctr = 0;
        foreach($rows as $key =>$value)
        { 
            if(!$ctr)
                 $html .= "<ul>";
    
            $html .='<li>';
            $html .= '<a href="' . $value['title'] . '">' . $value['title'] . '</a>';
            $html .= '</li>';              
            $ctr++;
    
            if($ctr == $count)
            {
                $html .= "</ul>";
                $ctr = 0;
            }
    
    
        }
        if($ctr > $count)
         $html .= '</ul>';
    
        $html .= '</div>';
        return $html;
    }
    
    函数GenerateNavHTML($rows,$count=5)
    {
    $html='';
    $html.='
      '; $ctr=0; foreach($key=>$value的行) { 如果(!$ctr) $html.=“
        ”; $html.='
      • '; $html.=''; $html.='
      • '; $ctr++; 如果($ctr==$count) { $html.=“
      ”; $ctr=0; } } 如果($ctr>$count) $html.='
    '; $html.=''; 返回$html; }
    在何处以及如何调用函数?放在那里
    if($key>$count)
    在同一页面中调用函数echo GenerateNavHTML($rows);如果我按照你说的做,所有菜单都在同一个容器中,我想在ul内每隔5 li调用一次