-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>

// 次に、再び div をnopadding

4

2 に答える 2

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

初期化することを忘れないでください$c

とても簡単です:

3 で割った余りが$c0の場合は' 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 に答える