By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

changing row's color

P: 51
I have the code like this to show data from xml as a table.Now I want change color of each rows.for example : first row is green,second is red,third is green,next is red..........how can i do it?I had tried to do with even and odd rows, but it didn't work.any solution?Thanks

<?php
// load SimpleXML

$SECURITIES = new SimpleXMLElement('xml/bagiaTableDt.xml', null, true);
$even_row_color ="#000066";
$odd_row_color ="#FFFF33";
$rowNum=0;
if(fmod($rowNum+1,2)==0)
{$rowcolor=$even_row_color;}
else
{$rowcolor=$odd_row_color;}
echo <<<EOF
<table border=1 align="center" bgcolor="$rowcolor">

<tr bgcolor="#FFCCCC"><td>Name</td><td>SYMBOL</td> <td>Ref</td><td>Ceiling</td><td>Floor</td><td>Price1</td><td>Volume1</td><td>Price2</td><td>Volume2</td><td>Price</td><td>Volume</td> <td>Change</td></tr>
EOF;

foreach($SECURITIES as $STOCK) // loop
{

$name=$STOCK->Stock_name;
$price1=$STOCK->PriceOne;
$vol1=$STOCK->VolOne;
$price2=$STOCK->PriceTwo;
$vol2=$STOCK->VolTwo;
$ref=$STOCK->PriorClosePrice;
$vol=$STOCK->Vol;
$ceiling=$STOCK->Ceiling;
$flo=$STOCK->Floor;
$change=$STOCK->Change;
$symbol=$STOCK->Symbol_code;
$price=$STOCK->Price;

echo "<tr >";

echo "<td>$name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$symbol&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$ref&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$ceiling&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$flo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$price1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$vol1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$price2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$vol2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$price&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$vol&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
if($change==0)
{
echo "<td>$change&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b r />";
}
else
{
if($change<0)
{
echo "<td><img src='red.gif'>$change</img>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />";
}
else
{echo "<td><img src='gre (2).gif'>$change</img>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;<br />";
}
}


}
echo '</tr></table>';

?>
Mar 1 '08 #1
Share this Question
Share on Google+
3 Replies


hsriat
Expert 100+
P: 1,654
[php]<?php
// load SimpleXML

$SECURITIES = new SimpleXMLElement('xml/bagiaTableDt.xml', null, true);
/*$even_row_color ="#000066";
$odd_row_color ="#FFFF33";
$rowNum=0;
if(fmod($rowNum+1,2)==0)
{$rowcolor=$even_row_color;}
else
{$rowcolor=$odd_row_color;}
CHANGED*/
$row_color = array("#000066","#ffff33"); //CHANGE 1
echo <<<EOF //??
<table border=1 align="center"> //CHANGE 2

<tr bgcolor="#FFCCCC"><td>Name</td><td>SYMBOL</td> <td>Ref</td><td>Ceiling</td><td>Floor</td><td>Price1</td><td>Volume1</td><td>Price2</td><td>Volume2</td><td>Price</td><td>Volume</td> <td>Change</td></tr>
EOF;

//ADDED
$color_i = 0; //CHANGE 3

foreach($SECURITIES as $STOCK) // loop
{

$name=$STOCK->Stock_name;
$price1=$STOCK->PriceOne;
$vol1=$STOCK->VolOne;
$price2=$STOCK->PriceTwo;
$vol2=$STOCK->VolTwo;
$ref=$STOCK->PriorClosePrice;
$vol=$STOCK->Vol;
$ceiling=$STOCK->Ceiling;
$flo=$STOCK->Floor;
$change=$STOCK->Change;
$symbol=$STOCK->Symbol_code;
$price=$STOCK->Price;

//CHANGED
echo "<tr bgcolor=\"".$row_color[$color_i%2]."\">"; //CHANGE 4

echo "<td>$name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$symbol&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$ref&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$ceiling&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$flo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$price1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$vol1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$price2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$vol2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$price&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<td>$vol&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
if($change==0)
{
echo "<td>$change&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b r />";
}
else
{
if($change<0)
{
echo "<td><img src='red.gif'>$change</img>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />";
}
else
{echo "<td><img src='gre (2).gif'>$change</img>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;<br />";
}
}

// ADDED
$color_i++;
echo "</tr>"; //CHANGE 5

}

//CHANGED
echo '</table>'; //CHANGE 6

?>[/PHP]

Try these 6 changes... n tell me if it works...
Mar 1 '08 #2

P: 51
OMG.Thanks so much
it's perfect.PLs teach me more when i have problem.Thanks again
Mar 3 '08 #3

hsriat
Expert 100+
P: 1,654
OMG.Thanks so much
it's perfect.PLs teach me more when i have problem.Thanks again
Sure, you can ask anytime...:)

Harpreet
Mar 3 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.