沃梦达 / 编程问答 / php问题 / 正文

PHP:如何确定循环的每 N 次迭代?

PHP: How do you determine every Nth iteration of a loop?(PHP:如何确定循环的每 N 次迭代?)

本文介绍了PHP:如何确定循环的每 N 次迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 XML 每隔 3 个帖子回显一个图像,这是我的代码:

I wanted to echo an image every after 3 post via XML here is my code :

<?php
// URL of the XML feed.
$feed = 'test.xml';
// How many items do we want to display?
//$display = 3;
// Check our XML file exists
if(!file_exists($feed)) {
  die('The XML file could not be found!');
}
// First, open the XML file.
$xml = simplexml_load_file($feed);
// Set the counter for counting how many items we've displayed.
$counter = 0;
// Start the loop to display each item.
foreach($xml->post as $post) {
  echo ' 
  <div style="float:left; width: 180px; margin-top:20px; margin-bottom:10px;">
 image file</a> <div class="design-sample-txt">'. $post->author.'</div></div>
';

  // Increase the counter by one.
  $counter++;
  // Check to display all the items we want to.
  if($counter >= 3) {
    echo 'image file';
    }
  //if($counter == $display) {
    // Yes. End the loop.
   // break;
  /

本文标题为:PHP:如何确定循环的每 N 次迭代?

基础教程推荐