我目前正在做一个使用while($r = mysql_fetch_array($the_data))提取数据的项目
我想让第一个,比方说3-4结果是不同的背景颜色,然后让其余的已经样式,我相信这里有一些简单的选择,但我只是不知道在哪里看起来真的…
希望你能帮上忙,谢谢!
发布于 2009-10-01 15:39:29
您是否在寻找类似以下内容:
#specialResult {background-color: #fff; }
#normalResult {background-color: #000; }因此,当您在while语句中循环时,您将希望跟踪您所处的结果编号:
$i = 0;
while (...)
{
if ($i < 4) echo "<div class='specialResult'>";
else echo "<div class='normalResult'>";
.... rest of your code
$i++;
}对于看起来更短的代码,你可以这样做:
$i = 0;
while (...)
{
?><div class='<?php echo ($i++ < 4 ?"specialResult":"normalResult); ?>'><?php
.... rest of your code
}发布于 2009-10-01 15:38:32
<?php
$i = 0;
$r = mysql_fetch_array($the_data);
foreach($r as $row) {
if($i <= 4) {
// Do special styling...
} else {
// Do normal styling.
}
$i++;
}
?>还是我误解了?
发布于 2009-10-01 16:41:06
您还可以尝试执行以下操作:
$i = 0;
while ( ( $row = mysql_fetch_assoc ( $result ) ) && $i < 4 ) {
/* Your first 4 rows */
echo 'Special : ' . $row['title'];
++$i; // Don't forget to increment
} while ( $row = mysql_fetch_assoc () ) {
/* Normal way */
echo $row['title'] . ' is already outdated';
}更喜欢mysql_fetch_assoc()而不是mysql_fetch_array() ;)
https://stackoverflow.com/questions/1504677
复制相似问题