2017-02-15 124 views
-1

我想在foreach循環中顯示每個表格內容的每首歌曲,我該怎麼做。在for循環中顯示錶格

+0

如果你想顯示所有藝術家的所有歌曲,從查詢 – JustBaron

+0

刪除'GROUP BY artistID'然後使用'ORDER BY artistName,cdTitle'或者'ORDER BY artistsID,cdTitle' – JustBaron

+0

我想分開每個藝術家的3首歌曲,這就是爲什麼我將它們分組的原因。由於ID相同,它會循環表中的所有數據並回顯所有數據。 –

回答

0

不是最漂亮的解決方案,但希望它可能對你有所幫助:

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 
+0

謝謝!有用! @justbaron –