Learn how to alternate row colors from database output using ASP.Simple enough, lets decide what this is going to do in English:
- Connect to the database, execute the SQL query.
- Run an IF...ELSE statement and use MOD 2 = 0 to figure whether the variable oddsevens is odd or even.
- Display a background color of black or white, depending on whether oddsevens was odd or even.
- Display the data.
- Add one to oddsevens, so the next row will be an alternating color.
- Move to the next row in the SQL query, continue the DO LOOP.
<%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%> |
NOTE:Had trouble connecting to the database? Click here.
Related Articles
- Alternating Row ColorsTheres 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: ArraysThe 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: MysqlThe 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 BackgroundsAlternate the backgrounds of table rows displayed from a MySQL database.
- The Basics of ColorsLearn everything about the basics of colors and use of colors!
- How To Use Color Effectively When You Create A Web SiteColors 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 CommunicatorsPeople 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 ColorColor 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 TutorialWhat are joins and how do you make them?
- Streamline Your Redesign: Working With Restricted ColorsBefore 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:...
