group results by first letter? Phone directory application

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default group results by first letter? Phone directory application

    I am building a phone directory for my intranet. I have a list of alphabet
    letters at the top of a page, those are links that will be anchors on the
    same page to group query results.

    My problem is this. Let say that as an example I have 5 results, Allan,
    Albertson, Angel, Bradford and Bentley.

    I want to group like this (results only shown for this)

    A
    Allan
    Albertson
    Angel

    B
    Bradford
    Bentley


    Seemed pretty easy when I started but I cannot get the grouping right by
    just the first letter.

    Any help?

    Thanks!

    Ben


    Benedict Guest

  2. Similar Questions and Discussions

    1. Changing from half letter to letter document size
      I have a document that was created in the size it will be (5.5" x 8.8") and it will be punched on the spine side. It has a slightly larger margin on...
    2. Order by results of count using 'group by'
      Hi there, I'm querying a single table with the goal of selecting the number of distinct cities of the field 'city' where the count is greater...
    3. Server cannot access application directory ... The directory does not exist or is not accessible because of security settings
      If you are using Windows XP in a Workgroup, rather than a Domain, then by default "Simple Filesharing" is turned on, and you won't see a security...
    4. Server cannot access application directory... The directory does not exist or is not accessible because of security settings.
      Hi, I have this issue with the error below. Let me explain my goal and my server environement: Goal: To have 3 separate web servers reading...
    5. Convert letter to phone keypad equivalent
      You could do it with a mathematical operation based on the ASCII value...course that's not a regex. -Tom Kinzer -----Original Message-----...
  3. #2

    Default Re: group results by first letter? Phone directoryapplication

    Ben,
    This might not be the most efficient way, but it works.

    <cfquery datasource="#request.dsn#" name="listnames">
    Select l_name, f_name from tbl_user
    Order By l_name
    </cfquery>

    <cfset myLetter = ''>
    <cfoutput query="listnames">
    <cfif left(l_name,1) EQ myLetter>
    #l_name#, #f_name#
    <br>
    <cfelse>
    <br>
    #UCase(left(l_name,1))# <br>
    <cfset myLetter = '#UCase(left(l_name,1))#'>
    #l_name#, #f_name#
    <br>
    </cfif>
    </cfoutput>

    The Albino Guest

  4. #3

    Default Re: group results by first letter? Phone directory application

    Hey that worked quite well. Now you made me think of one more thing.

    Let say I have results for A,B,C,D,F none for E.

    I want to display A-Z in the links list but only have links on letters when
    there is a result. I am showing my links list right now like this:

    <CFLOOP INDEX="alpha"FROM ="65" TO="90">
    <cfoutput><a href="###chr(alpha)#">#chr(alpha)#</a></cfoutput>
    </CFLOOP>

    This gives me A-Z with an anchor link to each letter. But if there is no E
    it just stays there, but visually there is a link.

    I would do an if statement but I am not sure where or if I need to loop
    through the query?

    Thanks,

    Ben
    "The Albino" <webforumsuser@macromedia.com> wrote in message
    news:d6fs73$e3o$1@forums.macromedia.com...
    > Ben,
    > This might not be the most efficient way, but it works.
    >
    > <cfquery datasource="#request.dsn#" name="listnames">
    > Select l_name, f_name from tbl_user
    > Order By l_name
    > </cfquery>
    >
    > <cfset myLetter = ''>
    > <cfoutput query="listnames">
    > <cfif left(l_name,1) EQ myLetter>
    > #l_name#, #f_name#
    > <br>
    > <cfelse>
    > <br>
    > #UCase(left(l_name,1))# <br>
    > <cfset myLetter = '#UCase(left(l_name,1))#'>
    > #l_name#, #f_name#
    > <br>
    > </cfif>
    > </cfoutput>
    >

    Benedict Guest

  5. #4

    Default Re: group results by first letter? Phone directoryapplication

    Ben,

    Again, might not be the most effcient.

    Brendan

    <cfoutput>
    <cfset myList = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y ,Z'>
    <cfloop list="#myList#" index="CurLtr">

    <cfquery name="qry_ChkLink" datasource="brendan">
    SELECT Distinct left(l_name,1) As Ltr
    FROM tbl_user
    WHERE left(l_name,1) = '#CurLtr#'
    </cfquery>


    <cfif qry_ChkLink.RecordCount EQ 1>
    <a href="##CurLtr##">#CurLtr#</a>&nbsp;&nbsp;
    <cfelse>
    #CurLtr# &nbsp;&nbsp;
    </cfif>
    </cfloop>
    </cfoutput>

    The Albino Guest

  6. #5

    Default Re: group results by first letter? Phone directoryapplication

    Hi Ben,

    As an alternative to the cfif ... try using a group clause on your cfoutput...
    like this... this was done with sqlserver so the left function in the query
    might not be applicable but I think most DBs have a left function...

    <cfquery name="g" datasource="#SQLDatabase#">

    select hs_name,
    left(hs_name,1) as lefty
    from hs_code
    order by hs_name

    </cfquery>

    <cfoutput query="g" group="lefty">

    <strong>#lefty#</strong><br>
    <cfoutput>
    #hs_name#<br>
    </cfoutput>

    </cfoutput>

    ultrafresh Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139