-1
我想在foreach循环中显示每个表格内容的每首歌曲,我该怎么做。在for循环中显示表格
我想在foreach循环中显示每个表格内容的每首歌曲,我该怎么做。在for循环中显示表格
不是最漂亮的解决方案,但希望它可能对你有所帮助:
require_once ("db/dbconn.php");
$sql = "SELECT * FROM artistcd NATURAL JOIN artist ORDER BY artistID, cdID";
$i = 0;
$previousArtistID = "";
while($row = $result->fetch_assoc()){ // loop through your database
if($row['artistID'] != $previousArtistID){ // if the current artist is not the previous
if($i > 0){
echo "</table>"; // if the artist is not the first, close the previous table
echo "<hr>"; // for demo
}
echo "<table>"; // start a table, with headers
echo "<tr>";
echo "<th>Genre</th>";
echo "<th>CD Identification</th>";
echo "<th>Title</th>";
echo "<th>Price</th>";
echo "</tr>";
echo "<tr>";
echo "<th colspan=\"4\">".$row['artistName']." (".$row['artistID'].")</th>"; // display the artistName/artistID
echo "</tr>";
$i++; // increment counter
$previousArtistID = $row['artistID']; // set the previousArtistID to the current artistID
}
// list all songs
echo "<tr>";
echo "<td>".$row['cdGenre']."</td>";
echo "<td>".$row['cdID']."</td>";
echo "<td>".$row['cdTitle']."</td>";
echo "<td>".$row['cdPrice']."</td>";
echo "</tr>";
}
echo "</table>"; // close the table
谢谢!有用! @justbaron –
如果你想显示所有艺术家的所有歌曲,从查询 – JustBaron
删除'GROUP BY artistID'然后使用'ORDER BY artistName,cdTitle'或者'ORDER BY artistsID,cdTitle' – JustBaron
我想分开每个艺术家的3首歌曲,这就是为什么我将它们分组的原因。由于ID相同,它会循环表中的所有数据并回显所有数据。 –