Jump to content

[SOLVED] formatting a table with an if else statement


skinnypuppy78

Recommended Posts

I'm trying to format a table being pulled from a MySQL database.  I'm trying to alternate colors between each column.  I may be taking the wrong approach because my knowledge on PHP and MySQL commands and their limitations are limited, but this what I tried and problem I'm having.

 

I created classes in my css to be called on in the php

 

.odd {background-color: #dfffbf;}
.even {background-color: #c0c0c0;}

 

I set my query, created a variable to hold the class for the columns ($color), and a variable to trigger the if then statement ($format).

 

$sql = "SELECT * FROM torches";
$result = mysql_query($sql, $conn) or die(mysql_error());
$color="odd";
$format=1;

 

I made the table using a foreach loop to pull the header information (which I didnt post since it works fine) and the row data the from query.

 

print "<table> \n";

while ($row=mysql_fetch_assoc($result)){
print "<tr>\n";
foreach ($row as $value){
	if ($format=1) {
		print "<td class=$color>$value</td>\n";
		$color="even";
		$format++;
	}else if (!$format=1){
		print "<td class=$color>$value</td>\n";
		$color="odd";
		$format=1;
	}else {
		print "<h2>Database Eror</h2>";
	}
}
print "</tr> \n";


}
print "</table>"

 

This is my result:

The first entry in the first row displays the .odd class color (which is what I want to happen), the second entry in the first row displays the .even class color (which I also want to happen).  Now for the problem, it doesn't repeat that pattern.  It formats the rest of the table with the .even class.  It appears that my looped if else statement works the first time through, but never switches beyond that.

 

For a visual example of my results,go here www.extremeoxygenproducts.com/tools/torches2.php

 

I don't know if I'm omitting something that causes this, or if I'm using functions out of the realm to which they are supposed to be used.  Any help or suggestions would be greatly appreciated.

 

Thanks in advance.

 

 


print "<table> \n";

$i=0;

while ($row=mysql_fetch_assoc($result)){

print "<tr>\n";

foreach ($row as $value){

	$i++;

	if (($i&1)=='1') {

		$class='odd';

		}else{

			$class='even';

			}//end if

		print "<td class=$class>$value</td>\n";

	}//end foreach

print "</tr> \n";

}//end while

print "</table>"

 

try this

 

EDIT: changed print "<td class=$color>$value</td>\n"; to print "<td class=$class>$value</td>\n";

That worked perfectly thank you!  I'm marking it as solved, but I have a question..in my quest for knowledge.

 

if (($i&1)=='1')

 

What is the '&1' portion do for the variable?  My lack of knowledge of things like that leads me to writing more lengthy code that doesn't work.

 

Again, thanks!

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.