Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginning Letter Alternating Color Rows

Status
Not open for further replies.

girky

Programmer
Oct 24, 2002
72
US
Hello -

I'm trying to make a page with a list of names where all of the people whose last name starts with A is colored green, B's are blue, C's are green etc. However, I dont want to just say
if LEFT((REC1("lname")),1) = "A" then
Response.Write &quot;<FONT color=green>&quot;

because then I'd have to write it out for each letter, and if there's no one in the list whose last name starts with &quot;H&quot; then the pattern will be messed up.

Is there an easy way to do this??
 
How about defining styles for each letter like so:
Code:
<style>
   .row_A{
      background-color:green;
   }
   .row_B{
      background-color:red;
   }
etc...
</style>

Then when you are outputting the rows:
Response.Write &quot;<tr class='row_&quot;&left(REC1(&quot;lname&quot;),1)&&quot;'><td>&quot;&REC1(&quot;lname&quot;)&&quot;</td></tr>&quot;

That will automatically generate classes for the style above based on the prefix row_ and the first letter of the name similar to how yuo have it above, but there is no if statement needed, and it should be easier to write. In the CSS style tags above you can use both color words and standard RGB hexadecimal colors.

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
OK
but it still doesn't solve the problem that if one letter doesn't have anyone in it, then it will mess up the sequence and I will have two of the same color next to each other. I was think about something similar to what you said but with only a row_1 and a row_2.
 
I am assuming your list is ordered so all the A's are displayed and then the B's and so on.

Have a colortoggle function

Hold the first letter of the previous name and compare it to the first letter of the current name. If they differ, call the colortoggle function to switch colors.
Code:
dim sRowColor, sPreviousFirstLetter

function colortoggle()
  if sRowColor = &quot;GREEN&quot; then
     sRowColor = &quot;BLUE&quot;
  else
     sRowColor = &quot;GREEN&quot;
  end if
end function

... Place this in your looping code.
if LEFT((REC1(&quot;lname&quot;)),1) = sPreviousFirstLetter then
  call colortoggle
end if
Response.Write &quot;<FONT color=&quot; & sRowColor & &quot;>&quot;

This will keep everything in order for letters that do not a name.

Hope this helps.

Thanks,

Gabe
 
Gabe-

Yours is just what i want, but I can't get it to work right. I don't understand how to get a value for sPreviousFirstLetter.
 
I assume you are looping through your recordset. Do something like:

sPreviousletter = &quot;A&quot;
sRowcolor = &quot;Blue&quot;
do while not rs.eof
if sPreviousletter <> left(rs.fields(&quot;name&quot;),1) then
call colortoggle
end if
.
.
other code
.
.
sPreviousletter = left(rs.fields(name),1)
rs.movenext
loop


Using this code, if your list does not start w/A, the grid will start with green instead of blue. If that's not a big deal, some combination of GabeC's and my code might do the trick.
 
Ah, sorry for the misunderstanding above, i was wondering what color Z was going to end up with, i though you were going to have individual colors for each letter :p
Glad you found an answer, sorry again,
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top