www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > ASP

    ASP Discussion and technical support for using and deploying Active Server Pages.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 08-04-2005, 08:24 AM
    scouse scouse is offline
    Registered User
     
    Join Date: Oct 2003
    Posts: 195
    Populating a select box from database

    Hi,

    I've managed to get this working, but I just want to check if there is a better, more efficient way of doing this as it seems a little bit long winded for what I'm trying to do.

    Basically, I've got a select box for a list of cities which is populated from a database. However, I want the data that is retrieved to be ordered into different sub groups.

    For example,

    my database has the following fields:

    CITY_ID, CITY_VALUE, CITY_OPTION, CITY_AREA

    and some example data

    1, LIV, LIVERPOOL, ENGLAND
    2, CAR, CARDIFF, WALES
    3, MAN, MANCHESTER, ENGLAND
    4, GLA, GLASGOW, SCOTLAND
    5, LON, LONDON, ENGLAND
    6, SWA, SWANSEA, WALES

    When populating the select box I want it to end up like this:

    Code:
    <select Name='CITY'>
      <OPTION value='javascript:void(0);'>--------------</OPTION>
      <OPTION value='javascript:void(0);'>ENGLAND</OPTION>
      <OPTION value='javascript:void(0);'>--------------</OPTION>
      <OPTION value='LIV'>LIVERPOOL</OPTION>
      <OPTION value='MAN'>MANCHESTER</OPTION>
      <OPTION value='LON'>LONDON</OPTION>
      <OPTION value='javascript:void(0);'>--------------</OPTION>
      <OPTION value='javascript:void(0);'>WALES</OPTION>
      <OPTION value='javascript:void(0);'>--------------</OPTION>
      <OPTION value='CAR>CARDIFF</OPTION>
      <OPTION value='SWA'>SWANSEA</OPTION>
      <OPTION value='javascript:void(0);'>--------------</OPTION>
      <OPTION value='javascript:void(0);'>SCOTLAND</OPTION>
      <OPTION value='javascript:void(0);'>--------------</OPTION>
      <OPTION value='GLA'>GLASGOW</OPTION>
    </SELECT>
    At the moment I'm creating a different recordset for each area. E.g

    strENG ="Select * from city WHERE CITY_AREA='ENGLAND'"
    Set engRS = objConn.Execute(strENG)
    .
    .
    put into select box
    strWAL ="Select * from city WHERE CITY_AREA='WALES'"
    Set walRS = objConn.Execute(strWAL)
    .
    .
    add into select box
    .
    .
    and so on!

    This all works fine, but I can't help thinking it isn't the most efficient way to do it. Would there be a simpilar way of putting all the data into one recordset and then putting each record into the relevent sub group as its populating the select box?

    Sorry for the long post, but I couldn't explain it any easier

    Thanks

    Phil
    Reply With Quote
      #2  
    Old 08-04-2005, 09:16 AM
    schizo schizo is offline
    Code Monkey
     
    Join Date: Jun 2004
    Posts: 388
    What i'd probably do is add an ORDER BY to the query and order it by CITY_AREA and CITY_OPTION. This way if the current CITY_AREA is different from the previous (check with a simple if statement), then you know you should have a new section option.

    Another thing you might want to look at to make the code cleaner is the <optgroup> tag for your section titles. So you could have <optgroup label="ENGLAND"> and not need to worry about messy javascript and such. I am pretty sure you can even style these tags... not sure why so few people know about them.

    Hope this helps.
    Reply With Quote
      #3  
    Old 08-04-2005, 10:38 AM
    scouse scouse is offline
    Registered User
     
    Join Date: Oct 2003
    Posts: 195
    Hi thanks for the reply,

    First of all, I didn't know about the <OPTGROUP> thing. Cheers, thats goin to come in very handy.

    Secondly, I tried to do what you suggested, but I think my brain has finally fried and I can't get my head around the problem.

    This is the code for populating the select box:

    PHP Code:

    <Select name='CITY'>
    <%
      
    cityRS.MoveFirst
        
      Response
    .write "<optgroup label='ENGLAND'>"&vbcrlf  
      
      
    WHILE NOT cityRS.EOF
      strCITY_VALUE
    =Trim(cityRS.Fields("CITY_VALUE"))
      
    strCITY_OPTION=Trim(cityRS.Fields("CITY_OPTION"))
      
    strCITY_AREA=Trim(cityRS.Fields("CITY_AREA"))
        

      
    response.write "<OPTION value='"&strCITY_VALUE&"'>"&strCITY_OPTION&"</OPTION>"&vbcrlf    

      cityRS
    .MoveNext
      Wend
    %>
    </
    select>
    The problem is I can't figure out where to put an if statement to check whether strCITY_AREA is different from the previous record.

    I tried putting:
    Code:
    strCITY_AREA2=Trim(cityRS.Fields("CITY_AREA"))
    if strCITY_AREA2 <> strCITY_AREA then  
      Response.write "<optgroup label='"&strCITY_AREA2&"'>"&vbcrlf 
    End if
    After the cityRS.MoveNext statement and before the Wend statement, but this just created an "Exception occurred Error" so I assume I must be doing something wrong.

    Any Ideas?

    Cheers

    Phil
    Reply With Quote
      #4  
    Old 08-04-2005, 10:51 AM
    schizo schizo is offline
    Code Monkey
     
    Join Date: Jun 2004
    Posts: 388
    Try this...

    Code:
    <Select name='CITY'>
    <%
      cityRS.MoveFirst
        
      strCITY_AREA2 = ""
      
      WHILE NOT cityRS.EOF
      strCITY_VALUE=Trim(cityRS.Fields("CITY_VALUE"))
      strCITY_OPTION=Trim(cityRS.Fields("CITY_OPTION"))
      strCITY_AREA=Trim(cityRS.Fields("CITY_AREA"))
        
      if strCITY_AREA2 <> strCITY_AREA then  
        Response.write "<optgroup label='"&strCITY_AREA&"'>"&vbcrlf 
      end if
    
      response.write "<OPTION value='"&strCITY_VALUE&"'>"&strCITY_OPTION&"</OPTION>"&vbcrlf    
    
      strCITY_AREA2 = strCITY_AREA
    
      cityRS.MoveNext
      Wend
    %>
    </select>
    Reply With Quote
      #5  
    Old 08-04-2005, 11:02 AM
    scouse scouse is offline
    Registered User
     
    Join Date: Oct 2003
    Posts: 195
    Thanks so much for your help schizo,

    That works perfectly!

    Cheers

    Phil
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:29 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.