mugelloblue
mugelloblue

Reputation: 137

Creating variables based on a dropdown selection from extracted XML data

I am extracting data from a XML document using DOMDocument and foreach loops. For one of the data points I am after, style, I created an associative array with the style name and style ID. I then exported the style name into a <select> dropdown. What I am trying to figure out is how to create a variable with style ID based on which style name was selected in the dropdown. Here is what I have so far:

The two style names are 5dr Avant Wgn Man and 5dr Avant Wgn Auto. These are exported to the dropdown. Their respective style Id's are 292015 and 292016. Thus, the associative array would look something like this 5dr Avant Wgn Man => 292015 and 5dr Avant Wgn Auto => 292016.

What I want to achieve is when 5dr Avant Wgn Man is selected from the dropdown, 292015 is set equal to $variable. Thus, echo $variable; will display 292015. And vice versa for the other style.

Here is my PHP:

<html>

<body>

<?php

 $xml = file_get_contents('data.xml');
 $dom = new DOMDocument();
 $dom->loadXML($xml);

?>

<select>
  <option selected="selected">Choose style</option>
    <?php
      foreach ( $dom->getElementsByTagName('style') as $styletwo )          {
        $styleid = $styletwo->getAttribute("name");
        $styleData [$styleid]= $styletwo->getAttribute("id");;
       }
      foreach ($styleData as $k=>$v){
      ?>
       <option><?php echo $k;?> </option>
      <?php
       }
      ?>
 </select>

</body>

</html>

Here is the XML document I am pulling from:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
  <VehicleDescription country="US" language="en" modelYear="2008" bestMakeName="Audi" bestModelName="S4" bestStyleName="5dr Avant Wgn" xmlns="urn:description7b.services.chrome.com">
     <responseStatus responseCode="Successful" description="Successful"/>

     <style id="292015" modelYear="2008" name="5dr Avant Wgn Man" nameWoTrim="5dr Avant Wgn Man" mfrModelCode="8ED549" fleetOnly="false" modelFleet="false" passDoors="4" altBodyType="Station Wagon" drivetrain="All Wheel Drive">
        <division id="4">Audi</division>
        <subdivision id="5020">Audi</subdivision>
        <model id="17308">S4</model>
        <basePrice unknown="false" invoice="46137.0" msrp="49610.0" destination="775.0"/>
        <bodyType primary="true" id="7">Station Wagon</bodyType>
        <marketClass id="53">Small Wagon</marketClass>
        <acode>USB80AUC085A0</acode>
     </style>
     <style id="292016" modelYear="2008" name="5dr Avant Wgn Auto" nameWoTrim="5dr Avant Wgn Auto" mfrModelCode="8ED54L" fleetOnly="false" modelFleet="false" passDoors="4" altBodyType="Station Wagon" drivetrain="All Wheel Drive">
        <division id="4">Audi</division>
        <subdivision id="5020">Audi</subdivision>
        <model id="17308">S4</model>
        <basePrice unknown="false" invoice="47162.0" msrp="50710.0" destination="775.0"/>
        <bodyType primary="true" id="7">Station Wagon</bodyType>
        <marketClass id="53">Small Wagon</marketClass>
        <acode>USB80AUC085A1</acode>
     </style>
   </VehicleDescription>
  </S:Body>
</S:Envelope>

I think some kind of if statement with $k and $v might work but I haven't been able to figure it out. Any help would be greatly appreciated.

Upvotes: 0

Views: 44

Answers (1)

Anton
Anton

Reputation: 1057

Altering your code as follows

    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>
    <select name='my_select'>
       <option value='<?php echo $v;?>'>
       <?php echo $k;?> 
       </option>
    </select>
    </form>

Gives following HTML

  <select name='my_select'>
  <option selected="selected">Choose style</option>
           <option value='292015'>
       5dr Avant Wgn Man 
       </option>
             <option value='292016'>
       5dr Avant Wgn Auto 
       </option>
   </select>

Is that what you want?

Upvotes: 1

Related Questions