View Full Version : alternate colors.
Mhtml
10-09-2002, 11:20 AM
I'm trying to loop through data in a database and have the table rows alternate colors like you see in a lot of places on the web.
I've no idea how to do it, I've seen a script which used MOD but I can't figure it out.
Alekz
10-09-2002, 11:35 AM
Hi,
Simply define a boolean variable outside the loop
alt = false;
then in the loop body check for this variable
if(alt){
write(<td class="alt_td_class">)
alt = false
}
else{
write(<td class="td_class">)
alt = true
}
Lame, but... works...
Alex
Mhtml
10-09-2002, 12:01 PM
How about in VBscript?
Hmm, that looks to difficult to me (so I guess I'm just somewhat stupid :) )
I use that feature a lot and it's really easy.
The script you saw could have bin mine. MOD = is the part after your decimal point, after deviding with an intiger.
if the value for row = 5, then rowMOD 2 = .50
So wat I do is start the loop by checking wat this quotient (?, sorry, i'm not a native speaker) is. The even rows get color one, the odd ones get color 2. I do this by opening a new row en defining the backgroundcolor.
My code for a table with alternating backgroundcolors for each row ('rij' = dutch for row)
dim rij
rij = "0"
response.write("<b>Zoekresultaat:</b> <a href='javascript:history.back();'>Nieuwe selectie</a><br><br>")
response.write("<table border='0' cellpadding='5' cellspacing='0' width='95%'>")
response.write("<tr bgcolor='#c6d8df'>")
response.write("<td><b>Naam(contactpersoon)</b></td><td><b>Firmanaam</b></td><td align='center'><b>Acties</b></td>")
do while rsKlanten.EOF=false 'voor elke rij uit recordset één lijn in tabel. Achtergrondkleur variëren door te kijken naar rest bij deling
if rij MOD 2 = 0 then 'even rijen
response.write("<tr bgcolor='#ddf1f9'>")
else
response.write("<tr bgcolor='#e0ebeb'>")
end if
response.write("<td>" &server.HTMLEncode (rsKlanten.Fields("naam")) &" "& server.HTMLEncode (rsKlanten.Fields("voornaam")) &"</td>")
response.write("<td>" &server.HTMLEncode (rsKlanten.Fields("firmanaam")) &"</td>")
response.write("<td align='right'><a href=order_klant.asp?klantnummer=" &rsKlanten.Fields("klantnummer") & ">Order invoeren </a><br>")
response.write("<a href=gegevens_klant_bekijken.asp?klantnummer=" &rsKlanten.Fields("klantnummer") & ">Klantengegevens bekijken/aanpassen </a></td>")
response.write("</tr>")
rij = rij+1
rsKlanten.MoveNext
loop
response.write("</table>")
All the code you need for this alternating backgroundfeature is :
dim rij
rij = "0"
if rij MOD 2 = 0 then 'even rows
response.write("<tr bgcolor='#ddf1f9'>")
else
response.write("<tr bgcolor='#e0ebeb'>")
end if
Mhtml
10-09-2002, 12:32 PM
That is exactly the code that I saw, except for the comment.
Thanks, I got it working.
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.