php回显变量1、暂停和回显变量2,然后回显其余变量1

php回显变量1、暂停和回显变量2,然后回显其余变量1,php,database,variables,echo,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.'</

假设我有一个简化的代码:

$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;
$sql=dbquery(“从视频中选择*视图>4按id排序描述限制0,10”);
while($row=mysql\u fetch\u数组($sql)){
$url=$row[“url”];
$title=$row[“title”];
$list='';
$ad='此处某物';
}
echo$列表;
为了显示10个div的列表,我想从
$list
回显5个div,回显
$ad
,然后回显
$list
的其余部分

我该怎么做

稍后编辑:

多亏了迈克尔,第一个问题解决了


现在,我的模板有问题,我不知道如何向每个X个$list div添加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;
  }
}
$sql=dbquery(“从视频中选择*视图>4按id排序描述限制0,10”);
$i=1;
//使用mysql\u fetch\u assoc()而不是mysql\u fetch\u array()!
while($row=mysql\u fetch\u assoc($sql)){
$url=$row[“url”];
$title=$row[“title”];
//更改某个数字上的列表类。。。
如果($i==3){
$list_class=“正常”;
}
else$list_class=“其他类”;
//合并新类
$list='';
//输出$list
echo$列表;
//增加你的计数器
$i++;
//达到5时输出$ad
//这只发生一次。之后,$list继续打印。
如果($i==5){
$ad='此处某物';
echo$ad;
}
}

您可以使用计数器
$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;
  }
}
$sql=dbquery(“从视频中选择*视图>4按id排序描述限制0,10”);
$i=1;
//使用mysql\u fetch\u assoc()而不是mysql\u fetch\u array()!
while($row=mysql\u fetch\u assoc($sql)){
$url=$row[“url”];
$title=$row[“title”];
//更改某个数字上的列表类。。。
如果($i==3){
$list_class=“正常”;
}
else$list_class=“其他类”;
//合并新类
$list='';
//输出$list
echo$列表;
//增加你的计数器
$i++;
//达到5时输出$ad
//这只发生一次。之后,$list继续打印。
如果($i==5){
$ad='此处某物';
echo$ad;
}
}

只是一个说明-您想使用
mysql\u fetch\u assoc()
而不是
mysql\u fetch\u array()
,因为您已经用
$row['url']
等键引用了您的列。谢谢,我将替换它。我添加了一个仍然存在的小问题。根据michael的代码$列表='';在将div类命名为div_1、div_2之后…谢谢,这样就可以了,但是我必须为div_4、div_8、div_12等等添加css。。。我不能只向每4个div添加一个类吗?请注意-你想使用
mysql\u fetch\u assoc()
而不是
mysql\u fetch\u array()
,因为你已经用
$row['url']
等键引用了你的列。谢谢你,我会替换它。我已经添加了一个我仍然存在的小问题$列表='';在将div类命名为div_1、div_2之后…谢谢,这样就可以了,但是我必须为div_4、div_8、div_12等等添加css。。。我不能只将类添加到每4个div中吗?再次感谢,唯一的问题是我将$i==5替换为to$i==6以显示前5个div。我的模板现在仍然存在一个问题:如何在每个X个显示的$list div中添加,class=“nomar”?再次感谢,唯一的问题是,我将$i==5替换为to$i==6以显示前5个。我的模板现在仍然存在一个问题:如何在显示的每个X数处添加$list div,class=“nomar”?