Hello everyone,
My table is not showing right I have look over my code but I do not see anything wrong. So I figure I post it on here to see if any of you guys see it. Here it is

<html>
<head>
<title> Html Tables</title>
</head>
<body>
<?php
echo "<tablewidth=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
  if ($x%4==0) {
    if ($rowcount%2==0){
       echo "</tr>";
       echo "<tr bgcolor=\"#FFCCFF\">\n";
       }
       else{
       	echo"</tr>";
       	echo "<tr bgcolor=\"#FFFFFF\">\n";
       	}
       	$rowscount++;
       }
     }
  echo "</tr>";
  echo "</table>";
  ?>
  </body>
  </html>

I got it to show up but the colors are off i need row 2 and 4 to show the colors.

Recommended Answers

All 20 Replies

how about having a look at the generated HTML-Output? That helps you to understand the problem.

So more less if I am understanding correctly I need to do a a text/css on it?

I am suppose to use modulus operator to set the background.

Member Avatar for diafol

You need to change $rowscount++ to $rowcount++

This is the output:

<tablewidth="50%" cellpadding="2" cellspacing="2" border="1"><tr bgcolor="#FFFFFF"> <td align="center" style="width:100px">1</td> 
 <td align="center" style="width:100px">2</td> 
 <td align="center" style="width:100px">3</td> 
 <td align="center" style="width:100px">4</td> 
</tr><tr bgcolor="#FFCCFF"> 
 <td align="center" style="width:100px">5</td> 
 <td align="center" style="width:100px">6</td> 
 <td align="center" style="width:100px">7</td> 
 <td align="center" style="width:100px">8</td> 
</tr><tr bgcolor="#FFFFFF"> 
 <td align="center" style="width:100px">9</td> 
 <td align="center" style="width:100px">10</td> 
 <td align="center" style="width:100px">11</td> 
 <td align="center" style="width:100px">12</td> 
</tr><tr bgcolor="#FFCCFF"> 
</tr></table

Is that what you expect? I think you also need a space between table and width in

<tablewidth

. That's why you're getting no colours, because HTML doesn't recognise the code as part of a table.

I am getting color to the table but not in the even rows just the color across the middle this is my code again. I am suppose to use modulus operators to set the background color. here is my code again.

<html>
<head>

<title> Html Tables</title>

</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
  if ($x%4==0) {
    if ($rowcount%2==0){
       echo "</tr>";
       echo "<tr bgcolor=\"#5CCDC9\">\n";
       }
       else{
       	echo "</tr>";
       	echo "<tr bgcolor=\"#FFFFFF\">\n";
       	}
       	$rowcount++;
       }
     }
  echo "</tr>";
  echo "</table>";
  ?>
  </body>
  </html>

I just don't understand how to get the evens to do the color of the blue instead of the center.

Member Avatar for diafol

You've only got 3 rows! So how do you expect to get colour into rows 4 and 6? I'm lost.
Can you post the expected html output. Just hand code it if you can. I'll be able to see what you're up to then.

Or post a mockup image.

Ok I am suppose to have four rows three cells per row. Which in mine i have 4 rows and 3 in each my rows are lable
1 2 3 4
5 6 7 8
9 10 11 12
I am sure that is what is asking. cause the cells in row 2 and 4 should have the blue backgound color(use modulus of the row which is (%2).
And then the odd numbers should have the background color of white.
I believe that is what mine shows beside my color blue is in the middle row and not where they are suppose to be.

Ok I trying to understand this stuff since i am new. Maybe I have my tables and rows mixed up. I am also suppose to use css class to change the bgcolot of the alternating rows so this is what I have so far I just dont know how to put that is the PHP.

Member Avatar for diafol

Like I said, if you post a mockup image of what you're trying to achieve, it should be simple to produce the code for it. And yes, using CSS is better than using bgcolor. The difference to the code will be trivial as you'll be concerned with the classname attribute (I assume) as opposed to the bgcolor value.

This is the new one that I am trying to do.. here is my new code

<html>
<head>

<title> Html Tables</title>
<style type=<\"text/css\">
.even { bgcolor:#5CCDC9; }
.odd { bgcolor:#FFFFFF; }
</style>
</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
  if ($x%4==0) {
    if ($rowcount%2==0){
       echo "</tr>";
       echo "<tr class=\"even\">\n";
       }
       else{
       	echo "</tr>";
       	echo "<tr class=\"odd\">\n";
       	}
       	$rowcount++;
       }
     }
  echo "</tr>";
  echo "</table>";
  ?>
  </body>
  </html>

do you understand this better..

OK this is what I have so far?

<html>
<head>

<title> Html Tables</title>
<style type=<\"text/css\">
.even { bgcolor:#5CCDC9; }
.odd { bgcolor:#FFFFFF; }
</style>
</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
  if ($x%4==0) {
    if ($rowcount%2==0){
       echo "</tr>";
       echo "<tr class=\"even\">\n";
       }
       else{
       	echo "</tr>";
       	echo "<tr class=\"odd\">\n";
       	}
       	$rowcount++;
       }
     }
  echo "</tr>";
  echo "</table>";
  ?>
  </body>
  </html>

Sorry I am a newbie at this. I am not understand how to write the php in it yet.

Member Avatar for diafol

No, this is not what I was talking about. You've pretty much posted the same code every time. I'm talking about the html output you expect to see or include a mock-up image of what it should look like in the browser. For example:

I have no clue what you are talking about

Member Avatar for diafol

I think the feeling's mutual. OK, I'll leave it somebody else. Good luck with it though.

Ok I am still working on this. I know i need to create a css on my work this is what I have so far.I am still confused about this with PHP. If someone would be nice enough to explain so i can understand. I think I did the Css right but then i dont know to me right now it looks like a mess.But can someone please help me understand. The table is suppose to have this a width of 100 px, content is to be centered ,each cell contains its corresponding number ( first cell contains "1", second contains "2", etc ). The cells in rows 2 and 4 should have a background color of light blue (use modulus of the row (%2)).The cells in the odd rows should have a background color of white. I am also suppose to use separate counter to keep track of the numbers of rows. Use modulus operators to set background color. use CSS class to change the background of the alternating rows,get the table to print correctly before changing the background color. I though i will share all this so people have a better understanding of what I am suppose to do.
Here is my code so far:

<html>
<head>
<title>Table Example</title>
<style type="text/css">
.altrow { background-color:#5CCDC9; }
.odd { background-color:#FFFFFF;}
</style>
</head>
<body>
<table width="50%" cellpadding="0" cellspacing="0" border="1">
<tr class="odd>
	<td>1</td>
	<td>3</td>
	<td>5</td>
</tr>
<tr class="altRow">
	<td>2</td>
	<td>4</td>
	<td>6</td>
</tr>
<tr class="odd">
	<td>7</td>
	<td>9</td>
	<td>11</td>
</tr>
<tr class="altRow">
	<td>8</td>
	<td>10</td>
	<td>12</td>
</tr>
<?php
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>;
for ($x%4==0) {
    if ($rowcount%2==0){
       echo "</tr>";
       echo "<tr class=\"even\">\n";
       }
       else{
       	echo "</tr>";
       	echo "<tr class=\"odd\">\n";
       	}
       	$rowcount++;
       }
     }
  echo "</tr>";
  echo "</table>";
  ?>
  </body>
  </html>
Member Avatar for diafol

Blinkin hell, that's the html output I was referring to.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style>
  td{
     width: 100px;
     text-align: center;
  }
  .odd{
     background-color: green;
  }
  .even{
     background-color: yellow;
  }


</style>

</head>
<body>

<table>
<?php
for($x=1;$x<=12;$x+=3){
  $class = ($x%2 == 0) ? 'even' : 'odd';
  echo "<tr class=\"$class\"><td>$x</td><td>" . ($x + 1) . "</td><td>" . ($x+ 2) . "</td></tr>";
}
?>
</table>
</body>
</html>

try that. Zebra striping is easy with css3 (nth-child), but browser support is dodgy.

Ok now i need to get the "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">"; now do i put that in the table part or do i do an echo for it?

Ok thank you so much for your help.I got it all put together thanks.

Just a piece of suggestion for you. I think you should use CSS rather than using CSS properties in PHP because it gets a little harder to debug a piece of combined code. It's like a container inside another inside another. If one was broken, you would have to fix 1st one then 2nd then 3rd and so on.

Good luck with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.