|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
terms in alphabetical order
Hi there,
I am building a glossary and have included all terms in a table. I have a query that displays all terms in alphabetical order (see below). My question is how do I "break up" the list of terms by including a visual break (maybe an <hr> or a big letter) after all terms that begin with a, all terms that begin with b, etc. so that the list is not one gigantic array of words? <cfquery name="get_terms" datasource="glossary"> select termID, term, definition from terms order by term </cfquery> <cfoutput query="get_terms"><a href="definition.cfm?termID=#termID#">#term#</a><br></cfoutput> Thanks, Robert |
|
#2
|
|||
|
|||
|
You can do this with conditional logic, something like
<cfset currentLetter = "A" /> <cfif left( myTerm, 1 ) neq currentLetter> <cfset currentLetter = getNextLetter( currentLetter )> ....do <hr> or whatever.... </cfif> You could write getNextLetter() as a UDF that returns the next letter of the alphabet...
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian. How to Post a Question in the Forums |
|
#3
|
|||
|
|||
|
Uh...thanks for the info. The advice sounds like something that would work. However, I've never created a user-defined function...Can you give me an example or point me to the right reference?
Thanks, R |
|
#4
|
|||
|
|||
|
Something like this:
Code:
<cffunction name="getNextLetter"> <cfargument name="aLetter" type="string" required="Yes" /> <cfset asciiValue = asc( arguments.aLetter ) /> <cfif asciiValue lte 89> <cfreturn chr( asciiValue + 1 ) /> <cfelse> <cfreturn "" /> </cfif> </cffunction> The CF documentation has an entire section on UDF's. |
|
#5
|
|||
|
|||
|
http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/udfs2.htm#wp1088159
|
|
#6
|
|||
|
|||
|
Thank you so much for this function!!! It almost works :-) The only thing that happens is that at the end of each group of words that start with a letter (let's say A), there is one word left that starts with B and then the <hr> appears and then the list of words that start with B... Is there something I am doing wrong in displaying the output? Here's is the code I have where getnextLetter is the function that you defined.
<cfoutput query="get_terms"><a href="definition.cfm?termID=#termID#">#term#</a><br> <cfif left( term, 1 ) neq currentLetter> <cfset currentLetter = getNextLetter( currentLetter )> <hr> </cfif> </cfoutput> |
|
#7
|
|||
|
|||
|
Does this work?
<cfoutput query="get_terms"> <cfif left( term, 1 ) neq currentLetter> <cfset currentLetter = getNextLetter( currentLetter )> <hr> </cfif> <a href="definition.cfm?termID=#termID#">#term#</a><br> </cfoutput> |
|
#8
|
|||
|
|||
|
It sure does!!! Thank you!
R |
|
#9
|
|||
|
|||
|
One more question for you - remember the getnextletter function that you designed for me? (see below).
<cffunction name="getNextLetter"> <cfargument name="aLetter" type="string" required="Yes" /> <cfset asciiValue = asc( arguments.aLetter ) /> <cfif asciiValue lte 89> <cfreturn chr( asciiValue + 1 ) /> <cfelse> <cfreturn "" /> </cfif> </cffunction> I still get an error, which says Just in time compilation error Invalid token found on line 11 at position 16. ColdFusion was looking at the following text: ( I guess it does not like the parenthesis in <cfreturn chr( asciiValue + 1 ) />... Any suggestions? Thank you so much again! (this is weird because it works on my localhost but not on the server...) |
|
#10
|
|||
|
|||
|
No, it runs fine for me too. Something else is wrong because it's impossible that it runs correctly locally and fails on another server, unless the other server is running another version of CF?
|
|
#11
|
|||
|
|||
|
Quote:
If the thing with the function doesn't work, you could use the following strategy. Depending on the database-system you work with the SQL will be slightly different, but the basic idea would be this: <cfquery name="get_terms" datasource="glossary"> select termID, term, definition, LEFT(term,1) as FirstLetter from terms order by term </cfquery> <cfoutput query="get_terms" group="FirstLetter"> #FirstLetter# <cfoutput><a href="definition.cfm?termID=#termID#">#term#</a><br></cfoutput> <hr> </cfoutput>
__________________
** Don't expect me to code your needs, but if I am able to help, I'm willing. Shout, grab and use the hand! ** Man can no more own the land we walk upon, as they can lay claim on the air that we breath ** DeepDown I'm addicted to structures.... ohw and music ![]() ** Almost forgot I had an account here [*o*] |
|
#12
|
|||
|
|||
|
Oh you'll end up with a HR at the end, which you can skip by:
<cfif currentrecord neq recordcount><HR></cfif> |
|
#13
|
|||
|
|||
|
Thank you sooooooooooo much! This worked out great!!!
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > terms in alphabetical order |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|