peterbarone Posted August 3, 2007 Share Posted August 3, 2007 I'm having a problem. I'm trying to make this happen on query from my database . But if someone could help me with this I should be able to get it to work. $h1 = 4; $h2 = 5; if ($h1 == 4){$color="green";} if ($h2 == 5){$color="yellow";} print "<table><tr>"; print "<td bgcolor=$color>$h1</td>"; print "<td bgcolor=$color>$h2</td>"; print "</tr></table>"; Quote Link to comment Share on other sites More sharing options...
Grodo Posted August 3, 2007 Share Posted August 3, 2007 You forgot to escape your quotes here try this $h1 = 4; $h2 = 5; if ($h1 == 4){$color1="green";} if ($h2 == 5){$color2="yellow";} print '<table><tr>'; print '<td bgcolor="'.$color1.'">$h1</td>'; print '<td bgcolor="'.$color2.'">$h2</td>'; print '</tr></table>'; Quote Link to comment Share on other sites More sharing options...
premiso Posted August 3, 2007 Share Posted August 3, 2007 Your logic is wrong, you are contradicting yourself and $color will always be yellow since h2 does equal 5. You would need separate names for the colors or else the last true statement will always be the color, that or do an elseif, then the first true statement will always be the color. What exactly are you trying to accomplish? EDIT:: You forgot to escape your quotes here try this $h1 = 4; $h2 = 5; if ($h1 == 4){$color="green";} if ($h2 == 5){$color="yellow";} print "<table><tr>"; print '<td bgcolor='$color'>$h1</td>'; print '<td bgcolor='$color'>$h2</td>'; print '</tr></table>'; Flawed code. For one variables cannot be interpreted inside single quotes, for two that is a syntax error not adding the . for concatination and for three his original code was just fine, may not of been well formed html but it should have worked. Quote Link to comment Share on other sites More sharing options...
Grodo Posted August 3, 2007 Share Posted August 3, 2007 premiso always picking on me Eh I need to get better at this stuff Quote Link to comment Share on other sites More sharing options...
peterbarone Posted August 3, 2007 Author Share Posted August 3, 2007 What exactly are you trying to accomplish? $sh1= ($h1-$p1); if ($sh1 == -2){$color="green";} if ($sh1 == -1){$color="yellow";} if ($sh1 == 1) {$color="orange";} if ($sh1 == 0) {$color="blue";} if ($sh1 == 2) {$color="red";} if ($sh1 >= 3) {$color="white";} echo "<table><tr><td bgcolor=$color>$h1</td>"; echo "<td bgcolor=$color>$h2</td>"; echo "<td bgcolor=$color>$h3</td>"; echo "<td bgcolor=$color>$h4</td>"; echo "<td bgcolor=$color>$h5</td>"; echo "<td bgcolor=$color>$h6</td></tr></table>"; That is what I'm trying to do. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted August 3, 2007 Share Posted August 3, 2007 why not make an array <?php $headings = array(); $headings[0] = ""; $headings[1] = "yellow"; $headings[2] = "green"; $headings[3] = "blue"; echo "<table><tr> <td bgcolor=\"".$headings[$h1]."\">".$h1."</td> <td bgcolor=\"".$headings[$h2]."\">".$h2."</td> <td bgcolor=\"".$headings[$h3]."\">".$h3."</td> <td bgcolor=\"".$headings[$h4]."\">".$h4."</td> <td bgcolor=\"".$headings[$h5]."\">".$h5."</td> </tr></table>"; ?> Quote Link to comment Share on other sites More sharing options...
peterbarone Posted August 3, 2007 Author Share Posted August 3, 2007 I'll try that now Thanks Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted August 3, 2007 Share Posted August 3, 2007 Note you might want to use an associtive key (not idexial as I have shown) because you will have gaps/negative values, just use quotes around the array keys insetad Quote Link to comment Share on other sites More sharing options...
peterbarone Posted August 3, 2007 Author Share Posted August 3, 2007 one question. $h1 can equal 1-10 $h2 can equal 1-10 $h3 can equal 1-10 and so on ........... if $h1 = 4 td color needs to be yellow if $h1 = 5 td color needs to be red if $h2 = 3 td color needs to be blue if $h2 = 4 td color needs to be yellow ECT..... How can I pick the color based on the value of $h1 and a different color for the value $h2 Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted August 3, 2007 Share Posted August 3, 2007 make an array for each $head_1[1] = "yellow"; $head_1[2] = "green"; $head_2[1] = "purple"; $head_2[2] = "yellow"; make sense? Quote Link to comment Share on other sites More sharing options...
peterbarone Posted August 3, 2007 Author Share Posted August 3, 2007 That makes sense. ($sh1 == -2) {$color="green";} ($sh1 == -1){$color="yellow";} ($sh1 == 1) {$color="orange";} ($sh1 == 0) {$color="blue";} ($sh1 == 2) {$color="red";} ($sh1 >= 3) {$color="white";} But I'm very new at this and can't code it correct. Quote Link to comment Share on other sites More sharing options...
iPixel Posted August 3, 2007 Share Posted August 3, 2007 I'd put something like this within a foreloop dowhile loop $i++; if($i%2!=0) $color="green"; else $color="yellow"; Now everytime a row gets generated, it will pass the above script first, and it basically alternates the 2 colors. At least i think thats what you are looking to do . Quote Link to comment Share on other sites More sharing options...
peterbarone Posted August 3, 2007 Author Share Posted August 3, 2007 ok due to my egnorance. let me try it this way for all of you that are nice enough to help me [td bgcolor=yellow]4[td bgcolor=red]3[/td][/tr][/table][td bgcolor=green]5[/td][/tr][/table][td bgcolor=blue]6[/td][/tr][/table] I need bgcolor to be set based on the value of the cell if cell = 4 bgcolor= yellow if cell = 5 bgcolor =green Sorry I can't explain it better. I'm new Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.