Jump to content

change the tr dynamically


houssam_ballout

Recommended Posts

Hello allam creating a table that is called throughout a function in php, and this will generate a table: that table wil have a lot of rows,my question is that: I need to have 2 colors, and for example: row 1 has color 1, row 2 has color 2, row 3 has color 1, row 4 has color 2, ......just to differentiate themThanks

Link to comment
Share on other sites

If you are generating the table rows from within a for loop, you can use the index of the loop and the modulus operator to determine which css class to use for your row:

for(i = 0; i < length; i++){	if(i % 2 == 1)	{		$cssclass = "AltRow";	}	// do your table row building...}

Link to comment
Share on other sites

Here is a piece of code which reads information from a text data file and then echos it back out using the modulus (%) function to determine if the class = hi-lite is needed to change the background colour of the table row.

<table>	 <tr class="hi_lite">		 <th>Date and Time</th>		 <th>Sender</th>		 <th>Email</th>		 <th>Message</th><?phpif ($show_IP){echo '<th>IP Address</th>';   };echo '</tr>';$row=0;while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {	if ($row % 2) { $data_string .= '<tr class="hi_lite"> ';	}	else  { $data_string .= '<tr> ';	}	 	if ($show_IP){$num = count($data);		   }				else {$num = (count($data) - 1 );					   };		   		 for ($c=0; $c < $num; $c++) {			 $data_string .= '<td> ' . $data[$c] . " </td>";			   };		$data_string .= '<tr> ' . "\r\n\t";		$row++;			 };		print $data_string;		$data_string = "";fclose($handle);?></table>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...