SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Make categories from data

    I have a query question. I have a table that has a "category" field. Records get assigned a cat when they go in.

    Cats:
    one
    two
    three

    On my main list page I have a drop down where I can view just those categories in my log. I want to populate the drop down with the different categories in the db, since I don't want to update the page if I add a new cat. Now I'm not sure if this is possible, but can I have mysql just find unique cats and return those? The way I'm creating my drop down it's sticking every record in it. In other words, I may have hundreds of records but I just want the unique cats to be returned.

    I am using asp and could do a loop but didn't know how to do it totally dynamic. Here's my function so far:
    Code:
    function getCat(val)
    
    ' SET SQL LOGIN QUERY...
    query = "SELECT cat FROM db"
    
    ' EXECUTE IT...
    Cmd.CommandText = query
    Set Results = Cmd.Execute
    
    opt = "<option value=''>- Select Cat -</option>" & vbcrlf
    if not results.BOF then
    Do while not (results.EOF)
    sel = ""
    if (lcase(val) = lcase(results.fields("cat" ))) then
    sel = " selected"
    end if
    
    opt = opt & "<option value='" & lcase(results.fields("cat" )) & "'" & sel &
    ">" & results.fields("cat" ) & "</option>" & vbcrlf
    results.movenext
    loop
    end if
    
    ' CLEAR DATA...
    set results = nothing
    
    getCat = opt
    end function

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aha!

    query = "SELECT distinct cat FROM db"


Bookmarks

Posting Permissions

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