Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

<tr> random colors when eventname changes

i need my table rows to randomly change colors based on my color parameters.  is there some sort of cfstylesheet i can use for this? or is there another way?
0
phillystyle123
Asked:
phillystyle123
  • 19
  • 13
  • 5
1 Solution
 
MauseCommented:
Hi there

No there is noting like cfstylesheet!
I don''t know when your color changes but I gues the color is based on value of a CF-variable
If this is true it will be something like this:

<cfswitch expression="#VARIABLE#">
   <cfcase value="value1">
         <CFSET color='RED'>
   </cfcase>
   <cfcase value="value2">
         <CFSET color='BLUE'>
   </cfcase>
   <cfcase value="value3">
         <CFSET color='ORANGE'>
   </cfcase>
   <cfdefaultcase>
           <CFSET color='BLACK'>
   </cfdefaultcase>
</cfswitch>

and then in your table
<CFOUTPUT>
<TR style="color:#color#;">
 <TD>...............</TD>
</TR>
</CFOUTPUT>

You can also do this width style-clases so in the switch you replace
<CFSET color='BLACK'>
width:
<CFSET colorclass='class_red'>

and in your table
<TR style="color:#color#;">
width:
<TR class="#colorclass#">

hope this helps, let me know
Mause
0
 
phillystyle123Author Commented:
this is what i have so far (i'm not getting any errors but i'm also not getting random table row colors:

<cfswitch expression="#timesprices.Eventname#">
   <cfcase value="value1">
         <CFSET color='RED'>
   </cfcase>
   <cfcase value="value2">
         <CFSET color='BLUE'>
   </cfcase>
   <cfcase value="value3">
         <CFSET color='ORANGE'>
   </cfcase>
   <cfdefaultcase>
           <CFSET color='BLACK'>
   </cfdefaultcase>
</cfswitch>
            <cfoutput query="timesprices" group="Eventname">
                    <tr style="color:#color#;">
0
 
MauseCommented:
you have to put the <cfoutput query="timesprices" group="Eventname"> before the cfswitch
else he get only the first result not every row.

so

<cfoutput query="timesprices" group="Eventname">

<cfswitch expression="#timesprices.Eventname#">
   <cfcase value="value1">
         <CFSET color='RED'>
   </cfcase>
   <cfcase value="value2">
         <CFSET color='BLUE'>
   </cfcase>
   <cfcase value="value3">
         <CFSET color='ORANGE'>
   </cfcase>
   <cfdefaultcase>
           <CFSET color='BLACK'>
   </cfdefaultcase>
</cfswitch>
.
.
<tr style="color:#color#;">
.
.
</cfoutput>

greetings,
Mause
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
phillystyle123Author Commented:
thanks mause but still no change - no color in any row  - this is my code:

<cfoutput query="timesprices" group="Eventname">
            <cfswitch expression="#timesprices.Eventname#">
   <cfcase value="value1">
         <CFSET color='RED'>
   </cfcase>
   <cfcase value="value2">
         <CFSET color='BLUE'>
   </cfcase>
   <cfcase value="value3">
         <CFSET color='ORANGE'>
   </cfcase>
   <cfdefaultcase>
           <CFSET color='BLACK'>
   </cfdefaultcase>
</cfswitch>

                    <tr style="color:#color#;">
0
 
MauseCommented:
hhmm sorry try this:


<cfoutput query="timesprices" group="Eventname">
          <cfswitch expression="#timesprices.Eventname#">
   <cfcase value="value1">
         <CFSET color='Red'>
   </cfcase>
   <cfcase value="value2">
         <CFSET color='Blue'>
   </cfcase>
   <cfcase value="value3">
         <CFSET color='Orange'>
   </cfcase>
   <cfdefaultcase>
           <CFSET color='Black'>
   </cfdefaultcase>
</cfswitch>

<tr style="background-color:#color#;">
0
 
phillystyle123Author Commented:
ok now we're getting somewhere - all the trs are the default color (black)
0
 
MauseCommented:
OK now what values do #timesprices.Eventname# have???
0
 
phillystyle123Author Commented:
oh ok - i think i see what you mean -

one of my event names is "Morton Subotnick with Tony Martin"

i stuck that in as "Value1" and the tr is red. so that works, except i need it to be dynamic so it knows an Eventname is different and assigns a new color to the table row because i'm going to continue to add Eventnames.  
0
 
MauseCommented:
so there are no default eventnames?

OK what are your color parameters?
Do you also fill in a color when you add a new eventname?
or do you want a random color on every row?
0
 
phillystyle123Author Commented:
i think a random color for every row will be fine but just in case - how would i fill in a color when i add a new eventname - database field?
0
 
MauseCommented:
Yes, you then need an extra database field
so your table would look something like this:
id - int
eventname - varchar(255)
eventcolor - varchar(15)

and an extra input field in you new event form called color
You can make this filed free text or a drop down field width all the collors you want

if you have this then forget the cfswitch and the row would look like this:
<tr style="background-color:#eventcolor#;">
0
 
phillystyle123Author Commented:
can i make it random without adding the db field? so, not assigning specific colors in the db just having the tr change colors based on the eventname variable?
0
 
MauseCommented:
what do you want random or based on the eventname??

Random you can do something like this:
make a list of all colors:
<cfset allcolors="red,blue,green, black, orange">

and then
<tr style="background-color:#listgetat(allcolors, RandRange(1,listlen(allcolors, ","), ","))#;">

If you want the color be based on eventname then use the cfswitch
but then you need to know somehow the eventnames

or do you have something like an event type??? so every eventtype is a color
0
 
phillystyle123Author Commented:
thanks mause - i'll try this out when i get back and give ya the pts.

0
 
MauseCommented:
NP
let me know if you need more help

Mause
0
 
phillystyle123Author Commented:
hi mause - i'm back and i tried out the code - here's the error:

Mause
The function RandRange takes 2 arguments(s)


The error occurred while processing an element with a general identifier of (#listgetat(allcolors, RandRange(1,listlen(allcolors, ","), ","))#), occupying document position (375:29) to (375:93).


Date/Time: Mon Nov 03 02:05:11 2003
Browser: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90)
Remote Address: 141.151.20.20
HTTP Referrer: http://redcatweb2.org/calendar/event.cfm?Eventname=Morton%20Subotnick%20with%20Tony%20Martin

here's the code i used:

<cfset allcolors="red,blue,green, black, orange">
<tr style="background-color:#listgetat(allcolors, RandRange(1,listlen(allcolors, ","), ","))#;">



0
 
hartCommented:
just try this

<cfset allcolors="red,blue,green,black,orange">
<tr style="background-color:#listgetat(allcolors, RandRange(1,listlen(allcolors))#;">

Regards
Hart
0
 
hartCommented:
or u could write directly bgcolor no need to write style [bcos it doesn't work in earlier versions of  NS 4.7]

<tr bgcolor="#listgetat(allcolors, RandRange(1,listlen(allcolors))#">

Regards
Hart
0
 
MauseCommented:
sorry replace it width this:

<tr style="background-color:#listgetat(allcolors, RandRange(1,listlen(allcolors, ',')), ',')#;">

I placed one ) at the wrong position..


Hart you also miss one ) of listgetat at the end
0
 
hartCommented:
yeah u r right mause i missed one ) too :-)

but why the style tag when it can be done with bgColor???

Regards
Hart
0
 
MauseCommented:
Yes you can use that one to but I rather use style I think its easier and because he wanted something like stylesheet (well cfstylesheet)

0
 
hartCommented:
if style sheet was required then
u should have given him a class,

but its ok it doesn't matter, ur solution will work gr8 :-)

the only reason why i said this was bcos it won't some times work in versions of NS.
but if he is not bothered about cross browser compatibilities, then its ok..

just forget my comments and i will move on
have a gr8 day :-)

Regards
Hart

0
 
MauseCommented:
I know about the class I said that in one of my first posts
but I did not want to make a class for every color that why I used style...

And no hard feelings you're a great expert

Mause
0
 
hartCommented:
u don't have to make different classes fro different color.
u could have just assigned the #color# variable in the class.

anywayz forget it, bcos of me this thread has become toooo long,
thats no help to u ;-)

thanks for the compliment, feels great to hear one from good experts like you :-)

Regards
Hart
0
 
phillystyle123Author Commented:
both ideas (hart, mause) work but i think i am going to go with style. have a ? though: is there a way to make sure the same color doesn't get assigned to two consecutive rows?
0
 
MauseCommented:
You can delete the color from the list let me work this out
0
 
MauseCommented:
here you go:

<CFSET getrandomcolor = RandRange(1,listlen(allcolors, ','))>
<tr style="background-color:#listgetat(allcolors, getrandomcolor, ',')#;">
<CFSET allcolors = listdeleteat(allcolors,getrandomcolor)>

But then make sure there are enough colors in the list as there are rows else you will get an error
0
 
phillystyle123Author Commented:
how can i work this into it:

<cfset allcolors="##cccccc,##d4d590,##c2bb9c,##e4eae9,##c4ae5a">

(these are the colors i need to be random)
0
 
MauseCommented:
in style you don't need the # so it is

<cfset allcolors="cccccc,d4d590,c2bb9c,e4eae9,c4ae5a">
0
 
MauseCommented:
hhmmm
<cfset allcolors="##cccccc,##d4d590,##c2bb9c,##e4eae9,##c4ae5a">

will work I gues i don't understand you?
0
 
phillystyle123Author Commented:
sorry to be such a huge hassle mause but it's still assigning back to back <tr>s with the same color: here's the link so you can see what i mean:

http://redcatweb2.org/calendar/timesprices.cfm
0
 
MauseCommented:
What is the code you have now????
0
 
MauseCommented:
you only have 5 colors if you have more records (rows) then there is a problem if you use this code

<CFSET getrandomcolor = RandRange(1,listlen(allcolors, ','))>
<tr style="background-color:#listgetat(allcolors, getrandomcolor, ',')#;">
<CFSET allcolors = listdeleteat(allcolors,getrandomcolor)>

because it will give you an error on row 6 => there are no more colors in the list

Or do you want to use only these 5 colors?? and show them like this:
row 1 - color1
row 2 - color2
row 3 - color3
row 4 - color4
row 5 - color5
row 6 - color1
row 7 - color2
row 8 - color3
row 9 - color4
row 10 - color5

then it wil be like this:
<tr style="background-color:#listgetat(allcolors, (timesprices.curentrow mod listlen(allcolors))+1, ',')#;">
0
 
phillystyle123Author Commented:
i'm still getting back to back rows with the same color:

here's my code:

<cfoutput query="timesprices" group="Eventname">
                        <cfset allcolors="##cccccc,##d4d590,##c2bb9c,##e4eae9,##c4ae5a">
                        
            
            

            <tr style="background-color:#listgetat(allcolors, (timesprices.currentrow mod listlen(allcolors))+1, ',')#;">
0
 
MauseCommented:
what do you mean width: back to back rows ?

You getting the same colors because you have 19 records (rows) and only 5 colors
so the first 5 rows have a different color but then what color do you want to give the other 14 rows?
0
 
phillystyle123Author Commented:
got ya - so there's really no way to assign 5 colors to a dynamically generated table rows if the number of rows is going to be constantly changing. your code works great - it's just i was hoping not to have two consecutive rows be the same color. unfortunately, i only have 5 colors to work with.  
0
 
MauseCommented:
Thx,

Maybe this is an option:
Only display 5 records and a next page buttun to the next 5 records so that there are only 5 rows to display width 5 differnt colors

Mause
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 19
  • 13
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now