Page 1 of 1

mysql query output not displaying in foreach statement

Posted: Thu Oct 12, 2006 5:55 pm
by apheli0n
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: Select all

<cfoutput query="bla" group="AlbumName">
#AlbumName#
<cfoutput>
#ListOfTracks#
</cfoutput>
</cfoutput>
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: Select all

//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>");
}
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 somehow i cannot output the track names or any other data returned by using $rowdiscog[ColumnName]. Can someone be so kind as to point me in the right direction?

Thanks!!!

Posted: Thu Oct 12, 2006 6:11 pm
by n00b Saibot
You only store Album Name & Title in the following code, then you're looking to output all of the data.

Code: Select all

while ($rowdiscog = mysql_fetch_array($resultdiscog)) $category[$rowdiscog["Album_Name"]][] = $rowdiscog["title"];
change your while loop to store all the items you need to display later.

Posted: Fri Oct 13, 2006 12:01 am
by apheli0n
Thanks for your reply. Unfortunately I was unable to figure out how to store the other fields so I can display them later. Would you mind showing me how i can do that?