I wish to make a "Previous events: ...." where it grabs the events that has date before now() WHERE date 开发者_开发问答< unix_timestamp(NOW())
and Upcoming events: where it grabs the events that has later than current date > unix_timestamp(NOW())
I got to this:
$grabEvents = $connect->prepare("SELECT eID FROM discos_events_guests WHERE uID =:user");
$grabEvents->bindValue(":user", $profile_id);
$grabEvents->execute();
if($grabEvents->rowCount()>0){
echo "<strong>Previous events:</strong><br>";
while($event = $grabEvents->fetch()){
$eID = $event["eID"];
$grabEvent = $connect->prepare("SELECT title FROM discos_events WHERE id=:eid AND date < unix_timestamp(NOW())");
$grabEvent->bindValue(":eid", $eID);
$grabEvent->execute();
$grabEvent = $grabEvent->fetch();
echo $grabEvent["title"]."<br>";
}
}
Works just fine, and grabs the previous events well. How should I do grab the Upcoming events then, in this code? I tried having another query inside the while but it gets all messed up because it checks two and yes, didnt turn out how i wish.
How can i do this?
For a start you need to optimise your query instead of repeatedly querying within a while loop do this instead:
SELECT deg.eID, de.title
FROM discos_events_guests deg
JOIN discos_events de on de.id = deg.eID
WHERE uID =:user
AND date < unix_timestamp(NOW())
For example to get the previous you would do:
$grabEvents = $connect->prepare("SELECT de.title
FROM discos_events_guests deg
JOIN discos_events de on de.id = deg.eID
WHERE uID =:user
AND date < unix_timestamp(NOW())");
$grabEvents->bindValue(":user", $profile_id);
$grabEvents->execute();
if($grabEvents->rowCount()>0){
while($event = $grabEvents->fetch()){
echo $event["title"]."<br>";
}
}
Then just execute the same again for the upcoming query:
$grabEvents = $connect->prepare("SELECT de.title
FROM discos_events_guests deg
JOIN discos_events de on de.id = deg.eID
WHERE uID =:user
AND date > unix_timestamp(NOW())");
$grabEvents->bindValue(":user", $profile_id);
$grabEvents->execute();
if($grabEvents->rowCount()>0){
while($event = $grabEvents->fetch()){
echo $event["title"]."<br>";
}
}
I would probably just start another loop, like:
$grabEvents = $connect->prepare("SELECT eID FROM discos_events_guests WHERE uID =:user");
$grabEvents->bindValue(":user", $profile_id);
$grabEvents->execute();
if($grabEvents->rowCount()>0){
echo "<strong>Previous events:</strong><br>";
while($event = $grabEvents->fetch()){
$eID = $event["eID"];
$grabEvent = $connect->prepare("SELECT title FROM discos_events WHERE id=:eid AND date < unix_timestamp(NOW())");
$grabEvent->bindValue(":eid", $eID);
$grabEvent->execute();
$grabEvent = $grabEvent->fetch();
echo $grabEvent["title"]."<br>";
}
echo "<strong>Upcoming Events:</strong><br>";
while($event = $grabEvents->fetch()){
$eID = $event["eID"];
$grabEvent = $connect->prepare("SELECT title FROM discos_events WHERE id=:eid AND date > unix_timestamp(NOW())");
$grabEvent->bindValue(":eid", $eID);
$grabEvent->execute();
$grabEvent = $grabEvent->fetch();
echo $grabEvent["title"]."<br>";
}
}
At least, if I understand your question correctly.
精彩评论