简体   繁体   English

使用for循环组合PHP优化MYSQL查询,同时循环输出

[英]Optimize a MYSQL query while loop output with a for loop combination PHP

I am trying to output each row from a mysql query slightly differently in terms of styling, however I want to try to avoid having to use offsets and multiple queries. 我试图从样式上稍微不同地从mysql查询输出每一行,但是我想避免使用偏移量和多个查询。

Current code: 当前代码:

$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 1";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
// output first news item with a style
echo '<div class="style-1">'.$title.'</div>';
}

$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 1 OFFSET 1";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
// output second news item with different style
echo '<div class="style-2">'.$title.'</div>';
}

I would thus like to avoid having 2 (or more) queries simple because I want to use different css classes for each while row, something like this: 因此,我想避免使用2个(或多个)简单查询,因为我想为while每一行使用不同的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
    ...

Desired Output: 所需输出:

<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>
...

I'm assuming you want 3 different styles, and here's how I would accomplish this: 我假设您想要3种不同的样式,这就是我要完成的方式:

$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 10";
$query = mysqli_query($connection, $sql);

$i = 1;

while($row = mysqli_fetch_array($query)){
    $title = $row["title"];
    if($i<2){
        $styleNumber = 1;
    }
    else if($i<3){
        $styleNumber = 2;
    }
    else if($i<4){
        $styleNumber = 3;
    }
    echo '<div class="style-'.$styleNumber.'">'.$title.'</div>';
    $i++;
} 
// end while

Output : 输出
Title 1 (Style 1) 标题1(样式1)
Title 2 (Style 2) 标题2(样式2)
Title 3 (Style 3) 标题3(样式3)
Title 4 (Style 3) 标题4(样式3)

This is not an answer. 这不是答案。 Just a comment/improvement of @JonTan answer since it was chosen as correct one. 由于@JonTan答案被选为正确答案,因此仅是对它的评论/改进。

My guess is that OP need to loop styles for every 3 records, but not to set style only for 3 first returned. 我的猜测是,OP需要为每3条记录循环样式,而不是仅为3个首先返回的样式设置样式。

That means we can change condition from if($i<3){ , if($i<2){ to something like: 这意味着我们可以将条件从if($i<3){if($i<2){更改为以下内容:

$i = 1;

while($row = mysqli_fetch_array($query)){
    $title = $row["title"];
    if($i % 3 == 0){
        $styleNumber = 3;
    } else if($i % 2 == 0){
        $styleNumber = 2;
    } else {
        $styleNumber = 1;
    }
    echo '<div class="style-'.$styleNumber.'">'.$title.'</div>';
    $i++;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM