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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.