PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am trying to use an ID retrieved from the querystring to get a node from an xml file. I then need to extract each child node from that node into a variable. Here's my code:
You were on the right track, but you went too far. If you want to get all the details from the car by its id you just need to say. $car will now be an array that holds: year, model, make, price, color, comments, and milesbeginning.
<?php
$carid=(is_numeric($_GET['id']))?$_GET['id']:1; //set the id to 1 if they enter a word or something crazy
$xml = simplexml_load_file("xml/inventory.xml");
$result = $xml->xpath("dataroot/car".$id); //you could also do $xml->xpath("car".$id);
print_r($result);
?>
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="inventory.xsd" generated="2009-01-31T11:57:25">
<XMLupdateQry>
<STATUS>ACTIVE</STATUS>
<IDsuffix>465</IDsuffix>
<VIN>1G8AJ55F06Z139166</VIN>
<YEAR>2006</YEAR>
<MAKE>Saturn</MAKE>
<MODEL>Ion 2</MODEL>
<MILESBEGINNING>41853</MILESBEGINNING>
<COLOR>red</COLOR>
<OPTIONS>This car runs and drives great with a 2.2L 4 cylinder engine and an automatic transmission. It has very low miles and features CD, power locks, and AC. It is a clean little car that is ready to go.</OPTIONS>
<COMMENTS> </COMMENTS>
<ASKINGPRICE>6000</ASKINGPRICE>
<PROBLEMS> </PROBLEMS>
<KBBRetail>10020</KBBRetail>
<KBBPPE>8070</KBBPPE>
<KBBPPG>7495</KBBPPG>
<KBBPPF>6770</KBBPPF>
</XMLupdateQry>
<XMLupdateQry>
<STATUS>ACTIVE</STATUS>
<IDsuffix>464</IDsuffix>
<VIN>1MEFM50U03A603185</VIN>
<YEAR>2003</YEAR>
<MAKE>Mercury</MAKE>
<MODEL>Sable GS</MODEL>
<MILESBEGINNING>84166</MILESBEGINNING>
<COLOR>silver</COLOR>
<OPTIONS>For sale is this 2003 Mercury Sable. This vehicle is in overall condition with a 3.0L V6 and an automatic transmission. The car features AC, cruise control, remote entry, and all power options.</OPTIONS>
<COMMENTS> The car currently has a check engine light on which we hope to diagnose and repair.</COMMENTS>
<ASKINGPRICE>2800</ASKINGPRICE>
<PROBLEMS>The front bumper does have some damage. See picture.</PROBLEMS>
<KBBRetail>5575</KBBRetail>
<KBBPPE>3900</KBBPPE>
<KBBPPG>3475</KBBPPG>
<KBBPPF>3000</KBBPPF>
</XMLupdateQry>
</dataroot>
...and here's the error message I'm getting:
Fatal error: Call to a member function xpath() on a non-object in /print.php on line 6
Get car id from querystring
Open xml document using simplexml_load_file and assign it to variable "xml"
Get the <car> child node of the xml document where the <ID> tag's innertext is equal to the car id
Get the <YEAR> node of the child node and assign its innertext to variable "year"
Get the <MAKE> node of the child node and assign its innertext to variable "make"
Get the <MODEL> node of the child node and assign its innertext to variable "model"
I appreciate what's been posted so far, but it has not been very helpful. I can get the ID from the querystring. I can load the xml document just fine, but I can't seem to get the xpath correct. I always get an empty array.