tzurielk Posted December 28, 2006 Share Posted December 28, 2006 I have searched thru the forum but didn't find anything that could help me.I have a database with air dates of shows from different years. My query looks like this[code]$res=mysql_query("select DISTINCT * from performance ORDER BY showDate");[/code]The db has the following fields:showIDshowDatebandIDyeardateHere is the rest of the code:[code]$num=mysql_numrows($res); while ($row = mysql_fetch_assoc($res)) { if ($prev_show_val != $row['date']) { //update the show date here so no dates repeat $prev_show_val = $row['date']; echo " "; echo '<font face=arial size=4 color=#2F4F4F>' . $row['date']. '';}}[/code]which successfully prints out only the unique airdates from the db.What i want to do is have the year listed once for each set of airdates from that year so it would display 1975 once and then all the dates from 1975. Then 1976 would display once and all the dates from that year, etc. I'm sure there is an easy solution for this with a nested loop but I can't seem to get it right. Please help!Thanks!Tzuriel Quote Link to comment https://forums.phpfreaks.com/topic/32007-help-with-nested-loops-please/ Share on other sites More sharing options...
ToonMariner Posted December 28, 2006 Share Posted December 28, 2006 [code]<?php$num=mysql_numrows($res); $prev_show_val = NULL;$prev_year = NULL;while ($row = mysql_fetch_assoc($res)) { if ($prev_year != $row['yea']) { //update the year here so no years repeat $prev_year = $row['date']; echo '<font face="arial" size="6" color="#2F4F4F">' . $row['year']. '</font>'; } if ($prev_show_val != $row['date']) { //update the show date here so no dates repeat $prev_show_val = $row['date']; echo '<font face="arial" size="4" color="#2F4F4F">' . $row['date']. '</font>'; }}?>[/code]Try not to use teh font tag though - its deprecated - use <span> or better still h1 h2 etc and STYLE them with your css. Quote Link to comment https://forums.phpfreaks.com/topic/32007-help-with-nested-loops-please/#findComment-148585 Share on other sites More sharing options...
conker87 Posted December 28, 2006 Share Posted December 28, 2006 [quote author=ToonMariner link=topic=120086.msg492391#msg492391 date=1167271437]Try not to use teh font tag though - its deprecated - use <span> or better still h1 h2 etc and STYLE them with your css.[/quote]Oops *whistles* Quote Link to comment https://forums.phpfreaks.com/topic/32007-help-with-nested-loops-please/#findComment-148587 Share on other sites More sharing options...
tzuriel Posted December 28, 2006 Share Posted December 28, 2006 [quote author=ToonMariner link=topic=120086.msg492391#msg492391 date=1167271437][code]<?php$num=mysql_numrows($res); $prev_show_val = NULL;$prev_year = NULL;while ($row = mysql_fetch_assoc($res)) { if ($prev_year != $row['yea']) { //update the year here so no years repeat $prev_year = $row['date']; echo '<font face="arial" size="6" color="#2F4F4F">' . $row['year']. '</font>'; } if ($prev_show_val != $row['date']) { //update the show date here so no dates repeat $prev_show_val = $row['date']; echo '<font face="arial" size="4" color="#2F4F4F">' . $row['date']. '</font>'; }}?>[/code]Try not to use teh font tag though - its deprecated - use <span> or better still h1 h2 etc and STYLE them with your css.[/quote]Thanks for the suggestion. I changed [code]<?phpif ($prev_year != $row['yea']) { //update the year here so no years repeat $prev_year = $row['date'];?>[/code]to[code]<?phpif ($newyear != $row['year']) { //update the year here so no years repeat $newyear = $row['year'];?>[/code]And it works the way I want it to. Maybe that's what you meant and just made a typo. Either way, thanks for pointing me in the right direction!Tzuriel Quote Link to comment https://forums.phpfreaks.com/topic/32007-help-with-nested-loops-please/#findComment-148623 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.