我试图从一个mysql查询输出每一行在样式方面略有不同,但是我想尽量避免使用偏移量和多个查询。优化MYSQL查询while循环输出与for循环组合PHP
当前代码:
我会这样想避免2(或更多)查询简单,因为我想用不同的CSS类为每个而行,像这样:
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 10";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
} // end while
$i = 1;
for($i; $i<2; $i++){ // output first row item with first style
echo '<div class="style-1">'.$title.'</div>';
} // end for loop 1
for($i; $i<3; $i++){ // output first row item with second style
echo '<div class="style-2">'.$title.'</div>';
} // end for loop 2
for($i; $i<4; $i++){ // output third row item with third style
echo '<div class="style-3">'.$title.'</div>';
} // end for loop 3
...
所需的输出:
<div class="style-1">News Headline Title 1</div>
<div class="style-2">News Headline Title 2</div>
<div class="style-3">News Headline Title 3</div>
...
你可以更新表结构,并与应该是什么样的期望输出你的问题? – Maximus2012
你究竟用“表格结构”是什么意思?期望的输出写在上面:echo'
应该输出什么? – Maximus2012