Jump to content

Displaying SQL in foreach loop


Payback451

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! :)
Link to comment
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




Link to comment
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
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.