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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Option values from database

    Hi all,

    I'm currently retrieving some values from a database and placing them into a drop down box with the following code:

    PHP Code:
    <select name="lea">
        <option value="">-- Search All --</option>
    <?php
        $query 
    mysql_query("SELECT Leaname FROM schools ORDER BY Leaname ASC");
            while (
    $result mysql_fetch_array($query)) {
                
    ?>
                <option value="<?php echo $result['Leaname']?>"><?php echo $result['Leaname']?></option>
    <?php
        
    }
    ?>
    </select>
    The problem is, because there is more than one instance of each value, the option box is returning something like this:

    Value 1
    Value 1
    Value 1
    Value 1
    Value 1
    Value 1
    Value 2
    Value 2
    Value 2
    Value 2
    Value 3
    Value 3
    Value 3

    Where as I only want it to show one instance of each, i.e.

    Value 1
    Value 2
    Value 3

    Is there a php workaround for this?

    I know I could manually code the values into the html, but there are some many it would take a long time!

    Thanks,

    Greens85
    Last edited by greens85; 10-21-2009 at 04:54 PM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    mysql, not php:
    Code:
    select distinct Leaname FROM schools ORDER BY Leaname ASC
    in php you can use array_uniq but is a complication. in my opinion.

    best regards

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by oesxyl View Post
    mysql, not php:
    Code:
    select distinct Leaname FROM schools ORDER BY Leaname ASC
    in php you can use array_uniq but is a complication. in my opinion.

    best regards
    Thats done the trick, many thanks for your help, its much appreciated!

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by greens85 View Post
    Thats done the trick, many thanks for your help, its much appreciated!
    you are welcome. A suggestion, each time you mix sql with any server side script get all you can from sql in the shorter you can. This will make your life easy.
    I say this because I read in another post of yours that you are not so good with sql, try to improve because you will need it,

    best regards


  •  

    Posting Permissions

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