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 ConnDim SqlTempDim oddsevensoddsevens = 1Set Conn = Server.CreateObject("ADODB.Connection")Conn.connectionstring = 	"Provider=Microsoft.Jet.OLEDB.4.0;Data 	Source=" & Server.MapPath("test.mdb")& ";"Conn.Open SQLTemp = "SELECT somecolumn FROM sometable"set rstemp=Conn.execute(SQLTemp)%><table><td><%Do While Not rstemp.EOFIf (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 + 1rstemp.MoveNextLoop%></td></table><%Conn.CloseSet 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.

Related Articles

  • Alternating Row Colors
    Theres two different ways this can be done. The first is by getting results from a mysql query, the second is going through the values of an array. Ill go over both. First the full files (ill break it up into two files, one using mysql, and one using an array)
  • Alternating Rows: Arrays
    The first thing we do here is echo out the html, again I wont go over this because theres really nothing to explain, except the css. We have two css classes, one for each of the alternating rows.
  • Alternating Rows: Mysql
    The first thing we need to do is connect to the database. I always put my db connection in a universal file, that way I can just change the variables once if I need to; but for the sake of the tutorial, im putting it in this file.
  • Alternating Backgrounds
    Alternate the backgrounds of table rows displayed from a MySQL database.
  • The Basics of Colors
    Learn everything about the basics of colors and use of colors!
  • How To Use Color Effectively When You Create A Web Site
    Colors are powerful, and create strong feelings. They can also make your web site look professional or look like a circus. Here are some powerful tips you should use every time you create a web site...
  • Colors as Visual Communicators
    People love stories. This must have originated from the time when getting tucked up in bed means a story afterwards. Fairy tales are colorful stories with colorful endings. Then came those fiction books with twists and turns and sometimes not so colorful endings...
  • Graphic Design Using Color
    Color is everywhere and conveys a message even if we dont realize it. While this message can vary by culture it pays to know what colors say in your own corner of the universe, and even what color means to your target market.
  • MySQL Join Tutorial
    What are joins and how do you make them?
  • Streamline Your Redesign: Working With Restricted Colors
    Before you start designing, have in mind a rough color scheme that you would like to apply to your web site. It seems to me there are 3 categories, or distinct styles, of coloring being applied to web pages:...

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.