-1
<?

$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;
?>

// 注意: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>

// 然后再次使用 divnopadding

4

2 回答 2

5
<div class="description-box'.(($c++%3==0)?' nopadding':'').'">

记得初始化$c

这很简单:

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

0 % 3 == 0 // add
1 % 3 == 1
2 % 3 == 2
3 % 3 == 0 // add
4 % 3 == 1
...

句法:

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
于 2012-08-23T14:52:25.163 回答
0
<?
$sql = dbquery("SELECT `id`, `title`, `description` FROM `content` LIMIT 0,12 ");
$counter = 0; 
while($row = mysql_fetch_array($sql)){  
$id = $row["id"];   
$title = $row["title"];
$description = $row["description"];
if($counter%3 == 0)
{
 $div = '<div class="description-box nopadding">'
}
else
{
 $div = '<div class="description-box">';
}
 content = $div.'
 <p>page id: '.$id.'</p>
 <p>'.$title.'</p>
 <p>'.$description.'</p>
 </div>';
 $counter++;
}

echo $content;  ?>
于 2012-08-23T14:55:20.737 回答