Jump to content

Nesting an in/else within an if/else.. Is it my answer?


Recommended Posts

Hello!

 

Looking for a kick in the right direction...  How would I go about nesting an if/else within an if/else? As an example, let's say I have a list of music groups, and by clicking one of them, a list of their albums is displayed. (this part I can do!) But I want a list of the songs associated with that album to be displayed below the album. Perhaps nesting an if/else within an if/else is not the answer?  I have tinkered with some theoretical code, and the only time I have been able to return anything, it's a single wrong song in every other album list. Any help would be most appreciated.

 

Here's a bit of code which I've successfully used to

 

 

    $sqlquery = "SELECT song.*, album.*group.* FROM song, album, group WHERE song.song_id = album.song_id AND album.group_id = group.group_id AND group.group_id = '$group_id' ";
    $queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query !");
    $row = mysql_fetch_row($queryresult);

    $album= $row[0];
    $tdcount = 1;
    $numtd = 1; // number of cells per row
    echo '

      ';
   mysql_data_seek($queryresult,0)  ;
while($row = mysql_fetch_array($queryresult)) {

    if ($tdcount == 1)
echo ''.$row['album'],' ';
if ($tdcount == $numtd) {
echo "<br />";
$tdcount = 1;
} else {
$tdcount++;
}
}

So guess you have structure like

[pre]

group            album              song

----------        -----------        -----------

group_id  --+    album_id  --+    song_id

group      |    album        |    song

            +--<  group_id    +---< album_id

[/pre]

 

<?php
$sql = "SELECT a.album, s.song
        FROM song s 
            INNER JOIN album a ON s.album_id = a.album_id
            INNER JOIN group g ON a.group_id = g.group_id
        WHERE group_id = '$group_id'
        ORDER BY album, song";
$res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>");

$lastAlbum = '';
while (list($album, $song) = mysql_fetch_row($res))
{
    if ($lastAlbum != $album)
    {
        echo "<h3>$album</h3>";
        $lastAlbum = $album;
    }
    echo "$song <br />";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.