php echo variable1,pause和echo变量2然后回显variable1的其余部分

时间:2011-05-28 14:56:18

标签: php database variables echo

让我说我有这个简单的代码:

$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,echo $ad然后回显$list的其余部分

我该怎么做?

稍后编辑:

感谢迈克尔解决了第一个问题。

现在,我的模板有问题,我不知道如何添加每个X列的$ list divs,class =“nomar”?

1 个答案:

答案 0 :(得分:1)

您可以使用计数器$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;
  }
}