Jump to content


Photo

Displaying SQL in foreach loop


  • Please log in to reply
3 replies to this topic

#1 Payback451

Payback451
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 April 2006 - 03:22 AM

Alright, i've spent wayyy to much time figuring this one out and nothing has been solved yet. I'm creating a simple database that tracks when people were certified for various skills, and depending on how old the certification is the cell's background color changes.

        $Water = mysql_query("SELECT blowout,boat_ent,contact_res,crew_pick,resc_board,swim_tend,swim,self_resc,tether_swim,victim_stab,wading FROM c_water WHERE id = $Person[id]");
        $Water = mysql_fetch_array($Water);
        foreach($Water as $i) {
               $Now = time();
            $Lapsed = $Now - $i;
            if($i == 0) { // if no date is entered, run me
                $Temp = "<td bgcolor=\"#FF0000\">test</td>";
            }
               elseif($Lapsed <= 31535999) { //white
                $Temp = "<td bgcolor=\"#FFFFFF\">" . date('m/d/Y', $i) . "</td>";
            }                
            elseif($Lapsed <= 63071999) { //green
                $Temp = "<td bgcolor=\"#00DD00\">" . date('m/d/Y', $i) . "</td>";
            }
            elseif($Lapsed <= 94607999) { //yellow
                $Temp = "<td bgcolor=\"#FFFF00\">" . date('m/d/Y', $i) . "</td>";
            }
            elseif($Lapsed >= 94607999) { //red
                $Temp = "<td bgcolor=\"#FF0000\">" . date('m/d/Y', $i) . "</td>";
            }
            echo $Temp;
        }

The script works as it should. It goes through each value in the array that it got from the database, checks how old it is, changes it to a readable format, and displays it. That stuff works fine. The part that doesnt work fine is that for whatever elseif conditional is chosen, it runs twice. For example, if the date is old enough to be considered 'yellow' (as commented in the code) then that string is echo'd twice. If it's red, that goes twice. I've linked a screenshot of the output so you can get an idea of what i'm talking about:

[a href=\"http://img20.imageshack.us/img20/6197/ugh5gy.jpg\" target=\"_blank\"]http://img20.imageshack.us/img20/6197/ugh5gy.jpg[/a]

As you can see, all the colors and dates are in blocks of two. I hope i've explained this well enough for you, now what am I doing wrong? Why are the strings being displayed twice? I beg for your help! :)

#2 KrisNz

KrisNz
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • LocationAuckland, New Zealand

Posted 23 April 2006 - 03:49 AM

change all your if's and elseifs into switch/case statements.

#3 mb81

mb81
  • Members
  • PipPipPip
  • Advanced Member
  • 120 posts

Posted 23 April 2006 - 04:01 AM

[!--quoteo(post=367595:date=Apr 22 2006, 10:22 PM:name=Payback451)--][div class=\'quotetop\']QUOTE(Payback451 @ Apr 22 2006, 10:22 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
As you can see, all the colors and dates are in blocks of two. I hope i've explained this well enough for you, now what am I doing wrong? Why are the strings being displayed twice? I beg for your help! :)
[/quote]

Change the mysql_fetch_array() to mysql_fetch_assoc() or mysql_fetch_row(),

mysql_fetch_row() returns an array with numerical keys based on the column number
mysql_fetch_assoc() returns an array with associative keys based on the name of the MySQL field
mysql_fetch_array() returns an array with both numerical and associative keys, so each field is actually in their twice






#4 Payback451

Payback451
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 April 2006 - 03:14 PM

[!--quoteo(post=367606:date=Apr 23 2006, 12:01 AM:name=mb81)--][div class=\'quotetop\']QUOTE(mb81 @ Apr 23 2006, 12:01 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Change the mysql_fetch_array() to mysql_fetch_assoc() or mysql_fetch_row()
[/quote]

You are officially my hero, and i'm officially hating myself for spending hours looking over that code and it being such a simple mistake.

Cheers,
Andrew




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users