简体   繁体   中英

Select data from my three tables

I have three tables, one named album , while the other one is soundtrack and last but definitely not the least, is the artist .

Under one album is more than one track (obviously) and an artist name...

So this is how it should look like:
Desired Output:

 |---------------------|
 |AlbumName by Charlie |
 |---------------------|
 | 1. See you again    |
 |---------------------|
 | 2. One call Away    |
 |---------------------|
 | Album by Ed Sheeran |
 |---------------------|
 | 1. Perfect          |
 |---------------------|
 | 2. Dive             |
 |---------------------|

And below is mysqli_fetch_array() that I used along side with while-loop to populate the <table> tag.

$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'];
}

The problem is, when I ran this code... I could only get this result...

|---------------------|
|AlbumName by Charlie |
|---------------------|
|  1. See you again   |
|---------------------|
|  2. One call away   |
|---------------------|
|  1. Perfect         |
|---------------------|
|  2.  Dive           |
|---------------------|

It got the list of the soundtracks, including the soundtrack's ID right, however, things didn't do well on the Album Name

I want it just to look like the Desired Output . Any ideas? I've tried to search stackoverflow, but it seems it couldn't direct me on the same question, so I made my own. Please forgive (do not flag) me if it had an existing already, its just that I didn't know where to find it. And pleeeeease... I need an immediate and working answer. Thanks guys!

As for DATABASE Structure

album = {album_id, artist_id,album_title}
artist = {artist_id, artist_name}
track = {song_id, album_id, song_title}

UPDATE!
Okay, so this goes a little bit out of control. When I tried @swellar's suggestion this what happened. And when I undo it, the output remains the same. See the result below...

|---------------------|
|AlbumName by Charlie |
|---------------------|
|  1. See you again   |
|---------------------|
|AlbumName by Charlie |
|---------------------|
|  2. One Call Away   |
|---------------------|
|AlbumName by Charlie |
|---------------------|
|  1.  Perfect        |
|---------------------|
|AlbumName by Charlie |
|---------------------|
|  2.   Dive          |
|---------------------|

What I want is to give each Album one or more than soundtrack but it get so messed up each time I dare to change the query . How can I sanitize the mysqli_fetch_array() for it to display the desired output?

Here is your SQL

Notice I've put 3 order by in there. First by the artist name, which would keep all artist's stuff grouped together, then by album name and then by song 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
)
*/

Below is your while loop

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

Overall Output is:

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

There are better ways to go around doing this, but not knowing your work environment, skills, requirements, other code, w/e this is the best quick and dirty solution for you to base your solution of. This works to what you requested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM