let's say that i have this simplyfied code:
$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="'.$u开发者_JAVA百科rl.'" >'.$title.'</a></div>';
$ad ='<div>something here</div>';
}
echo $list;
instead to display a list of 10 divs, i want to echo 5 divs from $list
, echo $ad
then echo the rest of the $list
How can i do this?
Later edit :
First problem solved thanks to Michael.
Now, i have a problem with my template, and i don't know how can i add to every X number of $list divs, class="nomar"?
You can use a counter $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;
}
}
精彩评论