Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 25th, 2007, 01:17 PM
Authorized User
 
Join Date: Apr 2006
Location: , , .
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
Reply With Quote
  #2 (permalink)  
Old April 27th, 2007, 11:46 PM
Authorized User
 
Join Date: Mar 2007
Location: , , USA.
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.


Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:14 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.