假设我有一个简单的代码:
$sql = dbquery("SELECT * FROM videos WHERE views > 4 ORDER BY id DESC LIMIT 0,10 ");
while($row = mysql_fetch_array($sql)){
$url = $row["url"];
$title = $row["title"];
$list ='<div><a href="'.$url.'" >'.$title.'</a></div>';
$ad ='<div>something here</div>';
}
echo $list;
相反,为了显示10个div的列表,我想从$list
回显5个div,回显$ad
,然后回显其余的$list
我该怎么做?
后期编辑:
多亏了迈克尔,第一个问题解决了。
现在,我的模板有问题,我不知道如何添加到$list-dvs的每个X个数字,class="nomar"?
您可以使用计数器$i
$sql = dbquery("SELECT * FROM videos WHERE views > 4 ORDER BY id DESC LIMIT 0,10 ");
$i = 1;
// Use mysql_fetch_assoc() rather than mysql_fetch_array()!
while($row = mysql_fetch_assoc($sql)){
$url = $row["url"];
$title = $row["title"];
// Change the list class on a certain number...
if ($i == 3) {
$list_class = "normal";
}
else $list_class = "some-other-class";
// Incorporate the new class
$list ='<div class="' . $list_class . '"><a href="'.$url.'" >'.$title.'</a></div>';
// Output $list
echo $list;
// Increment your counter
$i++;
// Output $ad when you reach 5
// This only happens once. Afterward, $list continues to print.
if ($i == 5) {
$ad ='<div>something here</div>';
echo $ad;
}
}