bishmedia Posted February 18, 2013 Share Posted February 18, 2013 I'm trying to sort some data drawn from an XML file but i just cant figure out how to do this?? Can anyone help?? <?php function getOptions($file, $element) { $xml = simplexml_load_file($file); $results = $xml->xpath("//result/$element"); $results = array_unique($results); $options = "<option value=''>- Choose Band Name -</option>\n"; foreach($results as $res) { $options .= "<option value='{$res}'>{$res}</option>\n"; } return $options; } ?> <form method="post" name="Band"> <select name="bandName"> <?php echo getOptions('xmldata.xml', 'bandName')?> </select> <input type="submit" name="btnSubmit" value="Search"> </form> Link to comment https://forums.phpfreaks.com/topic/274639-sorting-a-listbox-from-xml/ Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 We do not know what $results looks like echo '<pre>', print_r($results, 1), '</pre>'; Link to comment https://forums.phpfreaks.com/topic/274639-sorting-a-listbox-from-xml/#findComment-1413175 Share on other sites More sharing options...
bishmedia Posted February 18, 2013 Author Share Posted February 18, 2013 ive attached both files so you can see what i mean :-) xmldata.xml resulttest.php Link to comment https://forums.phpfreaks.com/topic/274639-sorting-a-listbox-from-xml/#findComment-1413177 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 $results = $xml->xpath("//result/bandName"); usort($results, 'cmp'); function cmp($a,$b ) { return strcmp($a, $b ); } Link to comment https://forums.phpfreaks.com/topic/274639-sorting-a-listbox-from-xml/#findComment-1413186 Share on other sites More sharing options...
bishmedia Posted February 19, 2013 Author Share Posted February 19, 2013 How do i fit this in to my current file :-( Link to comment https://forums.phpfreaks.com/topic/274639-sorting-a-listbox-from-xml/#findComment-1413369 Share on other sites More sharing options...
Barand Posted February 19, 2013 Share Posted February 19, 2013 I'd put the usort() after you get the results using xpath. Oh look, that's where it is in my post. Link to comment https://forums.phpfreaks.com/topic/274639-sorting-a-listbox-from-xml/#findComment-1413417 Share on other sites More sharing options...
bishmedia Posted February 19, 2013 Author Share Posted February 19, 2013 Im pulling my hair out with this and wished id stayed with VB from the start, ive tried various combinations with no success... <?php function getOptions($file, $element) { $xml = simplexml_load_file($file); $results = $xml->xpath("//result/bandName"); usort($results, 'cmp'); $options = "<option value=''>- Choose Band Name -</option>\n"; function cmp($a,$b ) { return strcmp($a, $b ); } foreach($results as $res) { $options .= "<option value='{$res}'>{$res}</option>\n"; } return $options; } ?> Link to comment https://forums.phpfreaks.com/topic/274639-sorting-a-listbox-from-xml/#findComment-1413426 Share on other sites More sharing options...
Barand Posted February 19, 2013 Share Posted February 19, 2013 Would you have put the "cmp" function definition there when using VB? function cmp($a,$b ) { return strcmp($a, $b ); } function getOptions($file, $element) { $xml = simplexml_load_file($file); $results = $xml->xpath("//result/$element"); usort($results, 'cmp'); $options = "\n"; foreach($results as $res) { $options .= "\n"; } return $options; } Link to comment https://forums.phpfreaks.com/topic/274639-sorting-a-listbox-from-xml/#findComment-1413429 Share on other sites More sharing options...
bishmedia Posted February 19, 2013 Author Share Posted February 19, 2013 Its not loading anything from the xml file??? <?php function cmp($a,$b ) { return strcmp($a, $b ); } function getOptions($file, $element) { $xml = simplexml_load_file($file); $results = $xml->xpath("//result/$element"); usort($results, 'cmp'); $options = "\n"; foreach($results as $res) { $options .= "\n"; } return $options; } ?> <form method="post" name="Band"> <select name="bandName"> <?php echo getOptions('xmldata.xml', 'bandName')?> </select> <input type="submit" name="btnSubmit" value="Search"> </form> <?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>"; } ?> Link to comment https://forums.phpfreaks.com/topic/274639-sorting-a-listbox-from-xml/#findComment-1413432 Share on other sites More sharing options...
Barand Posted February 20, 2013 Share Posted February 20, 2013 I don't know why, when I did a copy/paste, it lost a chunk from the middle but it should be $options .= "<option value='{$res}'>{$res}</option>\n"; Link to comment https://forums.phpfreaks.com/topic/274639-sorting-a-listbox-from-xml/#findComment-1413527 Share on other sites More sharing options...
Barand Posted February 20, 2013 Share Posted February 20, 2013 I've just run the code using your xml file and it gives duplicate bands in the option list. After the usort() line add $results = array_unique($results); Link to comment https://forums.phpfreaks.com/topic/274639-sorting-a-listbox-from-xml/#findComment-1413533 Share on other sites More sharing options...
bishmedia Posted February 20, 2013 Author Share Posted February 20, 2013 Ok thanks for your time, i was just about to approach it in 2 different ways.. 1) The XML is built with Excel so i can sort by that column. 2) Putting the results into an array Link to comment https://forums.phpfreaks.com/topic/274639-sorting-a-listbox-from-xml/#findComment-1413592 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.