Jump to content

Archived

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

Payback451

Displaying SQL in foreach loop

Recommended Posts

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.

[code]
        $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;
        }
[/code]

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! :)

Share this post


Link to post
Share on other sites
change all your if's and elseifs into switch/case statements.

Share this post


Link to post
Share on other sites
[!--quoteo(post=367595:date=Apr 22 2006, 10:22 PM:name=Payback451)--][div class=\'quotetop\']QUOTE(Payback451 @ Apr 22 2006, 10:22 PM) [snapback]367595[/snapback][/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




Share this post


Link to post
Share on other sites
[!--quoteo(post=367606:date=Apr 23 2006, 12:01 AM:name=mb81)--][div class=\'quotetop\']QUOTE(mb81 @ Apr 23 2006, 12:01 AM) [snapback]367606[/snapback][/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

Share this post


Link to post
Share on other sites

×

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.