ASP – An open application environment, it enables powerful server-side scripting.  Home Web Programming ASP Alternating Row Colors

Alternating Row Colors


Alternating Row ColorsLearn how to alternate row colors from database output using ASP.

Simple enough, lets decide what this is going to do in English:
  1. Connect to the database, execute the SQL query.
  2. Run an IF...ELSE statement and use MOD 2 = 0 to figure whether the variable oddsevens is odd or even.
  3. Display a background color of black or white, depending on whether oddsevens was odd or even.
  4. Display the data.
  5. Add one to oddsevens, so the next row will be an alternating color.
  6. Move to the next row in the SQL query, continue the DO LOOP.
Here it is in PHP:

<%
Dim Conn
Dim SqlTemp
Dim oddsevens
oddsevens = 1
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.connectionstring = 
	"Provider=Microsoft.Jet.OLEDB.4.0;Data 
	Source=" & Server.MapPath("	est.mdb")& ";"
Conn.Open 
SQLTemp = "SELECT somecolumn FROM sometable"
set rstemp=Conn.execute(SQLTemp)
%>
<table><td>

<%
Do While Not rstemp.EOF
If (oddsevens MOD 2 = 0) Then 
%>
<tr bgcolor=black><font color=white>
<%= rstemp.Fields("somedata").Value %>
</font>
<%
Else
%>
<tr bgcolor=white>
<%= rstemp.Fields("somedata").Value %>
<% End If %>

</tr>
<%
intNum = intNum + 1
rstemp.MoveNext
Loop
%>
</td></table>
<%
Conn.Close
Set Conn = Nothing
%>

Not as hard as it looks, is it? Simple idea, really. Just be sure to change test.mdb, somecolumn, sometable, and the two instances of somedata to match your database.

NOTE: Had trouble connecting to the database? Click here.

Author's URL: Wilfried Schobeiri
ASP – An open application environment, it enables powerful server-side scripting. More ASP Tutorials: Featured Materials | Fresh Materials | More ASP Tutorials at LearnPHP.org

Reader's comments
comments Elangi November 16, 2005 says:
Hai,
Ths s elango to all.When i m applying the Alternating rows colors it dosen't change the colors.I m eagerly copy and pate it in my project but it s not working .Could u plz solve my query..
i send my copy of that code here.i m waiting for u r early and postive reply for u.
with regards,

Elango

code: Here
------------

<!-- #include file="databasecon.asp" -->

<html>

<%

Dim rstemp
Dim SqlTemp
Dim oddsevens
oddsevens = 1

'Set Conn = Server.CreateObject("ADODB.Connection")
Set rstemp = Server.CreateObject("ADODB.recordset")
'Conn.connectionstring =
' "Provider=Microsoft.Jet.OLEDB.4.0;Data
' Source=" &amp; Server.MapPath("test.mdb")&amp; ";"
'Conn.Open
SQLTemp = "SELECT nickname FROM winnerall"
set rstemp=Con.execute(SQLTemp)
%>

<table border="1"><td>

<%
Do While Not rstemp.EOF
If (oddsevens MOD 2 = 0) Then
%>

<tr bgcolor=black><font color=green>
<%= rstemp.Fields("nickname").Value %>
</font>

<%
Else
%>
<tr bgcolor=white>
<%= rstemp.Fields("nickname").Value %>
<% End If %>

</tr>
<%
intNum = intNum + 1
rstemp.MoveNext
Loop
%>
</tr></table>
</html>

Reply
comments Elangi November 16, 2005 says:
Hai,
Ths s elango to all.When i m applying the Alternating rows colors it dosen't change the colors.I m eagerly copy and pate it in my project but it s not working .Could u plz solve my query..
i send my copy of that code here.i m waiting for u r early and postive reply for u.
with regards,

Elango

code: Here
------------

<!-- #include file="databasecon.asp" -->

<html>

<%

Dim rstemp
Dim SqlTemp
Dim oddsevens
oddsevens = 1

'Set Conn = Server.CreateObject("ADODB.Connection")
Set rstemp = Server.CreateObject("ADODB.recordset")
'Conn.connectionstring =
' "Provider=Microsoft.Jet.OLEDB.4.0;Data
' Source=" &amp; Server.MapPath("test.mdb")&amp; ";"
'Conn.Open
SQLTemp = "SELECT nickname FROM winnerall"
set rstemp=Con.execute(SQLTemp)
%>

<table border="1"><td>

<%
Do While Not rstemp.EOF
If (oddsevens MOD 2 = 0) Then
%>

<tr bgcolor=black><font color=green>
<%= rstemp.Fields("nickname").Value %>
</font>

<%
Else
%>
<tr bgcolor=white>
<%= rstemp.Fields("nickname").Value %>
<% End If %>

</tr>
<%
intNum = intNum + 1
rstemp.MoveNext
Loop
%>
</tr></table>
</html>

Reply
Add comments to "Alternating Row Colors"

Captcha