Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Different Color

Status
Not open for further replies.

mainmast

Programmer
Jun 26, 2003
176
US
How can I get it so that every other table or table row be a different color like the ones in this forum? If I didn't explain this problem well enough just ask, in the past I have had problems stating what I need....

Thanks!
 
Well, I'm not a CSS master or anything, but I don't think you can do it without some sort of HTML embedded programming language like PHP or Javascript.

Otherwise you'll have to create each table entry by hand, and modify the background color yourself as you create each row.

If you're reading from a database, it would be simple enough to output your rows into a table using PHP, generating a new background color for each row.

What are you going to be doing with the table? Are you building the rows by hand or are you compiling the table from a script?

- "Delightfully confusing..." raves The New York Times

-Kas
 
You can create css classes and assign properties to those classes.

For Example:
Put something like this in your Header:
<style type=&quot;text/css&quot;>
<!--
.class1 {
background-color: blue;
color: white;
}
.class2 {
background-color: green;
color: black;
}
-->
</style>

Then assign the class to your table rows:
<table>
<tr class=&quot;class1&quot;><TD>DATA</td><td>more data</td></tr>
<tr class=&quot;class2&quot;><TD>DATA</td><td>more data</td></tr>
<tr class=&quot;class1&quot;><TD>DATA</td><td>more data</td></tr>
</table>



Hope this helps

Scarecrow
 
Ahhh... I did explain it wrong. I already know how to do assign classes to different things, but what I'm wanting to do is have it automatically place 2 different colors one right after the other like this forum. Example:

- = Blue
+ = Red

-
+
-
+
-
+

etc...

Any other suggestions?
 
kasuals: I am creating the table from a script, placing a record in each one. I only want two different colors, I already know those colors and I want to be able to have each table have one of those colors. Look at the example I did in my other reply please. Thanks anyway.

Any other suggestions?
 
Again, we'd need to know how you're building your rows in your table. Are they automatically generated by a script? Or are you building them by hand?

If you are building them by hand, then you've already answered your own question.

- &quot;Delightfully confusing...&quot; raves The New York Times

-Kas
 
Sorry, you must have posted just before mine came out.

What kind of scripting language are you doing it in?

- &quot;Delightfully confusing...&quot; raves The New York Times

-Kas
 
Kasuals: I dont know if you missed my earlier post, but I already answered your question right before you replied. Check back it should be one post before your last reply, thanks.
 
Happened again, oh well. I'm using SQL to get the text from a MSACCESS database and using ASP to write it to the screen. Hope this helps.
 
Well, I dont know asp. However in most of the C based languages, I would do the following:

(I always have TRUE and FALSE defined as 1 and 0, just to make things easier on myself. You can use anything technically. This example is in PHP.)

/*Within your loop for reading from the rows in the DB*/

$switch_color = FALSE;

/*Check to see if we need to switch colors*/
switch($switch_color) {
case FALSE:
printf(&quot;<tr class='class1'>\n&quot;);
printf(&quot;<td>Blah blah.</td>\n&quot;);
printf(&quot;</tr>\n&quot;);
$switch_color = TRUE;
break;
case TRUE:
printf(&quot;<tr class='class2'>\n&quot;);
printf(&quot;<td>Blah blah.</td>\n&quot;);
printf(&quot;</tr>\n&quot;);
$switch_color = FALSE;
break;
default:
printf(&quot;<tr class='class1'>\n&quot;);
printf(&quot;<td>Blah blah.</td>\n&quot;);
printf(&quot;</tr>\n&quot;);
$switch_color = TRUE;
break;
}

I'm not sure how familiar you are with C based languages, but if ASP is anything like them, then you should understand that. If not... then I don't know how else to help you.

- &quot;Delightfully confusing...&quot; raves The New York Times

-Kas
 
BTW, this seems to be more of an ASP question than an HTML/CSS question... little off-topic. =]

I'd try the ASP forum and see if anyone there has a solution using ASP for you.

- &quot;Delightfully confusing...&quot; raves The New York Times

-Kas
 
Oh, sorry, I thought since it had to do with styles it would be in CSS. That helps, thank you.
 
BTW, if anyone wants the answer to this question with ASP here it is:

dim color

color = &quot;#FFFFFF&quot;

Do while rs.EOF
if color =&quot;#FFFFFF&quot; then
color = &quot;#000000&quot;
else
color = &quot;#FFFFFF&quot;
End if

Response.Write(&quot;<table width=&quot; & &quot;&quot;&quot;&quot; & &quot;400&quot; & &quot;&quot;&quot;&quot; & &quot; bgcolor=&quot; & &quot;&quot;&quot;&quot; & color & &quot;&quot;&quot;&quot; & &quot; height=&quot; & &quot;&quot;&quot;&quot; & &quot;25&quot; & &quot;&quot;&quot;&quot; & &quot; class=&quot; & &quot;&quot;&quot;&quot; & &quot;outlink&quot; & &quot;&quot;&quot;&quot; & &quot;>&quot;)
Response.Write(&quot;<tr>&quot;)
Response.Write(&quot;<td>&quot;)
Response.Write(&quot;<b><a href=&quot;)
Response.Write(&quot;&quot;&quot;&quot; & &quot;whatever.asp?parentid=&quot; & rs(&quot;ParentID&quot;).Value & &quot;&quot;&quot;&quot;)
Response.Write(&quot; class=&quot; & &quot;&quot;&quot;&quot; & &quot;outlink&quot; & &quot;&quot;&quot;&quot; & &quot;>&quot; & rs(&quot;Subject&quot;).Value & &quot;</a></b>&quot;)
Response.Write(&quot;</td>&quot;)
Response.Write(&quot;</tr>&quot;)
Response.Write(&quot;</table>&quot; & &quot;<p>&quot;)
rs.movenext
loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top