Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/4222-table-help/
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
Link to comment
https://forums.phpfreaks.com/topic/4222-table-help/#findComment-14710
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
Link to comment
https://forums.phpfreaks.com/topic/4222-table-help/#findComment-14732
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
Link to comment
https://forums.phpfreaks.com/topic/4222-table-help/#findComment-14747
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.