我試圖使用PHP和MySQL來自動生成一個像這樣的列表。使用從一個MySQL查詢插入到另一個查詢
對象#1
- 資源#3
- 資源#5
- 資源#12
受試者#2
- 資源#7
- Resourc È#4
- 資源#1
等
下面是表:
eresources
- ERID
- etitle
個科目
- SID
- stitle
subjectmap
- SID
- ERID
下面是成功給了我所有的subje代碼按字母順序CTS:
$subjectQuery = "SELECT * FROM subjects WHERE sid != 17 ORDER BY stitle ASC";
$subjectResult = $mysqli->query($subjectQuery);
while ($subjectArray = $subjectResult->fetch_assoc()) {
print "<h5 class='subcategory'>" . $subjectArray['stitle'] . "</h5>";
print "<div class='inner'>
// Need a list of resources that match each subject ID (sid) here!
</div>";
}
現在,這裏是唯一成功給了我所有的電子資源具有固定主題ID代碼:
$getERBySubjectQuery = " SELECT erid FROM subjectmap WHERE sid=11 ";
$getERBySubjectResult = $mysqli->query($getERBySubjectQuery);
if($getERBySubjectResult && $getERBySubjectResult->num_rows >= 1){
while($getERBySubjectArray = $getERBySubjectResult->fetch_assoc()){
$query = " SELECT * FROM eresources WHERE erid = " .$getERBySubjectArray['erid']. " ORDER BY ertitle ASC ";
$result = $mysqli->query($query);
if($result && $result->num_rows >= 1){
while($array = $result->fetch_assoc()){
print("<a href=\"" . $array['link'] . "\">" . "<h5 class='subcategory'>" . $array['ertitle'] . "<div class='accessnote'>" . $array['access'] . "</div></h5></a>");
}}}}
當然,我不傾向於複製相同節每個主題ID(sid)。我希望自動生成主題列表以及自動生成主題的資源列表。
基本上我需要從第一節喂sid到每個主題的第二節。
謝謝你們的Frits和Djangofan的回覆。非常感激! –