Php 在两个单独的foreach循环中分离变量的内容,全部在一个函数中

Php 在两个单独的foreach循环中分离变量的内容,全部在一个函数中,php,Php,我的函数中有两个foreach循环,每个循环使用传递给它的$newlist数据,第一个foreach循环只从数据库中检索一项。第二个foreach循环应该/应该单独检索三个新闻项,但事实并非如此,因为在html页面中,它只显示在第一个foreach循环中检索到的第一个项三次,而不是三个不同的项。我如何避免这种情况以实现我的目标 function getNewsHTMLStructureIsland($newslist = array()) { $html = '';

我的函数中有两个foreach循环,每个循环使用传递给它的$newlist数据,第一个foreach循环只从数据库中检索一项。第二个foreach循环应该/应该单独检索三个新闻项,但事实并非如此,因为在html页面中,它只显示在第一个foreach循环中检索到的第一个项三次,而不是三个不同的项。我如何避免这种情况以实现我的目标

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;
    }
}
函数getNewsHTMLStructureIsland($newslist=array()) { $html=''; if(空($newslist)) 返回NULL; $html.=''; $html.=''; $html.=''; $html.=''; $html.=''; // $html.=''; foreach(数组_切片($newslist,0,1)作为$newslistarticle){ $html.=''; $html.=''; $html.=''; } $html.='
    ; foreach(数组_切片($newslist,1,3)作为$newsarticle){ //打印(tweet); $html.='
  • ; $html.=''; $html.='
  • '; } $html.='
'; $html.=''; //// 返回$html; } } 更换

foreach (array_slice($newslist, 0, 1) as $newslistarticle) {

删除关闭
}

替换

foreach (array_slice($newslist, 1, 3) as $newsarticle) {


对于($i=1;$i工作解决方案也是,我意识到我应该使用$newsarticle->title而不是$newslistarticle->title,这将两个foreachloop的内容分开。新函数包含

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;
    }
函数getNewsHTMLStructureIsland($newslist=array()) { $html=''; if(空($newslist)) 返回NULL; $html.=''; $html.=''; $html.=''; $html.=''; $html.=''; // $html.=''; $newslistarticle=$newslist[0]; $html.=''; $html.=''; $html.=''; $html.='
    ; foreach(数组_切片($newslist,1,3)作为$newsarticle){ //打印(tweet); $html.='
  • ; $html.=''; $html.='
  • '; } $html.='
'; $html.=''; //// 返回$html; }
Fantastic@cyadder,这很快就有帮助了
for($i=1; $i<=3; $i++) {
    $newsarticle = $newslist[$];
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;
    }