我遇到这种情况,我希望从3个不同的数据库中获取数据,例如events
,content
和media
,如下所示:
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 | -------------------------
现在我想通过三个图片从事件1获取信息,所以我有以下查询
$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 3
More info about event 1 (not shown in example)
答案 0 :(得分:1)
在每次循环迭代中,将最近使用过的e_title
存储在$last_title
中,并且只有在更改后才会将其回显($show['e_title'] !== $last_title
):
$last_title = "";
while($show = mysql_fetch_assoc($result)){
// Display the title if it has changed
if ($show['e_title'] !== $last_title) {
echo $show['e_title'] . "\n";
}
echo "- " . $show['m_title'] . "\n";
// Store the title to check in the next loop iteration
$last_title = $show['e_title'];
}
更新如果我了解您要对此进行的操作,则需要测试是否有<ul>
打开,如果您这样做,请在开始新{之前关闭它{1}}而不是在循环结束时关闭它。
<h3>