# Trying to sort listbox from XML file

**URL:** <https://www.phphelp.com/t/trying-to-sort-listbox-from-xml-file/19653>\
**Category:** General PHP Help\
**Created:** [February 19, 2013, 7:49pm UTC](https://www.phphelp.com/t/trying-to-sort-listbox-from-xml-file/19653 "2013-02-19T19:49:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://www.phphelp.com/uploads/default/original/2X/2/243e9ed5d93c5653ce3186f18df16e019ce38445.png) [@system](https://www.phphelp.com/u/system)\
**Post date:** [February 19, 2013, 7:49pm UTC](https://www.phphelp.com/t/trying-to-sort-listbox-from-xml-file/19653/1 "2013-02-19T19:49:38Z")

</div>

Im trying to sort a list from and XML file but it wont sort, can anyone see where im going wrong??

[php] \<?php  
function getOptions($file, $element)  
{  
$xml = simplexml\_load\_file($file);  
$results = $xml-\>xpath("//result/bandName");  
usort($results, ‘cmp’);  
function cmp($a,$b )  
{  
return strcmp($a, $b );  
}  
foreach($results as $res) {  
$options .= “{$res}\n”;  
}  
return $options;  
}  
?\>[/php]

```
          <form method="post" name="Band">
                  <select name="bandName">
                  <?php echo getOptions('xmldata.xml', 'bandName')?>
                  </select>
                  <input type="submit" name="btnSubmit" value="Search">
          </form>

```

[php] \<?php  
if (isset($\_POST[‘btnSubmit’])) {  
$xml = simplexml\_load\_file(‘xmldata.xml’);

```
    $search = $_POST['bandName'];
	
    $results = $xml->xpath("//result[bandName='$search']");
    echo "$search <br />";
    echo "<table>";
    echo "<tr>";
    echo "<td>"; 
    foreach ($results as $res) {
    echo "$res->year</td> <td style=\"width:175px\">$res->compName</td> <td style=\"width:175px\">$res->section</td><td>$res->position";
    echo "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>"; 
    
    }
    echo "</table>";
    }
    ?> [/php]
```
