|
|
|
Creating a table with alternating background colours is not too difficult a task using the conventional <td bgcolor="#ffff00"> Having created such a table, it becomes a tedious job to insert a row, particularly near the top of the table - all the <td> instructions have to be changed, otherwise two rows are the same colour. Obvious eh? Using this code, all the table data is entered in the same place, one row after another, rather than scattered throughout the table itself. Background colours can be changed simply by redefining the appropriate STYLE. Rows can be inserted without having to do anything about the cell background colours. They change automatically, wherever the new row is entered. Even if you don't want to insert new rows, this code will enable you to change the table colours if you decide you want a new colour scheme, without having to troll through each and every cell in the table. With all the data in one place, you won't have to go looking for that either. It works like this ... The row number is set. Changing it from odd to even makes the table start with the opposite colour. Text to be displayed in the table is set up by defining variables, one for each table cell. A small block of code is then set up for each required row (my example is for the variable $t3) echo "<tr>"; if ( is_integer($row/2) ) { echo "<td class='T3'>$t3</td>"; } else { echo "<td class='T1'>$t3</td>"; } $row = $row + 1 ; In simple terms it says - if the row number divided by 2 is a whole number, use the data stored in variable t5 and display it using Class T3. If the row number divided by 2 is not a whole number use Class T1. To insert a row in the table, define a new unused variable, say $t7 (if in doubt, copy the format of a definition already there and just change the number). Cut and paste any block of code (as defined above), inserting it where the new row is required. Change the variable number to $t7 The php code can be downloaded from here. Right click, save to a folder on your PC and open with Wordpad. Notepad will open it, but it looks a mess. Have fun!! Change table colours demo - related to this page, but a separate script. Problems? Email to php@wight.info |