本文介紹了如何在php中的foreach語句的每三個結果上輸出一個值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我的應用程序中有一個 foreach
語句,用于回顯我的數據庫結果列表:
';回聲 $fp['project_name'];回聲'</div>';}?>我想:
每隔三個結果,給 div 一個不同的類.我怎樣才能做到這一點?
解決方案 您可以使用計數器和 模數/模數運算符如下:
I have a foreach
statement in my app that echos a list of my database results:
<?php
foreach($featured_projects as $fp) {
echo '<div class="result">';
echo $fp['project_name'];
echo '</div>';
}
?>
I would like to:
On every third result, give the div a different class. How can I achieve this?
解決方案 You can use a counter and the modulo/modulus operator as per below:
<?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>';
}
?>
這篇關于如何在php中的foreach語句的每三個結果上輸出一個值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!