In my small application, I have events that can have multiple images associated with them.
For example, the event “snowboarding”, may have three images associated with it.
Here are my tables:
Events
event_id title
Event_Images
event_image_id image_src time_uploaded
Here is the query that I am currently using:
SELECT * FROM Events, Event_Images
WHERE Events.event_id = Event_Images.event_id
It results in the following data:
[
{
"event_id": "67",
"title": "Setting up the UGS App!",
"event_image_id": "64",
"image_src": "uploads/50f47bce35d4f.jpg",
"time_uploaded": "2013-01-14 16:42:41"
},
{
"event_id": "67",
"title": "Setting up the UGS App!",
"event_image_id": "92",
"image_src": "uploads/49f4709eppdl.jpg",
"time_uploaded": "2013-02-14 12:44:43"
}
]
However, this seems redundant to me. The data structure I imagine would be the least redundant would appear like so:
[
{
"event_id": "67",
"title": "Setting up the UGS App!",
"event_image_id": "64",
"images": [
{
"image_src": "uploads/50f47bce35d4f.jpg",
"time_uploaded": "2013-01-14 16:42:41"
},
{
"image_src": "uploads/49f4709eppdl.jpg",
"time_uploaded": "2013-02-14 12:44:43"
}
]
}
]
I imagine that either the current way I am doing it is correct, or there is a method that will result in a less redundant result from my query.
Pardon my ignorance if this seems like a simple question.
Any references or help is greatly appreciated.
Ok, thanks to inhan I have my answer– use php to format the data after it is returned. With this information, I just wrote a reuseable function:
Use in this case: