Page 1 of 1

Alternating Table Colour

Posted: Thu Oct 23, 2003 8:12 pm
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);

?>

Posted: Thu Oct 23, 2003 8:41 pm
by nigma

Posted: Thu Oct 23, 2003 11:37 pm
by Subliminal
ah... did i mention I am a newbie... who wants to do it for me? lol hahahaha

Thanks for the link.

Posted: Fri Oct 24, 2003 7:27 am
by d3ad1ysp0rk
Tutorials: How to get out of "newbieness"

Posted: Fri Oct 24, 2003 11:22 am
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)); ?>

Posted: Fri Oct 24, 2003 11:27 am
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

Posted: Fri Oct 24, 2003 5:56 pm
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>';
}
...
?>

Thanks!

Posted: Sun Oct 26, 2003 4:58 pm
by Subliminal
You guys rock. All very helpful!

Thank You.