TheCatapult Posted April 6, 2012 Report Share Posted April 6, 2012 (edited) Hi! I just want to know if it is possible to change the background of DIV in PHP after by after. I mean: white-yellow-white-yellow and so on. Here's the code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title></title> <style type="text/css"> #div{ background-color: #669999; width: 450px; font-family:"Arial", Times, serif; text-align:justify;}#news{ width: 450px; height: 450px; background-color: #99ffff; position: absolute; top: 500px; left: 0px;}</style> <body><div id="news"><div><img border="0" src="headline.jpg" alt="Programs" width="451" height="82"> <?$username="";$password="";$database=""; mysql_connect("fdb3.eu.pn",$username,$password);@mysql_select_db($database) or die( "Unable to select database");$query="SELECT * FROM news ORDER BY id DESC LIMIT 3";$result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <?$i=0;while ($i < $num) { $title=mysql_result($result,$i,"title");$content=mysql_result($result,$i,"content");$poster=mysql_result($result,$i,"poster");?> <div id="div"><b><? echo $title;?></b> <br><? echo $content; ?> <br> by <? echo $poster;?></div><?$i++;} ?></div></body></html> Every DIV has a color of #99ffff which I want to be #99ffff-FFFFFF-#99ffff and so on. Would that be possible?Thanks! Edited April 6, 2012 by MisterCatapult Link to comment Share on other sites More sharing options...
Err Posted April 6, 2012 Report Share Posted April 6, 2012 (edited) Yes. You can do this by using the $i variable. Since it is incremental by one with each loop, you can change every other color on a div. All you have to do is check if $i is even or odd. If even, do this style, if odd, do this other style. if ($i % 2 == 0) { $bck = " style=\"background:yellow;\"";else { $bck = " style=\"background:gray;\"";} Or a more compact way to do the same thing as above: $bck = ($i % 2 == 0 ) ? " style=\"background:yellow;\"" : " style=\"background: gray;\""; Then just add $bck inside your div. Edited April 6, 2012 by Err Link to comment Share on other sites More sharing options...
TheCatapult Posted April 6, 2012 Author Report Share Posted April 6, 2012 Hi, I don't know what value should be put at: $bck = ($i % 2 == 0 ) ? " style=\"background:yellow;\"" : " style=\"background: gray;\""; When I put that it does only display the letters: style="background: gray;" or yellow.. Please help me.. Thanks Link to comment Share on other sites More sharing options...
thescientist Posted April 6, 2012 Report Share Posted April 6, 2012 show your code. his example was fully realized, there really wasn't much more for you to do. there was no value for you to put anywhere, other than to make sure you add $bck to the output string so the class gets assigned. The idea is you use the modulos operator to test the remainder of dividing $i by 2, to figure out if it is even or odd, thus producing the alternating assignment of background colors. Link to comment Share on other sites More sharing options...
TheCatapult Posted April 6, 2012 Author Report Share Posted April 6, 2012 (edited) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title></title> <style type="text/css"> #div{ background-color: #669999; width: 450px; font-family:"Arial", Times, serif; text-align:justify;}#news{ width: 450px; height: 450px; background-color: #99ffff; position: absolute; top: 500px; left: 0px;}</style> <body><div id="news"><div><img border="0" src="headline.jpg" alt="Programs" width="451" height="82"> <?$username="";$password="";$database=""; mysql_connect("fdb3.eu.pn",$username,$password);@mysql_select_db($database) or die( "Unable to select database");$query="SELECT * FROM news ORDER BY id DESC LIMIT 3";$result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <?$i=0;while ($i < $num) {$bck = ($i % 2 == 0 ) ? " style=\"background:yellow;\"" : " style=\"background: gray;\"";$title=mysql_result($result,$i,"title");$content=mysql_result($result,$i,"content");$poster=mysql_result($result,$i,"poster");?> <div id="div"><? echo $bck ?><b><? echo $title;?></b> <br><? echo $content; ?> <br> by <? echo $poster;?></div><?$i++;} ?></div></body></html> Edited April 6, 2012 by MisterCatapult Link to comment Share on other sites More sharing options...
Err Posted April 6, 2012 Report Share Posted April 6, 2012 You are echoing $bck between <div> and </div>. You need to put $bck inside your <div> tag.Instead of: <div id="div"> Replace with this: <div<?php echo $bck; ?>> Link to comment Share on other sites More sharing options...
TheCatapult Posted April 6, 2012 Author Report Share Posted April 6, 2012 thnx much!~!! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now