mysql query output not displaying in foreach statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
apheli0n
Forum Newbie
Posts: 2
Joined: Thu Oct 12, 2006 5:41 pm

mysql query output not displaying in foreach statement

Post 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!!!
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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.
apheli0n
Forum Newbie
Posts: 2
Joined: Thu Oct 12, 2006 5:41 pm

Post 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?
Post Reply