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. :thumbsup:
Pie
abduraooft 01-18-2010, 09:49 AM 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
$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";
?> ?
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.
Fou-Lu 01-18-2010, 12:50 PM 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:
$options=array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian");
sort($options);
foreach ( $options as $v)
JAY6390 01-18-2010, 01:05 PM Yeah assigning it back to $options was the slip up. Fou-Lu's solution will work fine
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>
JAY6390 01-18-2010, 01:36 PM <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>
abduraooft 01-18-2010, 01:51 PM 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 :o
That's the one Jay. Thanks everyone.
|
|