本文介紹了如何在php中的foreach語句的每三個結(jié)果上輸出一個值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我的應(yīng)用程序中有一個 foreach
語句,用于回顯我的數(shù)據(jù)庫結(jié)果列表:
';回聲 $fp['project_name'];回聲'</div>';}?>我想:
每隔三個結(jié)果,給 div 一個不同的類.我怎樣才能做到這一點(diǎn)?
解決方案 您可以使用計數(shù)器和 模數(shù)/模數(shù)運(yùn)算符如下:
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>';
}
?>
這篇關(guān)于如何在php中的foreach語句的每三個結(jié)果上輸出一個值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!