您需要重構表格。你幾乎在那裏,但你需要一些規範化。取而代之的揚聲器1,揚聲器...等,把你的音箱在一個單獨的表:
speaker:
speaker_id | firstname | lastname | headshot ... (etc)
然後演示表應該基本上只是與呈現相關聯列:
presentation:
presentation_id | title | location ...(etc)
然後加入吧一起你需要一個連接表與結構
presentation_speaker:
presentation_id | speaker_id
所以引用的ID一樣,你可以通過執行獲得爲了所有發言者:
SELECT * FROM speaker ORDER BY lastname, firstname
編輯 如果你想用於演示的特定組揚聲器查詢
SELECT * FROM presentation_speaker ps
JOIN speaker s ON s.speaker_id=ps.speaker_id
WHERE ps.presentation_id IN ($pres1, $pres2, ... etc)
ORDER BY s.lastname, s.firstname
您的原始輸出是有點困難,但不是太糟糕。如果您沒有噸然後做一個額外的查詢在循環中並沒有那麼糟糕:
$query1 = "SELECT * FROM presentation";
$res = mysql_query($query1);
if(mysql_error($res)) {
die(mysql_error($res));
}
$presentations = array();
while($row = mysql_fetch_row($res)) {
$row['speakers'] = array();
$query2 = "SELECT * FROM presentation_speaker ps
JOIN speaker s ON ps.speaker_id = s.speaker_id
WHERE sp.presentation_id=" . mysql_real_escape_string($row['presentation_id']);
$res2 = mysql_query($query2);
if(mysql_error($res2)) {
die(mysql_error($res2));
}
while($speaker = mysql_fetch_row($res2)) {
$row['speakers'][] = $speaker;
}
$presentations[] = $row;
}
有了,你會最終有一個數據結構,看起來像:
Array(
'title' => 'Presentation title',
'location' => 'Some place',
'speakers' =>
Array(
'firstname' => 'Quazi',
'lastname' => 'Moto',
'headshot' => 'quazi.jpg'
),
Array(
'firstname' => 'Slarty',
'lastname' => 'Bartfast',
'headshot' => 'dontpanic.jpg'
)
)
你有什麼嘗試?嘗試失敗的地方在哪裏?你的具體編碼問題是什麼? – 2012-03-19 18:21:32
您是否在問如何使用Speakers頁面的單獨SQL查詢或者如何對已從表中提取的PHP中的數組進行排序?向我們展示一些代碼。 – 2012-03-19 18:28:26