简体   繁体   中英

php loop with several times per day

I have a mysql table that looks like this

id          date                    day
1           2016-03-10 15:00:00     monday
2           2016-03-10 16:00:00     monday
3           2016-03-10 17:00:00     monday
4           2016-03-11 15:00:00     tuesday
5           2016-03-11 16:00:00     tuesday
6           2016-03-11 17:00:00     tuesday
7           2016-03-11 18:00:00     tuesday

And then I'm using the following php code to extract the date and time info between now and 3 months

<?php 
session_start();
if(isset($_SESSION["userID"])){
    $start=date("Y-m-d H:i:s");
echo $stop= date('Y-m-d H:i:s', strtotime("+3 months", strtotime($start)));
    $result2 = $con->query("select * from event WHERE start_at BETWEEN '".$start."' AND '".$stop."'
ORDER by start_at ASC");
}else{
    header('Location: Login.php');
    }
 ?>

And then in my html body I've typed something like this

 <?php
     echo "<table><thead><tr><th>Day</th><th>Time</th></tr></thead>";
// output data of each row
while($row = $result2->fetch_assoc()) {
    echo "<tr><td>".$row["name"]."</td><td> ".date('G:i', strtotime($row["start_at"]))."</td></tr>";
}
echo "</table>";
?>

Which generates the following result

Day     Time
Monday  15:00
Monday  16:00

and so forth. But the result I want is this

Monday 2016-03-10
15:00
16:00
17:00
Tuesday 2016-03-11
15:00
16:00
17:00
18:00

Ideas on how to change my code to give me the desired output?

That is your needs.

<?php
$start=date("Y-m-d H:i:s");
$stop= date('Y-m-d H:i:s', strtotime("+3 months", strtotime($start)));

$result2 = $con->query("select * from event WHERE date BETWEEN '".$start."' AND '".$stop."' ORDER by date ASC");

echo "<table>";
$day = null;
while($row = $result2->fetch_assoc()) {
    if($day != $row['day']){
        $day = $row["day"];
        echo "<tr><td>".ucfirst($row['day'])."</td><td>".date('Y-m-d', strtotime($row["date"]))."</td></tr>";
        echo "<tr><td colspan='2'>".date('G:i', strtotime($row["date"]))."</td></tr>";
    }else{
        echo "<tr><td colspan='2'>".date('G:i', strtotime($row["date"]))."</td></tr>";
    }
}
echo "</table>";
?>

I just edited last code snippet based on your mySQL table structure . NOT TESTED . But I hope you get the idea:

<?php
    $day = null;
    echo "<table><thead><tr><th>--</th><th>--</th></tr></thead>";
    while($row = $result2->fetch_assoc()) {
        if($day != $row["name"]) {
            $day = $row["name"];
            echo "<tr><td>".$row["name"]."</td><td> ".strstr($row["date"], " ", -1)."</td></tr>";
        } else {
            echo "<tr><td>".strstr($row["start_at"], " ")."</td><td></td></tr>";
        }
    }
    echo "</table>";
?>

Edit :

I just notice that time from database comes from row named "start_at". Fixed the code.

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