apheli0n Posted October 13, 2006 Share Posted October 13, 2006 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 1Track 1Track 2Track 3Album Name 2Track 1Track 2Track 3Currently, 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 infoforeach ($category as $cat=>$title_array){ echo("<br/><b>$cat</b> ($resultdiscog[Album_Year]) <br/>");echo("Label link <br/>");//album tracklistforeach ($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!!! Quote Link to comment https://forums.phpfreaks.com/topic/23859-mysql-query-output-not-displaying-in-foreach-loop/ Share on other sites More sharing options...
HuggieBear Posted October 13, 2006 Share Posted October 13, 2006 Try changing this:[code]while ($rowdiscog = mysql_fetch_array($resultdiscog)) $category[$rowdiscog["Album_Name"]][] = $rowdiscog["title"];//album header infoforeach ($category as $cat=>$title_array){ echo("<br/><b>$cat</b> ($resultdiscog[Album_Year]) <br/>");echo("Label link <br/>");//album tracklistforeach ($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.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23859-mysql-query-output-not-displaying-in-foreach-loop/#findComment-108390 Share on other sites More sharing options...
apheli0n Posted October 13, 2006 Author Share Posted October 13, 2006 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 1asdfasdfasdfasfdasdfasdfasdfAlbum Name 2asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfSo 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. Quote Link to comment https://forums.phpfreaks.com/topic/23859-mysql-query-output-not-displaying-in-foreach-loop/#findComment-108431 Share on other sites More sharing options...
HuggieBear Posted October 13, 2006 Share Posted October 13, 2006 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]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23859-mysql-query-output-not-displaying-in-foreach-loop/#findComment-108461 Share on other sites More sharing options...
apheli0n Posted October 13, 2006 Author Share Posted October 13, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/23859-mysql-query-output-not-displaying-in-foreach-loop/#findComment-108505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.