简体   繁体   中英

While loop within switch case - Grouping results

I'm trying to group results based on the Times when an article was published (just like newsnow website aggregates)

My PHP code looks like this:

 $sql="SELECT articles.id, name, title, description, timestamp 
        FROM articles 
        LEFT JOIN sources ON `articles`.`source_id` = `sources`.`id` 
        ORDER BY timestamp 
        DESC LIMIT 25";
$result = mysqli_query($db,$sql);

while($row = mysqli_fetch_array($result)) {
$currentTime = time();
$articletimes = $row['timestamp'];
$timedifference = $currentTime - $articletimes;  


switch (true) {
case ($timedifference < 300):
    echo "<br/>Less than 5 minutes ago <br/>";
    echo "$row['title']";
    break;
case ($timedifference < 900 && $timedifference > 300):
    echo "<br/>15 Mins ago <br/>";
    echo "$row['title']";
    break;
case ($timedifference < 1800 && $timedifference > 900):
    echo "<br/>30 mins ago <br/>";
    echo "$row['title']";
    break;
case ($timedifference < 3600 && $timedifference > 1800):
    echo "<br/>about an hour ago <br/>";
    echo "$row['title']";
                }

}

} mysqli_close($db);

Currently the code above displays something like this:

Less than 5 minutes ago
Article Name A
Less than 5 minutes ago
Article Name B
Less than 5 minutes ago
Article Name C
30 mins ago
Article Name D
30 mins ago
Article Name E

Is there a way to group the articles by the time they were posted (example:)

Less than 5 minutes ago
Article Name A
Article Name B
Article Name C
30 mins ago
Article Name D
Article Name E

Much Appreciated :)

You have to store all the visited case information in array or variable. for this current solution.

$visitedCase = [];
while($row = mysqli_fetch_array($result)) {
    $currentTime = time();
    $articletimes = $row['timestamp'];
    $timedifference = $currentTime - $articletimes;  

    switch (true) {
        case ($timedifference < 300):
            if(!(in_array(1, $visitedCase)) {
                echo "<br/>Less than 5 minutes ago <br/>";
                $visitedCase[] = 1;
            }
            break;
        case ($timedifference < 900 && $timedifference > 300):
            if(!(in_array(2, $visitedCase)){
                echo "<br/>15 Mins ago <br/>";
                $visitedCase[] = 2;
            }   
            break;
        case ($timedifference < 1800 && $timedifference > 900):
            if(!(in_array(3, $visitedCase)){
                echo "<br/>30 mins ago <br/>";
                $visitedCase[] = 3;
            }                               
            break;
        case ($timedifference < 3600 && $timedifference > 1800):
            if(!(in_array(4, $visitedCase)){
                echo "<br/>about an hour ago <br/>";
                $visitedCase[] = 4;
            }                    
    }
    echo $row['title'];

} 

You can use flags to identify if its the 1st time of the switch =>

$i = 0;
$j = 0;
$k = 0;
while($row = mysqli_fetch_array($result)) {
$currentTime = time();
$articletimes = $row['timestamp'];
$timedifference = $currentTime - $articletimes;

switch (true) {
case ($timedifference < 300):
    if($i == 0){
     echo "<br/>Less than 5 minutes ago <br/>";
     $i++;
    }
    echo "$row['title']";
    break;
case ($timedifference < 900 && $timedifference > 300):
    if($j == 0){
     echo "<br/>15 Mins ago <br/>";
     $j++;
    }
    echo "$row['title']";
    break;
case ($timedifference < 1800 && $timedifference > 900):
    if($k == 0){
     echo "<br/>30 mins ago <br/>";
     $k++;
    }
    echo "$row['title']";
    break;
case ($timedifference < 3600 && $timedifference > 1800):
    echo "<br/>about an hour ago <br/>";
    echo "$row['title']";
                }

}

}

If you want to do it in PHP then you will have to remember that you have already output the Title line so you only do it once per grouping

$sql="SELECT articles.id, name, title, description, timestamp 
        FROM articles 
        LEFT JOIN sources ON `articles`.`source_id` = `sources`.`id` 
        ORDER BY timestamp 
        DESC LIMIT 25";
$result = mysqli_query($db,$sql);

while($row = mysqli_fetch_array($result)) {
    $currentTime = time();
    $articletimes = $row['timestamp'];
    $timedifference = $currentTime - $articletimes;  

    $done = ['d300'=>false,'d900'=>false,'d1800'=>false,'d3600'=>false];

    switch (true) {
        case ($timedifference < 300):
            if ( !$done['d300'] ) {
                echo "<br/>Less than 5 minutes ago <br/>";
                $done['d300'] = true;
            }
            echo "$row['title']";
            break;
        case ($timedifference < 900 && $timedifference > 300):
            if ( !$done['d900'] ) {
                echo "<br/>15 Mins ago <br/>";
                $done['d900'] = true;
            }
            echo "$row['title']";
            break;
        case ($timedifference < 1800 && $timedifference > 900):
            if ( !$done['d1800'] ) {
                echo "<br/>30 mins ago <br/>";
                $done['d1800'] = true;
            }
            echo "$row['title']";
            break;
        case ($timedifference < 3600 && $timedifference > 1800):
            if ( !$done['d3600'] ) {
                echo "<br/>about an hour ago <br/>";
                $done['d3600'] = true;
            }
            echo "$row['title']";
            break;
    }
}

Here is code that can display output as per you need.

$sql="SELECT articles.id, name, title, description, timestamp FROM articles 
    LEFT JOIN sources ON `articles`.`source_id` = `sources`.`id` 
    ORDER BY timestamp DESC LIMIT 25";
$result = mysqli_query($db,$sql);

while($row = mysqli_fetch_array($result)) {
    $currentTime = time();
    $articletimes = $row['timestamp'];
    $timedifference = $currentTime - $articletimes;  

    $td_3 = $td_9 = $td_18 = $td_36 = 0;

    switch (true) {
            case ($timedifference < 300):
                if($td_3 == 0){
                    echo "<br/>Less than 5 minutes ago <br/>";  
                }   
                $td_3 = 1;
                echo "$row['title']";
                break;
            case ($timedifference < 900 && $timedifference > 300):
                if($td_9 == 0){
                    echo "<br/>15 Mins ago <br/>";  
                }   
                $td_9 = 1;
                echo "$row['title']";
                break;
            case ($timedifference < 1800 && $timedifference > 900):
                if($td_18 == 0){
                    echo "<br/>30 mins ago <br/>";  
                }
                $td_18 = 1;
                echo "$row['title']";
                break;
            case ($timedifference < 3600 && $timedifference > 1800):
                if($td_36 == 0){
                    echo "<br/>about an hour ago <br/>";
                }
                $td_36 = 1;
                echo "$row['title']";
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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