dongyuan9149 2014-02-13 20:53
浏览 46
已采纳

在两个单独的foreach循环中分离变量的内容,所有这些都在一个函数中

I have two foreach loops in my function, each use the $newlist data passed to it, the first foreach loop, retrieves only one item from the database. The second foreach loop should/ought to independently retrieve separate three news items, however that is not the case, as in the html page, it only shows the first item retrieved in the first foreach loop three times, instead of three different items. How can I avoid this to achieve what I want?

function getNewsHTMLStructureIsland($newslist = array())
    {
        $html = '';
        if (empty($newslist))
            return NULL;
        $html .= '<div role="main" class="fluid flush split homepage">';
        $html .= '<div class="lc flush lc-island">';
        $html .= '<div class="l-two-col">';
        $html .= '<div class="l-main-container">';
        $html .= '<div class="l-main">';

        //<!-- Begin: Channel Archive Page - Primary Island -->
        $html .= '<div class="island plain-island">';
        foreach (array_slice($newslist, 0, 1) as $newslistarticle) {
        $html .= '<div class="plain-feature block block-inset">';

        $html .= '<a href="'. site_url() .'news/'.$newslistarticle->slug.' " data-omni-sm="hp_featureddl">';
        $html .= '<img class="thumb" data-aspect-ratio="500x369" height="369" width="500" alt="'.$newslistarticle->title.'" src="" />';
        $html .= '<div class="block-title">';
        $html .= '<h2>'.$newslistarticle->title.'</h2>';
        $html .= '<div class="byline">by Alexia Tsotsis</div>';
        $html .= '</div>';
        $html .= '</a>';
        $html .= '</div>';
        }


        $html .= '<ul class="plain-item-list">';
        foreach (array_slice($newslist, 1, 3) as $newsarticle) {
            //print_r($tweet);
            $html .= '<li class="plain-item block block-small">';
            $html .= '<a href="'. site_url() .'news/'.$newslistarticle->slug.' " data-omni-sm="hp_featureddl">';
            $html .= '<img class="thumb" data-featured-thumb="1" height="90" width="145" alt="" src="" />';
            $html .= '<div class="plain-title">';
            $html .= '<h2 class="h-alt">'.$newslistarticle->title.'</h2>';
            $html .= '<p class="byline">by Jordan Crook</p>';
            $html .= '</div>';
            $html .= '</a>';
            $html .= '</li>';

        }

            $html .= '</ul>';
            $html .= '</div>';
            ////<!-- End: Channel Archive Page - Primary Island -->
        return $html;
    }
}
  • 写回答

2条回答 默认 最新

  • dpe77294 2014-02-13 21:23
    关注

    Working solution also,is I realise I should have used $newsarticle->title instead of $newslistarticle->title , this separates the contents of the two foreachloop. The new function contains

    function getNewsHTMLStructureIsland($newslist = array())
        {
            $html = '';
            if (empty($newslist))
                return NULL;
            $html .= '<div role="main" class="fluid flush split homepage">';
            $html .= '<div class="lc flush lc-island">';
            $html .= '<div class="l-two-col">';
            $html .= '<div class="l-main-container">';
            $html .= '<div class="l-main">';
    
            //<!-- Begin: Channel Archive Page - Primary Island -->
            $html .= '<div class="island plain-island">';
            $newslistarticle = $newslist[0];
            $html .= '<div class="plain-feature block block-inset">';
    
            $html .= '<a href="'. site_url() .'news/'.$newslistarticle->slug.' " data-omni-sm="hp_featureddl">';
            $html .= '<img class="thumb" data-aspect-ratio="500x369" height="369" width="500" alt="'.$newslistarticle->title.'" src="" />';
            $html .= '<div class="block-title">';
            $html .= '<h2>'.$newslistarticle->title.'</h2>';
            $html .= '<div class="byline">by Alexia Tsotsis</div>';
            $html .= '</div>';
            $html .= '</a>';
            $html .= '</div>';
    
    
    
            $html .= '<ul class="plain-item-list">';
            foreach (array_slice($newslist, 1, 3) as $newsarticle) {
                //print_r($tweet);
                $html .= '<li class="plain-item block block-small">';
                $html .= '<a href="'. site_url() .'news/'.$newsarticle->slug.' " data-omni-sm="hp_featureddl">';
                $html .= '<img class="thumb" data-featured-thumb="1" height="90" width="145" alt="" src="" />';
                $html .= '<div class="plain-title">';
                $html .= '<h2 class="h-alt">'.$newsarticle->title.'</h2>';
                $html .= '<p class="byline">by Jordan Crook</p>';
                $html .= '</div>';
                $html .= '</a>';
                $html .= '</li>';
    
            }
    
                $html .= '</ul>';
                $html .= '</div>';
                ////<!-- End: Channel Archive Page - Primary Island -->
            return $html;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器