weikelbob
asked on
if-else with mod and CSS
What is incorrect?:
if ($i%2==1)
{
echo '<tr class=\"other\">';
}
if ($i%2==1)
{
echo '<tr class=\"other\">';
}
//try this
<?php
echo "<table border=0 cellspacing=0 cellpadding=0>";
$num=20;
for($i=0;$i<$num;$i++){
if($i % 2 ==0)
{
$bgcolor='#f3f3f3';
}
else
{
$bgcolor='#e3e3e3';
}
// echo the table content with $bgcolor
echo "<tr bgcolor=$bgcolor><td> This is line number $i </td></tr>";
// close loop
}
// close table
echo "</table>";
?>
<?php
echo "<table border=0 cellspacing=0 cellpadding=0>";
$num=20;
for($i=0;$i<$num;$i++){
if($i % 2 ==0)
{
$bgcolor='#f3f3f3';
}
else
{
$bgcolor='#e3e3e3';
}
// echo the table content with $bgcolor
echo "<tr bgcolor=$bgcolor><td> This is line number $i </td></tr>";
// close loop
}
// close table
echo "</table>";
?>
ASKER
That's almost what I have:
for ($i=0; $i<count($name); $i++)
{
echo if ($i % 2==1)
{
echo '<tr class=\"other\">';
}
else
{
echo '<tr class=\"normal\">';
}
...echo stuff
}
for ($i=0; $i<count($name); $i++)
{
echo if ($i % 2==1)
{
echo '<tr class=\"other\">';
}
else
{
echo '<tr class=\"normal\">';
}
...echo stuff
}
ASKER
echo if should be if. Nothing happens, no error.
why an echo for the if stmnt ??
"echo if ($i % 2==1)" shouldnt that be if ($i % 2==1)
try this
if ($i % 2==1)
{
$myclass='other';
}
else
{
$myclass='normal';
}
echo "<tr class=$myclass><td> .....";
"echo if ($i % 2==1)" shouldnt that be if ($i % 2==1)
try this
if ($i % 2==1)
{
$myclass='other';
}
else
{
$myclass='normal';
}
echo "<tr class=$myclass><td> .....";
ASKER
Not getting it, here's all related code:
tr.normal
{
background-color:#FFFFFF;
}
tr.other
{
background-color:#000000;
}
...
for ($i=0; $i<count($name); $i++)
{
if ($i % 2==1)
{
$myclass='other';
}
else
{
$myclass='normal';
}
echo "<tr class=$myclass>";
echo ...
}
tr.normal
{
background-color:#FFFFFF;
}
tr.other
{
background-color:#000000;
}
...
for ($i=0; $i<count($name); $i++)
{
if ($i % 2==1)
{
$myclass='other';
}
else
{
$myclass='normal';
}
echo "<tr class=$myclass>";
echo ...
}
Change
if ($i % 2==1)
to
if ($i % 2==0)
if ($i % 2==1)
to
if ($i % 2==0)
ASKER
Nope. I think I'm overlooking something.
Check the ex at :http://www.phpfreaks.com/tutorials/5/0.php
Are u getting any output ordoes teh screen show blank>
Are u getting any output ordoes teh screen show blank>
ASKER
IE gives me all green, firefox gives me nothing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
finally got it sajuks. I copied and pasted exactly from that .php file you sent me.
Thank you for your help
Bob
Thank you for your help
Bob
if($i%2): $color = "#FFFFFF"; else: $color = "#CCCCCC"; endif;
is assume this is related to the alternate table row color ?
then use this
$s = "<table>\r\n";
for($x=0;$x<count($array);
if($x/2 == round($x/2)){
$c = "class=\"normal\"";
}else{
$c = "class=\"other\"";
}
$s .= "<tr>";
$s .= "<td $c>".$array[$x]."</td>";
$s .= "</tr>\r\n";
}
$s .= "</table>\r\n";
echo $s;
$s .= "</table>\r\n";
echo $s;
or
or Heres a simple ex:http://www.phpfreaks.com/tutorials/5/0.php