在下面的示例中,我正在提取六个随机行,并希望将变量之一(a、b、c、d、e、f)分配给每个生成的行。
$result = mysql_query("SELECT * FROM cub_posts WHERE active = 'Y' ORDER BY rand() LIMIT 6") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$demo_id = 'a, b, c, d, e, f';
echo '<div class="span4 span-box">
<div id="demo5'.$demo_id.'" class="ho-box demo5">'.$row['title'].'</div>
</div>';
}
所以输出将是:
<div class="span4 span-box">
<div id="demo5a" class="ho-box demo5">Title</div>
</div>
<div class="span4 span-box">
<div id="demo5b" class="ho-box demo5">Title</div>
</div>
<div class="span4 span-box">
<div id="demo5c" class="ho-box demo5">Title</div>
</div>
<div class="span4 span-box">
<div id="demo5d" class="ho-box demo5">Title</div>
</div>
<div class="span4 span-box">
<div id="demo5e" class="ho-box demo5">Title</div>
</div>
<div class="span4 span-box">
<div id="demo5f" class="ho-box demo5">Title</div>
</div>