Jump to content

pls helpp changing div back one by one


TheCatapult

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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

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

Link to comment
Share on other sites

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

Archived

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

×
×
  • Create New...