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 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档