Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Display data from two tables - one to many relationship

$
0
0
Hello and thanks for your time. I am not well schooled in PHP or mySQL so please bare with me.

I have two tables:

Events:
eventid
title
description
location

Dates:
eventid
eventdate
starttime
endtime

The event could have multiple dates. So in the events table I could have:

eventid:1, title:Company Seminar, description:Company Seminar, location:Headquarters

And in the dates table the related dates (indicated by the same eventid) could be:

eventid:1, eventdate:2013-01-02, starttime:04:00, endtime:05:00
eventid:1, eventdate:2013-01-03, starttime:06:00, endtime:07:00
eventid:1, eventdate:2013-01-04, starttime:04:00, endtime:07:00

What I would like to display in the browser is something like this:

Event Title: Company Seminar
Event Description: Company Seminar
Event Location: Headquarters
Dates:
Day 1: 2013-01-02
Start Time: 04:00
End Time: 05:00
Day 2: 2013-01-03
Start Time: 06:00
End Time: 07:00
Day 3: 2013-01-04
Start Time: 04:00
End Time: 07:00

Next Event from the tables..., etc.

I have tried several query combinations but nothing that has produced the above. I'm sure it's some kind of loop perhaps with an inner loop inside.

Here's an example of some of the stuff I've been trying:

$sql = "SELECT * FROM events as x LEFT JOIN dates as y on x.eventid=y.eventid ORDER BY x.eventid";
$result = mysql_query($sql);
if($result){
    $currGroup = -1;
    while($row=mysql_fetch_array($result)){
       if($row['eventid']!=$currGroup){
         $currGroup = $row['eventid'];
         echo $currGroup."\n";
       }
echo "<p>";
echo "<b>Event ID:</b> ".$row['eventid'] . "</br>";
    }
}



I could provide 3 or 4 others that didn't work either. Any help will be greatly appreciated! Thanks!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>