Jump to content

Archived

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

viper3two

table help

Recommended Posts

Just a quick question, I have a table that will display an employee's demographics.
What is the code to make the borders not visible, and also to change the cell row colors
EG: Blue first Row
Yellow second row and alternate back and forth?

Here is what I have, it works, but it is just a plain vanilla table:

$display_block .= "
<table cellpadding=3 cellspacing=2 border=1 width=98%>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Title</th>
<th>Picture</th>
</tr>";
while ($newArray = mysql_fetch_array($result)) {
$empnumber = $newArray['EmpNo'];
$firstname = $newArray['FstName'];
$lastname = $newArray['LstName'];
$title = $newArray['Title'];
$department = $newArray['HmDept'];
$picfile = $newArray['PictureFile'];
$display_block .= "<tr>
<td align=center>$lastname <br> </td>
<td align=center>$firstname <br> </td>
<td align=center>$title <br> </td>
<td align=center><img src=/EmployeeDirectory/database/$picfile> <br> </td>
</tr>";
}

and then in the HTML:

<html>
<head>
<title>Administration</title>
</head>
<body>
<p align="center"><a name="top" id="top"></a></p>
<p align="center"><a name="top" id="top"></a></p>
<p align="center"><font face="Arial" size="3">Administration</font></p>
<?php echo $display_block; ?>


</body>
</html>

Thanks for the reply in advance! You guys are awesome!
Tony

Share this post


Link to post
Share on other sites
Well you need to add some code before and after your loop to get it to alternate colors. Here is the code

first we set the row color before the loop
[code]$bgcolor = '#FFFF00'; //yellow cause you want to start with blue
[/code]

Then at the start of the loop we have a check to change the color if it is one or the other
[code]if ($bgcolor == "#FFFF00"){
  $bgcolor = "#0000FF";
} else {
  $bgcolor = "#FFFF00";
}[/code]

Now we can put everything together
[code]$bgcolor = '#FFFF00'; //yellow cause you want to start with blue
$display_block .= "
<table cellpadding=3 cellspacing=2 border=0 width=98%>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Title</th>
<th>Picture</th>
</tr>";
while ($newArray = mysql_fetch_array($result)) {
// altername the colors
if ($bgcolor == "#FFFF00"){
  $bgcolor = "#0000FF";
} else {
  $bgcolor = "#FFFF00";
}
$empnumber = $newArray['EmpNo'];
$firstname = $newArray['FstName'];
$lastname = $newArray['LstName'];
$title = $newArray['Title'];
$department = $newArray['HmDept'];
$picfile = $newArray['PictureFile'];
$display_block .= "<tr bgcolor=$bgcolor>
<td align=center>$lastname <br> </td>
<td align=center>$firstname <br> </td>
<td align=center>$title <br> </td>
<td align=center><img src=/EmployeeDirectory/database/$picfile> <br> </td>
</tr>";
}[/code]

Also the code to take out borders is border=0

Ray

Share this post


Link to post
Share on other sites
[!--quoteo(post=352154:date=Mar 6 2006, 01:51 PM:name=craygo)--][div class=\'quotetop\']QUOTE(craygo @ Mar 6 2006, 01:51 PM) [snapback]352154[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Well you need to add some code before and after your loop to get it to alternate colors. Here is the code

first we set the row color before the loop
[code]$bgcolor = '#FFFF00'; //yellow cause you want to start with blue
[/code]

Then at the start of the loop we have a check to change the color if it is one or the other
[code]if ($bgcolor == "#FFFF00"){
  $bgcolor = "#0000FF";
} else {
  $bgcolor = "#FFFF00";
}[/code]

Now we can put everything together
[code]$bgcolor = '#FFFF00'; //yellow cause you want to start with blue
$display_block .= "
<table cellpadding=3 cellspacing=2 border=0 width=98%>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Title</th>
<th>Picture</th>
</tr>";
while ($newArray = mysql_fetch_array($result)) {
// altername the colors
if ($bgcolor == "#FFFF00"){
  $bgcolor = "#0000FF";
} else {
  $bgcolor = "#FFFF00";
}
$empnumber = $newArray['EmpNo'];
$firstname = $newArray['FstName'];
$lastname = $newArray['LstName'];
$title = $newArray['Title'];
$department = $newArray['HmDept'];
$picfile = $newArray['PictureFile'];
$display_block .= "<tr bgcolor=$bgcolor>
<td align=center>$lastname <br> </td>
<td align=center>$firstname <br> </td>
<td align=center>$title <br> </td>
<td align=center><img src=/EmployeeDirectory/database/$picfile> <br> </td>
</tr>";
}[/code]

Also the code to take out borders is border=0

Ray
[/quote]

Thank you Ray. I put in your code and read through it to understand how it works now. I appreciate your help!
Tony

Share this post


Link to post
Share on other sites
[!--quoteo(post=352184:date=Mar 6 2006, 02:44 PM:name=craygo)--][div class=\'quotetop\']QUOTE(craygo @ Mar 6 2006, 02:44 PM) [snapback]352184[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Glad to help!!
[/quote]

here's a little simplified (well, at least in AMOUNT of code ;-)) version that i like to use:

[code]
$color = "#4444ee";
while ($row = mysql_fetch_array($sql)) {
  $color = $color == "#4444ee" ? "#ffffff" : "#4444ee;
  echo "<tr style='background-color: $color;'>\n";
  // then, print the rest of your row
}
[/code]

also, try to get into the habit of using quotes around the value of EVERY attribute in your markup. your code won't validate without it! for instance:
[code]
<table border="0" cellpadding="0" cellspacing="0">
[/code]

good luck

Share this post


Link to post
Share on other sites

×

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.