我有三个表,一个名为album
,另一个名为soundtrack
,最后但绝对不是,artist
。
在一张专辑中,有多首曲目(显然)和艺术家名称......
所以它应该是这样的:
期望输出:
|---------------------|
|AlbumName by Charlie |
|---------------------|
| 1. See you again |
|---------------------|
| 2. One call Away |
|---------------------|
| Album by Ed Sheeran |
|---------------------|
| 1. Perfect |
|---------------------|
| 2. Dive |
|---------------------|
以下mysqli_fetch_array()
与while-loop
一起使用,以填充<table>
代码。
$query = "SELECT a.* AS Album, b.* AS Track, c.* AS Artist
FROM album a
INNER JOIN track b ON a.album_id=b.album_id
INNER JOIN artist c ON a.artist_id=c.artist_id
ORDER BY a.album_id ASC";
$result = mysqli_query($con, $query);
$c=0;
while($row = mysqli_fetch_assoc($result)) {
$album = $row['Album']." (".$row['Artist'].") ";
$track = $row['Track'];
}
问题是,当我运行此代码时...我只能得到这个结果......
|---------------------|
|AlbumName by Charlie |
|---------------------|
| 1. See you again |
|---------------------|
| 2. One call away |
|---------------------|
| 1. Perfect |
|---------------------|
| 2. Dive |
|---------------------|
它获得了配乐的列表,包括配乐的ID权利,但是,Album Name
我希望它看起来像 Desired Output 。有任何想法吗?我试图搜索stackoverflow,但它似乎无法指导我同样的问题,所以我自己做了。请原谅(不要标记)我已经存在,只是我不知道在哪里找到它。并且pleeeeease ...我需要一个即时和有效的答案。谢谢你们!
关于DATABASE结构
album = {album_id, artist_id,album_title}
artist = {artist_id, artist_name}
track = {song_id, album_id, song_title}
更新!
好的,所以这有点失控。当我尝试@ swellar的建议时,发生了什么。当我撤消它时,输出保持不变。
请参阅下面的结果...
|---------------------|
|AlbumName by Charlie |
|---------------------|
| 1. See you again |
|---------------------|
|AlbumName by Charlie |
|---------------------|
| 2. One Call Away |
|---------------------|
|AlbumName by Charlie |
|---------------------|
| 1. Perfect |
|---------------------|
|AlbumName by Charlie |
|---------------------|
| 2. Dive |
|---------------------|
我想要的是给每个Album
一个或多个音轨,但每当我敢于更改query
时它就会变得混乱。如何清除mysqli_fetch_array()
以显示所需的输出?
答案 0 :(得分:-3)
这是你的SQL
注意我在那里放了3个订单。首先是艺术家的名字,它将所有艺术家的东西组合在一起,然后是专辑名称,然后是歌曲ID。
$sql = "SELECT a.album_title AS Album,
t.song_title AS Track,
t.song_id AS ID,
n.artist_name AS Artist
FROM album AS a
INNER JOIN track AS t
ON a.album_id = t.album_id
INNER JOIN artist AS n
ON a.artist_id = n.artist_ID
ORDER BY n.artist_name ASC,
a.album_id ASC,
t.song_id ASC";
/*
That SQL will generate the following array
Array
(
[Album] => AlbumName
[Track] => See you again
[ID] => 1
[Artist] => Charlie
)
Array
(
[Album] => AlbumName
[Track] => One call Away
[ID] => 2
[Artist] => Charlie
)
Array
(
[Album] => Album
[Track] => Perfect
[ID] => 1
[Artist] => Ed Sheeran
)
Array
(
[Album] => Album
[Track] => Dive
[ID] => 2
[Artist] => Ed Sheeran
)
*/
以下是你的while循环
<table>
<?php
// Some placeholders
$album = null;
while($row = mysqli_fetch_assoc($result))
{
if($row['Album'] !== $album)
{
?>
<tr>
<td style="border: 1px solid #000000; background-color: #dedede;"><?php echo $row['Album']; ?> by <?php echo $row['Artist']; ?></td>
</tr>
<tr>
<td style="border: 1px solid #000000;"><?php echo $row['ID']; ?>: <?php echo $row['Track']; ?></td>
</tr>
<?php
}
else
{
?>
<tr>
<td style="border: 1px solid #000000;"><?php echo $row['ID']; ?>: <?php echo $row['Track']; ?></td>
</tr>
<?php
}
$album = $row['Album'];
}
?>
</table>
总体输出是:
<table>
<tr>
<td style="border: 1px solid #000000; background-color: #dedede;">AlbumName by Charlie</td>
</tr>
<tr>
<td style="border: 1px solid #000000;">1: See you again</td>
</tr>
<tr>
<td style="border: 1px solid #000000;">2: One call Away</td>
</tr>
<tr>
<td style="border: 1px solid #000000; background-color: #dedede;">Album by Ed Sheeran</td>
</tr>
<tr>
<td style="border: 1px solid #000000;">1: Perfect</td>
</tr>
<tr>
<td style="border: 1px solid #000000;">2: Dive</td>
</tr>
</table>
有更好的方法可以解决这个问题,但不了解您的工作环境,技能,要求和其他代码,这是最好的快速和肮脏的解决方案,您可以根据自己的解决方案。这符合您的要求。