开发者

php echo variable1, pause and echo variable 2 then echo the rest of variable1

开发者 https://www.devze.com 2023-03-08 19:24 出处:网络
let\'s say that i have this simplyfied code: $sql = dbquery(\"SELECT * FROM videos WHERE views > 4 ORDER BY id DESC LIMIT 0,10 \");

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;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消