I have this situation where i want to get data from 3 different databases, say events, content and media and the look like this:
events ------------------------- | e_id | e_title | ------------------------- | 1 | event 1 | | 2 | event 2 | | 3 | event 3 | ------------------------- content ------------------------- | c_id | c_title | ------------------------- | 1 | first event | | 2 | second event | | 3 | third event | ------------------------- media ------------------------- | m_id | m_title | ------------------------- | 1 | picture 1 | | 1 | picture 2 | | 1 | picture 3 | -------------------------
Now i want to get the information from event 1, with the three pictures, so i have the following query
$result = mysql_query("
SELECT events.e_id, events.e_title, content.c_id, content.c_title, media.m_id, media.m_content
FROM events
INNER JOIN content
ON events.e_id = content.c_id
INNER JOIN media
ON events.e_id = media.m_id
WHERE events.e_id = '1'");
Now the point is, when i use
$show = mysql_fetch_assoc($result);
echo($show['e_title']." - ".$show['m_title'])
I only get picture 1
when i use
while($show = mysql_fetch_assoc($result)){
echo($show['e_title']." - ".$show['m_title']);}
i get three times event 1
How can i combine this that my output will be something like:
event 1 * picture 1 * picture 2 * picture 3
?
EDIT:
The solution below works fine, but now i have the following that doesn't work
while($show = mysql_fetch_assoc($result)){
if($show['e_title'] !== $last_title){
echo("<h3>".$show['e_title']."</h3>\n<ul>\n");
}
echo("\t<li>".$show['m_title']."</li>\n");
if($show['e_title'] !== $last_title){
echo("\t</ul>\n");
}
$last_title = $show['e_title'];
}
How can i achieve that my output will be like:
Event 1 * picture 1 * picture 2 * picture 3More info about event 1 (not shown in example)
?
On every loop iteration, store the most recent used
e_titlein$last_titleand only echo it out if it has changed ($show['e_title'] !== $last_title):Update If I understand what you’re trying to do with this, you need to test whether you have a
<ul>open and if you do, close it before starting a new<h3>rather than closing it at the end of the loop.