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
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.
Link to comment
Share on other sites

[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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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