Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 25th, 2007, 01:17 PM
Authorized User
 
Join Date: Apr 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to fill drop down menu

I have a form, with 3 drop down menus on it.

The values of these drop down menus I'd like to come from an XML file that looks like the below:

Code:
<?xml version="1.0" encoding="utf-8"?>
<manufacturers>
    <manufacturer name="Audi">
        <model name="A4">
            <vtrim>1</vtrim>
        </model>
        <model name="A6">
            <vtrim>1</vtrim>
        </model>
        <model name="A8">
            <vtrim>1</vtrim>
        </model>
        <model name="Quattro">
            <vtrim>1</vtrim>
        </model>
        <model name="TT">
            <vtrim>1</vtrim>
        </model>
    </manufacturer>
</manufacturers>
Now I am not entirely sure whether or not this is valid XML. But essentially, what I want to happen is have the Manufacturer's drop down box fill with just the manufacturer names, and then when one of those is selected, it determines the values in the box below it for the Models - preferrably without reloading the page.

Is there an easy way to do this using what I have above or have I done it completely wrong?

What I've tried to do so far is do it using PHP5. Just to debug and check I could pull values from the XML data, I used the following code:

Code:
if(!$xml=simplexml_load_file('Vehicles.xml')){
                        trigger_error('Error reading XML file',E_USER_ERROR);
                    }
                    echo 'Displaying contents of XML file...<br />';
                    foreach($xml as $makes){
                        echo 'Name: '.$makes->manufacturer.' Model: '.$makes->model.'
                    Trim: '.$makes->vtrim.'<br />';
                    }


But all it prints our to screen is '; foreach($xml as $makes){ echo 'Name: '.$makes->manufacturer.' Model: '.$makes->model.' Trim: '.$makes->vtrim.'
'; } ?>

Please help, I need to get this up and running as soon as I can!

Liam Gulliver,
http://p2p.neopian-hosting.com
A New Programming Chat Forum
__________________
Liam Gulliver,
http://p2p.neopian-hosting.com
A New Programming Chat Forum
 
Old April 27th, 2007, 11:46 PM
Authorized User
 
Join Date: Mar 2007
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The XML is fine, you just have to run the structure in PHP.
For example, your foreach statement is iterating through manufacturers first so you would start off with this:
Code:
foreach ($xml as $manufacturer)
{
}
Next, you want to display the manufacturer's name:
Code:
foreach ($xml as $manufacturer)
{
   // The manufacturer's name comes from an attribute
   $m_name = $manufacturer->attributes()->name;
   echo "Manufacturer: $m_name\n<br />";
}
And finally, you need to iterate through the model:
Code:
foreach ($xml as $manufacturer)
{
   // The manufacturer's name comes from an attribute
   $m_name = $manufacturer->attributes()->name;
   echo "Manufacturer: $m_name\n<br />";

   foreach ($manufacturer as $model)
   {
      $model_name = $model->attributes()->name;
      $vtrim = $model->vtrim;

      echo "Model: $model_name<br />VTrim: $vtrim<br />";
   }
}
see.. not too bad
of course this example is hard-coded for the specific XML file that you've supplied and you'll probably have to tweek the PHP in order to read a much more complex XML file.







Similar Threads
Thread Thread Starter Forum Replies Last Post
How to auto fill a drop down janakiraman ASP.NET 1.0 and 1.1 Basics 1 July 2nd, 2007 06:58 PM
Drop down menu antoneath Beginning PHP 1 January 27th, 2006 08:45 AM
right click menu hidden by drop-drown menu Andraw HTML Code Clinic 0 March 18th, 2005 03:28 PM
drop down menu scrowler Javascript 1 January 27th, 2005 05:44 AM
Fill a drop down box with table names? morpheus Classic ASP Basics 10 April 23rd, 2004 08:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.