Link to home
Start Free TrialLog in
Avatar of weikelbob
weikelbobFlag for United States of America

asked on

if-else with mod and CSS

What is incorrect?:

 if ($i%2==1)
  {
     echo '<tr class=\"other\">';
  }
Avatar of sajuks
sajuks

What error are u getting ?

 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);$x++){
    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
//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>";
?>
Avatar of weikelbob

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
}
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> .....";

 
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 ...
}
Change
if ($i % 2==1)
to
if ($i % 2==0)
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>
IE gives me all green, firefox gives me nothing.

ASKER CERTIFIED SOLUTION
Avatar of sajuks
sajuks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
finally got it sajuks. I copied and pasted exactly from that .php file you sent me.

Thank you for your help

Bob