If($i%2 == 0) HELP

I am trying to implement code similar to the one below to the to my table. It will make one row one color and the next row another color and so on. Any help would be appreciated.


	if($i%2 == 0)
		$color= '#F0F0F0';
	else
		$color = '#F8F8F8';
		echo '<tr bgcolor="'.$color.'"><td>';

Need it to work here:


<?php
$sql="SELECT map_id,map_name,theorder FROM sitemap ORDER BY theorder ASC";
$result=mysql_query($sql);
$j=1;
while($abc=mysql_fetch_array($result)){
echo "<tr id='tt$j'>";
echo"<td>";
echo "<a href='screen3.php?cat_id=$abc[map_id]'><img src='images/subcategory.gif' class='rokib' ></a>";
echo"</td>";
echo"<td width='40%'>"; 
echo"<input type='text' name='mapname[$abc[map_id]]'  value='$abc[map_name]' size='58'>";
echo"</td>";
echo"<td>";
echo"<input type='text' name='order[$abc[map_id]]'  value='$abc[theorder]' size='60'>";
echo"</td>";
echo"<td>";
echo"<input type='checkbox' class='deleterow' name='deleterow[]' value='$abc[map_id]' onClick='bgSingle($j,this.checked);'>";
echo"</td>";
echo"</tr>";
$j++;
}
?>

Something like this should do the trick:


$sql='SELECT map_id,map_name,theorder FROM sitemap ORDER BY theorder ASC';
$result=mysql_query($sql);

$j=1;

while($abc=mysql_fetch_array($result)){

$bgcolor = ($j%2===0) ? "F0F0F0" : "F8F8F8";

echo '<tr id="tt'.$j.'" style="background-color:#'.$bgcolor.';">';
echo'<td>';
echo '<a href="screen3.php?cat_id="'.$abc[map_id].'"><img src="images/subcategory.gif" class="rokib"></a>';
echo'</td>';
echo'<td width="40%">'; 
echo'<input type="text" name="mapname['.$abc[map_id].']"  value="'.$abc[map_name].'" size="58">';
echo'</td>';
echo'<td>';
echo'<input type="text" name="order['.$abc[map_id].']"  value="'.$abc[theorder].'" size="60">';
echo'</td>';
echo'<td>';
echo'<input type="checkbox" class="deleterow" name="deleterow[]" value="'.$abc[map_id].'" onClick="bgSingle($j,this.checked);">';
echo'</td>';
echo'</tr>';

$j++;

}

there’s a couple things I did there. first, switched around your use of single quotes and double quotes. Using single quotes on attributes is not valid HTML syntax. So, you can either use single quote strings and include double quotes, or use double quote strings and escape any double quotes that appear within the string. Using single quote strings tends to be simpler when writing/echoing HTML.

The other thing was using a CSS tag instead of an HTML tag for specifying the row’s background color.

Lastly, the $bgcolor statement is simply a shorthand if-else statement.


<?php
$sql="SELECT map_id,map_name,theorder FROM sitemap ORDER BY theorder ASC";
$result=mysql_query($sql);
$j=1;
$cycle = false;
while($abc=mysql_fetch_array($result)){
echo "<tr id=\\"tt$j\\" style=\\"background-color:#".($cycle =! $cycle?'F0F0F0':'F8F8F8').";\\">";
echo"<td>";
echo "<a href='screen3.php?cat_id=$abc[map_id]'><img src='images/subcategory.gif' class='rokib' ></a>";
echo"</td>";
echo"<td width='40&#37;'>"; 
echo"<input type='text' name='mapname[$abc[map_id]]'  value='$abc[map_name]' size='58'>";
echo"</td>";
echo"<td>";
echo"<input type='text' name='order[$abc[map_id]]'  value='$abc[theorder]' size='60'>";
echo"</td>";
echo"<td>";
echo"<input type='checkbox' class='deleterow' name='deleterow[]' value='$abc[map_id]' onClick='bgSingle($j,this.checked);'>";
echo"</td>";
echo"</tr>";
$j++;
}
?>

Both work. Thank you!

If you check the documentation you’ll find that single quotes and double quotes are both as equally valid. The rest of what you said is spot on though, in regards to the convention of using single quotes for strings so that the HTML double quotes are easier to do.

As a side note, you’d probably save a bit of space in the Source if you used CSS to implement a class for the rows (.row0 and .row1), and then at the bottom of the code manipulate $j as $j = ($j + 1) % 2; and just call it as the element attribute class=‘row".$j."’ (Yes, i always make it a habit to escape my variables inside strings)

So they are. I’ve long since forgotten about that.

This one only affects the first row. All the other rows are the same color.

This one does the job but affects my next page. I get the following error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home1/website/public_html/tools/sitemap/screen3.php on line 107

Here is the code for that php file. The red text is line 107:


<?php
$sql="SELECT * FROM sitemap_sub WHERE map_id=".$cat_id." ORDER BY theorder ASC";
$result=mysql_query($sql);
$j=1;
[COLOR="Red"]while($abc=mysql_fetch_array($result)){[/COLOR]
echo "<tr id='tt$j'>";
echo"<td>";
echo"<input type='text' name='mapname[$abc[sub_id]]'  value='$abc[map_name]' size='40'>";
echo"</td>";
echo"<td>"; 
echo"<input type='text' name='mapurl[$abc[sub_id]]'  value='$abc[map_url]' size='45'>";
echo"</td>";
 echo"<td>"; 
echo"<input type='text' name='adddate[$abc[sub_id]]'  value='$abc[add_date]' size='15'>";
echo"</td>";
echo"<td>";
echo"<input type='text' name='order[$abc[sub_id]]'  value='$abc[theorder]' size='12'>";
echo"</td>";
echo"<td>";
echo"<input type='checkbox' class='deleterow' name='deleterow[]' value='$abc[sub_id]' onClick='bgSingle($j,this.checked);'>";
echo"</td>";
echo"</tr>";
$j++;
}
?>

Something must be wrong with the SQL statement. What is $cat_id

Not really sure. Someone wrote the script for me