简体   繁体   English

如何跳过foreach循环中的第一个实例并将其限制为8?

[英]How to skip the first instance in a foreach loop and limit to 8?

I was wondering if anyone could give me a hand with this... Basically I am trying to modernize the news system of my site but I can't seem to limit the amount of posts showing in the foreach loop that is on my blog part of the site. 我想知道是否有人可以帮我这个忙...基本上,我正在尝试现代化网站的新闻系统,但似乎无法限制博客部分的foreach循环中显示的帖子数量该网站。 I need to skip the first instance as it is already promoted at the top of the page. 我需要跳过第一个实例,因为它已在页面顶部升级。 I've tried various google searches but im getting results for C++ Perl and python, what is really irritating. 我已经尝试过各种Google搜索,但即时通讯会获得C ++ Perl和python的结果,这确实很令人讨厌。 I just need a simple PHP solution. 我只需要一个简单的PHP解决方案。 I'll pop my code below and see if anyone can help. 我将在下面弹出代码,看看是否有人可以提供帮助。 Thanks for any help in-advance. 感谢您的提前帮助。 And please remember to leave your responses as an answer so I can mark them up if they helped ;) 并且,请记住留下您的回答作为答案,以便我帮忙将其标记出来;)

<div class="view22 full" style="margin-top:0;">
    <h3>Recent News and Announcements</h3>
    <?php foreach ($articles as $article) { 
?>
        <div class="ah7_ clearfix">
            <p class="date"><?php echo date('F j', $article['article_timestamp']); ?>, <?php echo date('Y', $article['article_timestamp']); ?></p>
            <h3><a href="<?php echo $url.'/newsroom/'.$article['article_id']; ?>"><?php echo $article['article_title']; ?></a></h3>
        </div>
    <?php 
    }
?>
</div>

I assume that the $articles array has keys starting with 0. How about modifying the loop like this: 我假定$ articles数组具有以0开头的键。如何像这样修改循环:

foreach ($articles as $key => $article) 

and checking if $key is 0 at the beginning? 并检查$ key开头是否为0?

if($key == 0)
   continue;

If the array keys are different: Create a new variable $i, set it to 0 and increase the value by 1 in every foreach loop iteration. 如果数组键不同:创建一个新变量$ i,将其设置为0,并在每次foreach循环迭代中将其值增加1。

$i = 0;
foreach ($articles as  $article) {
   $i++;
   if($i == 1)
      continue;
   elseif($i > 8)
      break;

   //the other code goes here
}

In case it is based on a SQL query, using "limit" might help to reduce load! 如果它基于SQL查询,则使用“ limit”可能有助于减少负载!

To remove the first instance you can manually unset the item ($articles[0]) after making a copy of it or printing it as a featured news. 要删除第一个实例,您可以在复制项目或将其打印为特色新闻后手动取消设置该项目($ articles [0])。

To limit the number of post you can use the mysql LIMIT Clause; 要限制发布数量,您可以使用mysql LIMIT子句;

Or you can do something like this 或者你可以做这样的事情

foreach($articles as $key => $article){
    if($key===0)
        continue;
    if($key===8)
        break;
    echo $article;// or_do_whatever_youwant_with($article);
}

There are a few things you can do: 您可以做几件事:

  1. If you $articles is an array of array, having continous indexes, use a for loop instead of foreach and do something like 如果$ articles是一个数组数组,具有连续索引,请使用for循环而不是foreach并执行类似的操作

     for ($i = 1; $i < 8 : $i++ ) { // and access it like $articles[$i]['some_index'] ... } 
  2. If it is not then you can use an external counter Say 如果不是,则可以使用外部计数器说

      $counter = -1; foreach ( $articles as $article) { $counter++; if (!$counter) continue; if ($counter > 7 ) break; ...// your code // } 
  3. You can change your Mysql query to give you only the desired data, using LIMIT and OFFSET 您可以使用LIMIT和OFFSET将Mysql查询更改为仅提供所需数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM