Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: list box

  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    list box

    i know its possible using php to create a list box populated from a database and this i have done in the past.
    What i want to do now is:
    I have records that have colour options in a single field seperated by a comma eg red,yellow,green,blue
    What i want to do is add each of those colours into a list box using php pulling that data from the db. These colours are different for each record in the db and are all in one field. Is this doable?

    Thanks

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Loop over the query result set:
    PHP Code:
    <?php
    $b 
    = array();
    while (
    $row mysql_fetch_array($resultMYSQL_ASSOC)) {
        
    $color $row['color']
        
    $a explode(','$color);
        
    $b array_merge(array_diff($a$b), $b);
    }
    echo 
    '<pre>';
    print_r($b);
    echo 
    '</pre>';
    ?>
    In $b you will have an array with all the distinct colors.

  • #3
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thats great i will try that out later, how would i create a list box reflecting those colours now?

  • #4
    New Coder
    Join Date
    Nov 2002
    Location
    Wien Österreich//Vienna Austria
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    by using CSS. every item with an individual style

  • #5
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
      <?php
      $col 
    explode(','$row_rs['colour']);
      
    ?>
      <select name="mySel">
          <?php
        
    foreach($col as $opt){
        
    ?>
          <option style="background-color: <?php echo $opt?>" value="<?php echo $opt?>"><?php echo $opt?></option>
        <?php
        
    }
        
    ?>
      </select>


  •  

    Posting Permissions

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