I am having problem with coloring alternate table rows.
print '<tr '.if(childnum%2==0){.'bgcolor="#CCFFCC">'.} else {.'bgcolor="#990000">'};
This results in the following error: "Syntax Error, unexpected T_IF in /home/ on line [...]
"
Try using if/else
logic to set a string variable to the background color you want before issuing the print statement. Then, just include that variable as the bgcolor
value when you print.
print '<tr '.(($childnum % 2 == 0) ? 'bgcolor="#CCFFCC">' : 'bgcolor="#990000">');
is better
probably childnum%2
should be $childnum % 2
if ($childnum % 2 == 0) {
$color = '#CCFFCC';
} else {
$color = '#990000';
}
print '<tr bgcolor="'.$color.'">';
Do not mix conditional statements (like if
) and language constructs (like print
) or functions.
And keep readability in mind. Remember: There will come a time when you have to maintain that code.
Try this instead:
$tr = '';
if(($childnum % 2) == 0) $tr = '<tr bgcolor="#CCFFCC">';
else $tr = '<tr bgcolor="#990000">';
print $tr;
What I usualy do to solve a problem is:
do not write the code in that manner, in few weeks you won't understand it no more, TRUST ME
if($childnum%2==0){
$mybgcolor = '#CCFFCC';
} else {
$mybgcolor = '#990000';
}
print '<tr bgcolor="'.$mybgcolor.'">';
much simpler and readable