如何在php中的foreach语句的每第三个结果上输出一个值?

时间:2012-05-04 08:29:15

标签: php foreach echo

我的应用程序中有一个foreach语句,它回显了我的数据库结果列表:

<?php

foreach($featured_projects as $fp) {
  echo '<div class="result">';
  echo $fp['project_name'];
  echo '</div>';
}

?>

我想:

在每第三个结果上,给div一个不同的类。我怎样才能做到这一点?

11 个答案:

答案 0 :(得分:19)

您可以使用计数器和modulo/modulus运算符,如下所示:

<?php

// control variable
$counter = 0;

foreach($featured_projects as $fp) {

    // reset the variable
    $class = '';

    // on every third result, set the variable value
    if(++$counter % 3 === 0) {
        $class = ' third';
    }

    // your code with the variable that holds the desirable CSS class name
    echo '<div class="result' . $class . '">';
    echo $fp['project_name'];
    echo '</div>';
}

?>

答案 1 :(得分:5)

<?php

foreach ($featured_projects as $i => $fp) {
    echo '<div class="result' . ($i % 3 === 0 ? ' third' : '') . '">';
    echo $fp['project_name'];
    echo '</div>';
}
?>

答案 2 :(得分:1)

如果$featured_projects数组基于增量索引,您只需使用索引和modulo % operator

否则你必须添加一个计数器。

http://php.net/manual/en/language.operators.arithmetic.php

答案 3 :(得分:0)

在此循环中添加一个计数器,并检查计数器是否等于3并应用类。

答案 4 :(得分:0)

使用计数器和模运算符,这很容易实现

答案 5 :(得分:0)

这是工作版,抱歉我的上一版本:

<?php
$featured_projects[0]['project_name'] = "pippo";
$featured_projects[1]['project_name'] = "pippo2";
$featured_projects[2]['project_name'] = "pippo3";

$class[0] = "class1";
$class[1] = "class2";

$i=0;
foreach($featured_projects as $fp) {
  $i++;
  if ($i == 3) {
    $c = $class[1];
    $i=0;
  } else {
    $c = $class[0];
  }
  echo "<div class=\"$c\">";
  echo $fp['project_name'];
  echo "</div>\n";
}
?>

产地:

<div class="class1">pippo</div>
<div class="class1">pippo2</div>
<div class="class2">pippo3</div>

答案 6 :(得分:0)

您可以循环添加计数器...尝试以下内容...

 <?php 
 $i = 0; 
 foreach($featured_projects as $fp) {
 $i = ++$i;
 if(($i%3) == 0)
 {
  $class1 = 'test1';
 }
 else
 {
 $class1 = 'test2';
 }
       echo '<div class="'.$class1.'">';
       echo $fp['project_name'];
       echo '</div>';
 }
 ?>

答案 7 :(得分:0)

<?php 
foreach($featured_projects as $fp) {
    if(++$i % 3 === 0) {
        $class = ' something';
    } else {
        $class = '';
    }
    echo '<div class="result' . $class . '">';
    echo $fp['project_name'];
    echo '</div>';
}
?>

答案 8 :(得分:0)

<?php
$i = 0;
foreach($featured_projects as $fp) {
    echo '<div class="'.($i++%3 ? 'result' : 'other_class').'">';
    echo $fp['project_name'];
    echo '</div>';
}
?>

答案 9 :(得分:0)

你的代码主要是什么?

<?php 
$i = 1;
foreach($featured_projects as $fp) {
printf ('<div class="%s">',(($i % 3) ? "result" : "result_every_third" ));
echo $fp['project_name'];
echo '</div>';
$i++;
}
?>

但是你可能想考虑在“each($ featured_projects)”周围使用for或while结构(参见http://php.net/manual/en/function.each.php),这可能会产生更整洁的代码。

答案 10 :(得分:0)

<?php
    $counter = 0;

    foreach ($featured_projects as $fp) {
        echo '<div class="result' . ($counter++ % 3 === 0 ? ' third' : '') . '">';
        echo $fp['project_name'];
        echo '</div>';
    }
?>