我该如何解决这个php mysql循环?

m3t*_*sys -1 php mysql

<?

$sql = dbquery("SELECT `id`, `title`, `description` FROM `content` LIMIT 0,12 ");
while ($row = mysql_fetch_array($sql)) {
    $id          = $row["id"];
    $title       = $row["title"];
    $description = $row["description"];
    $content     = '
    <div class="description-box">
    <p>page id: ' . $id . '</p>
    <p>' . $title . '</p>
    <p>' . $description . '</p>
    </div>';
}

echo $content;
?>
Run Code Online (Sandbox Code Playgroud)

// notice:div class ="description-box"

//如何在每3个输出div之后用以下代码替换:div class ="description-box nopadding"?

html生成代码示例:

<div class="description-box nopadding">
// content
</div>
<div class="description-box">
// content
</div>
<div class="description-box">
// content
</div>
<div class="description-box">
// content
</div>
Run Code Online (Sandbox Code Playgroud)

//然后又是div nopadding

Pet*_*ter 5

<div class="description-box'.(($c++%3==0)?' nopadding':'').'">
Run Code Online (Sandbox Code Playgroud)

记住初始化 $c

这很简单:

如果$c除以3的余数等于0则添加' nopadding'字符串,否则添加''stirng

0 % 3 == 0 // add
1 % 3 == 1
2 % 3 == 2
3 % 3 == 0 // add
4 % 3 == 1
...
Run Code Online (Sandbox Code Playgroud)

句法:

echo true ? "it's true" : "it's false"; // it's true
echo (5 % 3); // 2, remainder of 5/3 division
$c = 0;
echo $c++; // 0
echo $c;   // 1
Run Code Online (Sandbox Code Playgroud)