Alternating Table Colour

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Subliminal
Forum Commoner
Posts: 40
Joined: Thu Oct 23, 2003 8:12 pm

Alternating Table Colour

Post by Subliminal »

Hi I would like to have an alternating BG colour for the table created by this code, or is this done somehow in MySQL (newbie) Any help you can provide would be amazing. Thanks. javascript:emoticon(':)')

Code: Select all

<?php

// Connect To SQL Server
$connection = mysql_connect("localhost","x","x") or die ("Could Not Connect To Required Server");

// Select Database
$select_db = mysql_select_db("database") or die ("Could Not Connect To Required Database");

// Set Query As Vaiable
$result = mysql_query("SELECT * FROM test_table ORDER BY first_name, last_name"); 

// Show Table Headers
echo		"\n<center><table class='body' border='0' width='100%'>";
echo		"\n<tr>".
			"\n\t<th><div align=center></div></th>".
			"\n\t<th><div align=center>In/Out</div></th>".
			"\n\t<th><div align=left>Name</div></th>".
			"\n\t<th><div align=left>Ext</div></th>".
			"\n\t<th><div align=left>Department</div></th>".
			"\n\t<th><div align=left>Alt Phone</div></th>". 
			"\n\t<th><div align=left>Email</div></th>".
			
			"\n</tr>";

// Fetch Query Variable & Start Output In Table
while ($row = @ mysql_fetch_array($result)) 
{
echo		"\n<tr>" .
			"\n\t<td><a href=actions/toggle_status.php?carbonite_id={$row["carbonite_id"]}&in_out={$row["in_out"]}><center>Toggle</center></a>".
			"\n\t<td><center><b>{$row["in_out"]}</b></center></td>".
			"\n\t<td>{$row["first_name"]}\n\t {$row["last_name"]}</td>" .
			"\n\t<td>{$row["extension"]}</td>" .
			"\n\t<td>{$row["designation"]}</td>".
			"\n\t<td>{$row["alt_phone"]}</td>".
			"\n\t<td><a href=mailto:{$row["email"]}>{$row["email"]}</a></td>" ;		
}

// End Table
echo "\n</table></center>";

// Get # Of Table Rows & Output 
$num_carbonites = mysql_num_rows($result);
echo "\n<center><P class=body>Number Of Listed Carbon Staff Members:  <b>$num_carbonites</b></p></center>";

//Kill Connection
mysql_close($connection);

?>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Subliminal
Forum Commoner
Posts: 40
Joined: Thu Oct 23, 2003 8:12 pm

Post by Subliminal »

ah... did i mention I am a newbie... who wants to do it for me? lol hahahaha

Thanks for the link.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Tutorials: How to get out of "newbieness"
Swede78
Forum Contributor
Posts: 198
Joined: Wed Mar 12, 2003 12:52 pm
Location: IL

Post by Swede78 »

Here's another simple way of doing it, no math involved.

Code: Select all

<?php $RowBG = 'DDDDDD';  // this goes before the loop and is the 2nd color in the rotation ?>
<?php do {  // use whatever type of of loop you want ?>
<?php if( $RowBG == 'FFFFFF' ) { $RowBG = 'DDDDDD'; } else { $RowBG = 'FFFFFF'; }  // this goes inside the loop ?>
<tr> 
<td bgcolor="#<?php echo $RowBG; ?>">Bla bla bla</td>
</tr>
<?php } while ($row_Results = mysql_fetch_assoc($Results)); ?>
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Being a newbie, I wouln't get him started on getting in and out of PHP. It'll just mess scripts up imho. Here's a tutorial:

http://www.spoono.com/php/tutorials/tutorial.php?id=2

If that's still too hard for you, I recommend going to learn the basics again.

-Nay
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Shorter version of Swede78's...

Code: Select all

&lt;style&gt;
td.red &#123; background: red; &#125;
td.blue &#123; background: blue; &#125;
&lt;/style&gt;

Code: Select all

<?php
...
$style = 'red';
while ($row = mysql_fetch_array($aquery)) {
 echo '<td class="'.($style == 'red' ? 'blue' : 'red').'">...</td>';
}
...
?>
Subliminal
Forum Commoner
Posts: 40
Joined: Thu Oct 23, 2003 8:12 pm

Thanks!

Post by Subliminal »

You guys rock. All very helpful!

Thank You.
Post Reply