dongqie2010
2016-06-27 00:24
浏览 317
已采纳

指定从哪个索引开始在foreach循环中循环

The content if pulled from a while loop that I used to fetch a query. I need to sort the content from different starting point of the array, let's say results from indexes 5to 8 and ignore 0 to 4 or 9 to 12.

<?php
$i='0';
foreach($articulos as $articdos){
?>
<div class="cajon4centro">
<div class="">
  <a href="efecto.php?libelula=noticias&artic=<?= $articdos['id']; ?>&gen=<?= $articdos['genero']; ?>&id=<?= $articdos['id'] ?>">
    <div class="c4img">
    <img src="img/chica/<?= $articdos['foto']; ?>" alt="" />
    <div class="titC4">
      <?= $articdos['titulo']; ?>
    </div>
    </div>
  </a>
</div>
</div>
<?php
if ($i++ == 3) break;
} ?>

As it is right now it all starts from index 0 and I want it to start after the 6th index... Can this be done?

图片转代码服务由CSDN问答提供 功能建议

如果从我用于获取查询的while循环中提取内容。 我需要从数组的不同起点对内容进行排序,让我们说索引5到8的结果,忽略0到4或9到12。

 &lt;?php \  n $ i ='0'; 
foreach($ articulos as $ articdos){
?&gt; 
&lt; div class =“cajon4centro”&gt; 
&lt; div class =“”&gt; 
&lt; a href  =“efecto.php?libelula = noticias&amp; artic =&lt;?= $ articdos ['id'];?&gt;&amp; gen =&lt;?= $ articdos ['genero'];?&gt;&amp; id =  &lt;?= $ articdos ['id']?&gt;“&gt; 
&lt; div class =”c4img“&gt; 
&lt; img src =”img / chica /&lt;?= $ articdos ['foto  '];?&gt;“  alt =“”/&gt; 
&lt; div class =“titC4”&gt; 
&lt;?= $ articdos ['titulo'];  ?&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
&lt; / a&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
&lt;?php 
if($ i ++ == 3)  break; 
}?&gt; 
   
 
 

因为现在它全部从索引0开始,我希望它在第6个索引之后启动...可以 这样做了吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpan1416 2016-06-27 00:46
    已采纳

    Here's how you could do this using a counter:

    <?php
        $i = 0;
        foreach($articulos as $articdos){
            if ($i >= 5 && $i <= 8) {
    ?>
        //Your HTML code here
    <?php
            } // end if block
            $++i;
        } // end foreach loop
    ?>
    

    During the loops where the if statement is false, no HTML code will be executed or output, and unless you're looping through thousands of records, the time taken during those loops where nothing is done will be negligible.

    Instead of testing for a number, you could also test the data inside $articdos if you were only looking for specific records with specific data in them.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题