browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need HTML / CSS help?

Get answers from our community of HTML / CSS experts on BYTES! It's free.

alternate table rows

Newbie
 
Join Date: Sep 2007
Posts: 7
#1: Sep 29 '07
hey everyone,
I'm transforming my site to be database driven website for better and faster modification since it uses HTML for the moment.
My quetion is,
I have a control panel where I can control the tables of the site, some sections, edit the css...
there is a section on my site where a table has to be created to display some info with specs for the user [hosting plans]

let's take this exmple
Plan Name
Space
Bandwidth
POP3
FTP
MySQL
...

now from my control panel, i want to add:
plan 1
1 Gb
yes / or image [tick]
yes / or image
no / or image [x]

the question is as shown in the title,
I want that every row of the table to be altered from the row coming after it
so the first row will be white, the second will be gray, it will get the colors from a css file.
and I want the table header to be color fixed differnet from the rows, i.e blue

any help would be really appreciated

thanks in advance



Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 4,189
#2: Sep 29 '07

re: alternate table rows


Hi. Welcome to The Scripts!

So basically what you are trying to do is paint the background of every other row differently from the one above it? That is to say; odd row numbers one color and even numbers another?

If so then the first thing we need to know, what type of server-side script are you using? PHP? ASP? etc...
Are you perhaps planing on doing this in JavaScript?

And is there any particular reason why you posted this in a MySQL forum?
Newbie
 
Join Date: Sep 2007
Posts: 7
#3: Sep 29 '07

re: alternate table rows


hey Atli,

this is what i want, even rows color is different than the odd ones.

I'm using PHP/MySQL

Is there any else method other than the javascript one ?

I posted in the MySQL forum for the reason shown below:

Quote:
I'm transforming my site to be database driven website for better and faster modification since it uses HTML for the moment.
which i thought that the answer for my question, can be found here, where MySQL experts are


regards
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 4,189
#4: Sep 29 '07

re: alternate table rows


OK I see.

In the much popular PHP/MySQL duo, the actual coding is done in PHP. MySQL is mostly just a data storage, the database which PHP uses to store data in.

So, to solve your problem, you would need a PHP code. Something similar to this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. # Print the start of the table.
  3. echo "\n<table>";
  4.  
  5. # Loop throught while $i is less than 10
  6. # each time incrementing $i by one.
  7. for($i = 0; $i < 10; $i++) 
  8. {
  9.   # $i % 2 divides the number $i by 2 and returns the rest (0 or 1)
  10.   # which I then add one to, so this will alwasy be either 1 or 2
  11.   $color = "color". ($i % 2 + 1);
  12.  
  13.   # Print a row into the table.
  14.   echo "\n\t<tr><td class=\"$color\">Row $i</td></tr>";
  15. }
  16.  
  17. # Print the end of the table.
  18. echo "\n</table>";
  19. ?>
Now this code assumes that there are CSS classes called "color1" and "color2" that contain the color.

You will obviously need to adapt this to your own code, but you can see where I am going with this.
Newbie
 
Join Date: Sep 2007
Posts: 7
#5: Sep 29 '07

re: alternate table rows


thank you for your reply,

The code works perfectly ;)
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#6: Sep 30 '07

re: alternate table rows


Moving to the HTML/CSS forum.
drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,902
#7: Sep 30 '07

re: alternate table rows


Or:
.even{
background-color:blue
}
.odd{
background-color:yellow
}

<tr class="even">

<tr class="odd">
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#8: Sep 30 '07

re: alternate table rows


Heya, TheGenius.

Check out this article.
drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,902
#9: Sep 30 '07

re: alternate table rows


I'm aware of that article but forgot about his css selector solution.
drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,902
#10: Sep 30 '07

re: alternate table rows


Quote:

Originally Posted by pbmods

Heya, TheGenius.

Is that what they're calling me? I'll have to find this "they" person and shoot him.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#11: Sep 30 '07

re: alternate table rows


Quote:

Originally Posted by drhowarddrfine

Is that what they're calling me? I'll have to find this "they" person and shoot him.

The OP's nick is 'thegenius', and I embellished it a bit with some fancy capitals.

But we all like to think of you as "theGenius". 0:)
drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,902
#12: Sep 30 '07

re: alternate table rows


Does it pay more? .
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#13: Sep 30 '07

re: alternate table rows


Quote:

Originally Posted by drhowarddrfine

Does it pay more? .

It pays 500% what you're getting for it now!!
Reply