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> Quote Link to comment 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>'; Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 (edited) $results = $xml->xpath("//result/bandName"); usort($results, 'cmp'); function cmp($a,$b ) { return strcmp($a, $b ); } Edited February 18, 2013 by Barand Quote Link to comment 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 :-( Quote Link to comment 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. Quote Link to comment 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; } ?> Quote Link to comment 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; } Quote Link to comment 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>"; } ?> Quote Link to comment 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"; Quote Link to comment 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); Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.