Jump to content

mysql query output not displaying in foreach loop


apheli0n

Recommended Posts

Hello,

I am a newbie to php and to this forum...I'm posting this question after hours of troubleshooting with no found solution. My background is in ColdFusion which i am fairly fluent with. I'm basically trying to do the php equivalent of this coldfusion code...

[code]
<cfoutput query="bla" group="AlbumName">
#AlbumName#
<cfoutput>
#ListOfTracks#
</cfoutput>
</cfoutput>
[/code]

The output i'm going for is:

Album Name 1
Track 1
Track 2
Track 3

Album Name 2
Track 1
Track 2
Track 3

Currently, the php code i'm using is this:



[code]

//Discog query
$sqldisog = "SELECT Albums.*, Tracks.* FROM Albums LEFT JOIN Tracks on Albums.Album_Name = Tracks.AlbumName WHERE ArtistName = '$aName'";
$resultdiscog = mysql_query($sqldisog)
                or die("Query failed: " . mysql_error());

while ($rowdiscog = mysql_fetch_array($resultdiscog)) $category[$rowdiscog["Album_Name"]][] = $rowdiscog["title"];
//album header info

foreach ($category as $cat=>$title_array)
{     

echo("<br/><b>$cat</b> ($resultdiscog[Album_Year]) <br/>");
echo("Label link <br/>");

//album tracklist
foreach ($title_array as $title)
echo("$rowdiscog[TrackOrder]) asdf<br>");
}
[/code]

This php code is outputting the Album name correctly (which is currently set to $cat) and is even outputting the correct number of "asdf<br>" for each album, but for some reason i cant output any of the other data returned in the query by using...$rowdiscog[ColumnName]. Can someone please show me how to do this properly?

Thanks!

Thanks!!!
Link to comment
Share on other sites

Try changing this:

[code]
while ($rowdiscog = mysql_fetch_array($resultdiscog)) $category[$rowdiscog["Album_Name"]][] = $rowdiscog["title"];
//album header info

foreach ($category as $cat=>$title_array)
{     

echo("<br/><b>$cat</b> ($resultdiscog[Album_Year]) <br/>");
echo("Label link <br/>");

//album tracklist
foreach ($title_array as $title)
echo("$rowdiscog[TrackOrder]) asdf<br>");
}
[/code]

To this:

[code]
while ($rowdiscog = mysql_fetch_array($resultdiscog)){
  $category[$rowdiscog['Album_Name']] = $rowdiscog['title'];

  //album header info
  foreach ($category as $cat => $title_array){
      echo "<br/><b>$cat</b> {$resultdiscog['Album_Year']} <br/>";
      echo "Label link <br/>";

      //album tracklist
      foreach ($title_array as $title){
        echo "$rowdiscog['TrackOrder'])asdf<br>";
      }
  }
}
[/code]

All I've done is tidy up your code.  I'll post and easier way for you shortly.

Regards
Huggie
Link to comment
Share on other sites

Thanks for your help...though the cleaned up code you posted doesn't work quite right...its almost a step backwards because it repeats the album name for each track in the album.

Right now, with the original code i posted, the output is like this...

Album Name 1
asdf
asdf
asdf
asfd
asdf
asdf
asdf

Album Name 2
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf

So its outputting the number of rows for each album correctly, but i just cant figure out how to output the track names from the query.

Link to comment
Share on other sites

No problem, assuming the column that holds the track name is called Track_Name then give this a try

[code]<?php
//Discog query
$sqldisog = "SELECT Albums.*, Tracks.* FROM Albums LEFT JOIN Tracks on Albums.Album_Name = Tracks.AlbumName WHERE ArtistName = '$aName' ORDER BY Albums.Album_Name";
$resultdiscog = mysql_query($sqldisog) or die("Query failed: " . mysql_error());

$album_title = "null";
while ($rowdiscog = mysql_fetch_array($resultdiscog)){
  if (is_null($album_title) || strcmp($album_title,$rowdiscog['Album_Name']) !=0 ){
      $album_title = $rowdiscog['Album_Name'];
     
      // Album header info
      echo "<br>$album_title - $rowdiscog['Album_Year']<br>\n";
  }
  // Track listing
  echo "$rowdiscog['Track_Name']<br>\n"; // Change this variable to the name of your column
}
?>[/code]

Regards
Huggie
Link to comment
Share on other sites

Thanks! I was able to get it to work from that code...now i can define all the variables I want from the query by using this code:

[code]
$album_title = "null";
while ($rowdiscog = mysql_fetch_array($resultdiscog)){
  if (is_null($album_title) || strcmp($album_title,$rowdiscog['Album_Name']) !=0 ){   
      // Album header info
  $album_title = $rowdiscog['Album_Name'];
  $album_year = $rowdiscog['Album_Year'];<br />
  $album_label = $rowdiscog['LabelName'];
  $labelurl = $rowdiscog['LabelURL'];
      echo "<br><b>$album_title</b> ($album_year)<br>\n";
  }
  // Track listing
    $TrackName = $rowdiscog['TrackName'];
  $TrackNum = $rowdiscog['TrackOrder'];
  echo "$TrackNum) $TrackName<br>\n";
}
[/code]
Link to comment
Share on other sites

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.