好,最簡單的事情是爲你重複代碼塊3倍。每一次,與傳遞給查詢不同的藝術家ID:
<?php
include "Connection.php";
$sql = "SELECT artist.artistName, artistcd.artistID, artistcd.cdID, artistcd.cdTitle, artistcd.cdGenre, artistcd.cdPrice
FROM artist, artistcd
WHERE artist.artistID = "$ArtistID1";
$query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));;
$name = mysqli_fetch_assoc($query);
echo "<p>" . $name["artistID"] . $name["artistName"] . "</p>";
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr>
<th>Genre</th>
<th>CD Identification</th>
<th>Title</th>
<th>Price</th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($query)) {
echo "<tr>";
echo "<td>" . $row['cdGenre']. "</td>";
echo "<td>" . $row['cdID']. "</td>";
echo "<td>" . $row['cdTitle']. "</td>";
echo "<td>" . $row['cdPrice']. "</td>";
echo "</tr>";
}
?>
<table width="70%" cellpadding="5" cellspace="5">
</table>
<?php
$sql = "SELECT artist.artistName, artistcd.artistID, artistcd.cdID, artistcd.cdTitle, artistcd.cdGenre, artistcd.cdPrice
FROM artist, artistcd
WHERE artist.artistID = "$ArtistID2";
$query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));;
$name = mysqli_fetch_assoc($query);
echo "<p>" . $name["artistID"] . $name["artistName"] . "</p>";
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr>
<th>Genre</th>
<th>CD Identification</th>
<th>Title</th>
<th>Price</th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($query)) {
echo "<tr>";
echo "<td>" . $row['cdGenre']. "</td>";
echo "<td>" . $row['cdID']. "</td>";
echo "<td>" . $row['cdTitle']. "</td>";
echo "<td>" . $row['cdPrice']. "</td>";
echo "</tr>";
}
?>
<table width="70%" cellpadding="5" cellspace="5">
</table>
<?php
$sql = "SELECT artist.artistName, artistcd.artistID, artistcd.cdID, artistcd.cdTitle, artistcd.cdGenre, artistcd.cdPrice
FROM artist, artistcd
WHERE artist.artistID = "$ArtistID3";
$query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));;
$name = mysqli_fetch_assoc($query);
echo "<p>" . $name["artistID"] . $name["artistName"] . "</p>";
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr>
<th>Genre</th>
<th>CD Identification</th>
<th>Title</th>
<th>Price</th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($query)) {
echo "<tr>";
echo "<td>" . $row['cdGenre']. "</td>";
echo "<td>" . $row['cdID']. "</td>";
echo "<td>" . $row['cdTitle']. "</td>";
echo "<td>" . $row['cdPrice']. "</td>";
echo "</tr>";
}
?>
<table width="70%" cellpadding="5" cellspace="5">
</table>
另一種方法是爲所有的藝術家查詢一次,並通過直接從關聯數組過濾藝術家打造的3個表。但正如你所說,你剛剛開始,我認爲這個解決方案會做到這一點。
花空數組,然後同時申請一次,並指定所有的值該數組,然後使用該數組變量分爲三個不同的表 –
有什麼區別3桌之間?有沒有需要做的分組?您可能需要進行3個獨立的查詢。或者在for循環中定義一個用於在3分割結果的邏輯。 –
@LeonelAtencio是的3個表顯示3個不同的藝術家的名字,並有3個不同的歌曲 –