Jump to content

Sorting a Listbox from XML


bishmedia

Recommended Posts

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

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;
}
?>

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;
}

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>";
       }
       ?>  

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.