Jump to content

Help with nested loops please?


tzurielk

Recommended Posts

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:

showID
showDate
bandID
year
date

Here 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
Link to comment
https://forums.phpfreaks.com/topic/32007-help-with-nested-loops-please/
Share on other sites

[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 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]<?php
if ($prev_year != $row['yea']) {
  //update the year here so no years repeat
  $prev_year = $row['date'];
?>
[/code]
to
[code]<?php
if ($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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.