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 10 of 10
  1. #1
    Pie
    Pie is offline
    New Coder
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sorting Array Alphabetically For Select Box

    Hey guys,

    I have some array code which I have put into a select box. I am trying to sort the array so it's alphabetical (not by key), but I don't seem to be able to figure it out. Here's my code:

    <select name="ethnicity" id="ethnicity" class="">
    <option<?php echo empty($ethnicity)?' selected="selected"':''?> value="" >Choose</option>
    <?php
    foreach (array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian") as $v)
    echo "<option".($ethnicity==$v?' selected="selected"':'')." value=\"".htmlspecialchars($v)."\">".htmlspecialchars($v)."</option>\n";
    ?>
    </select>

    It's usually easy to use sort(); but in this case I'm a little confused as to how I'd sort this without errors. Could you guys show me how you'd do it?

    Thanks.
    Pie

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    It's usually easy to use sort(); but in this case I'm a little confused as to how I'd sort this without errors. Could you guys show me how you'd do it?
    PHP Code:
    <?php
    $options
    =array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian");
    $options=sort($options);
    foreach ( 
    $options as $v)
    echo 
    "<option".($ethnicity==$v?' selected="selected"':'')." value=\"".htmlspecialchars($v)."\">".htmlspecialchars($v)."</option>\n";
    ?>
    ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Pie
    Pie is offline
    New Coder
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks abduraooft I can see that's a better way of doing it but perhaps I'm doing something wrong as it does not work....this is what I am trying....

    <select name="ethnicity" id="ethnicity" class="">
    <option<?php echo empty($ethnicity)?' selected="selected"':''?> value="" >Choose</option>
    <?php
    $options=array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian");
    $options=sort($options);
    foreach ( $options as $v)
    echo "<option".($ethnicity==$v?' selected="selected"':'')." value=\"".htmlspecialchars($v)."\">".htmlspecialchars($v)."</option>\n";
    ?>
    </select>

    Thanks.

  • #4
    Pie
    Pie is offline
    New Coder
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyone?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Sort takes a reference to an array and returns a boolean to indicate a success. In other words, it sorts an array in place and does not return a sorted array:
    PHP Code:
    $options=array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian");
    sort($options);
    foreach ( 
    $options as $v
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Yeah assigning it back to $options was the slip up. Fou-Lu's solution will work fine
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    Pie
    Pie is offline
    New Coder
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Fou-Lu but I don't understand that code, how would that fit with my code???:

    <select name="ethnicity" id="ethnicity" class="">
    <option<?php echo empty($ethnicity)?' selected="selected"':''?> value="" >Choose</option>
    <?php
    $options=array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian");
    $options=sort($options);
    foreach ( $options as $v)
    echo "<option".($ethnicity==$v?' selected="selected"':'')." value=\"".htmlspecialchars($v)."\">".htmlspecialchars($v)."</option>\n";
    ?>
    </select>

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    PHP Code:
    <select name="ethnicity" id="ethnicity" class="">
    <option<?php echo empty($ethnicity)?' selected="selected"':''?> value="" >Choose</option>
    <?php
    $options
    =array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian");
    sort($options);
    foreach ( 
    $options as $v)
    echo 
    "<option".($ethnicity==$v?' selected="selected"':'')." value=\"".htmlspecialchars($v)."\">".htmlspecialchars($v)."</option>\n";
    ?> 
    </select>
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #9
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Quote Originally Posted by Fou-Lu View Post
    Sort takes a reference to an array and returns a boolean to indicate a success. In other words, it sorts an array in place and does not return a sorted array:
    Ah.. my bad
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #10
    Pie
    Pie is offline
    New Coder
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's the one Jay. Thanks everyone.


  •  

    Posting Permissions

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