Reputation: 107
I want to select the following values with the same ID. I basically have a submit form which stores its values and I want to display each submit. This is how the Databse Table looks like:
| id | name | value |
------------------------------------
| 1 | venue_1 | New York |
| 1 | time_1 | 12:00 |
| 1 | costs_1 | 50€ |
| 2 | venue_1 | Berlin |
| 2 | time_1 | 15:00 |
| ... | ... | ... |
I want to display the values like this:
| Venue | Time | Cost |
------------------------------------
| New York | 12:00 | 50€ |
| Berlin | 15:00 | ... |
| A Venue | The time | cost |
My SQL queries in PHP look like this (Joomla):
$db->setQuery('SELECT value FROM subrecords WHERE name = "venue_1"');
$venue = $db->loadColumn();
$db->setQuery('SELECT value FROM subrecords WHERE name = "time_1"');
$time = $db->loadColumn();
But when I loop through them to display them it doesn't match up:
foreach ($venue as $key => $val) {
$output .= "<tr>
<td>".$venue[$key]."</td>
<td>".$time[$key]."</td>
</tr>";
}
print($output);
I dont see through the logic here because as of now I just select all the venue_1 and time_1 etc and load the columns so I get every value but they need to be linked to each other so I can display them in the right way.
Where is my mistake? How can I link them through the ID maybe?
If something is unclear tell me please I don't want to make it difficult for you to help me :)
Upvotes: 0
Views: 455
Reputation: 15057
Here you can get all in one Query. When you delete the WHERE line you get the hole table
SELECT
Coalesce ((CASE WHEN name = "venue_1" THEN value END),NULL) Vebue,
Coalesce ((CASE WHEN name = "time_1" THEN value END),NULL) Time,
Coalesce ((CASE WHEN name = "cost_1" THEN value END),0) Coast
FROM subrecords
WHERE id = 1
GROUP BY id;
Upvotes: 4