0

I cannot seem to figure this bit out. I'm trying to load data from an xml file depending on what the value of a url parameter is. I have an attribute set in my xml, but cannot seem to figure out how to go about this. I've looked at several examples of getting the attribute so I'm not sure where I am wrong since I haven't come across an example using url parameters to determine what data should be fetched. Here is what I have so far.

An example of my xml file. There will be more records of piercing in this file.

<piercings>
 <piercing id="antieyebrow">
  <title>Anti-Eyebrow</title>
  <names>Anti-Eyebrow, Teardrop</names>
  <gauge>16</gauge>
  <healing>6 - 8</healing>
  <risk>Rejection/Migration, Scarring</risk>
  <description>The anti-eyebrow piercing is located on the upper side of the cheek bone right below the orbital socket of the eye. This piercing is most commonly pierced using a 16 gauge curved barbell or custom bent jewelry. This piercing may also be referred to as a teardrop piercing.</description>
  <aftercare>It is recommended with this piercing to clean twice a day using saline solution or antibacterial soap. Do not overclean. Irritation from overcleaning can result in migration of the piercing.
</aftercare>
  <avoid>Using rubbing alchohol as a cleaner. Changing the jewelry for atleast 3 weeks although recommended to wait until the piercing is fully healed. Pools/hot tubs especially those with chemical cleaners in them. Swimming holes, creeks, rivers, etc. due to bacterial exposure risk.</avoid>
  <img>http://example.com/img/display/stock/antieyebrow_default.jpg</img>
  <additionalimgs>
   <additionalimg>http://example.com/img/thumb/stock/antieyebrow_1.jpg</additionalimg>
   <additionalimg>http://example.com/img/thumb/stock/antieyebrow_2.jpg</additionalimg>
   <additionalimg>http://example.com/img/thumb/stock/antieyebrow_3.jpg</additionalimg>
  </additionalimgs>
 </piercing>
</piercings>

So for this one I am trying to pull all of the data for the piercing with the attribute of id="antieyebrow". Here is what I tried to retrieve that.

 if (file_exists($_SERVER['DOCUMENT_ROOT'].'/info/piercing/piercings.xml')) {   
  $piercingxml = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/info/piercing/piercings.xml');
  $location = $_GET['location'];
  $piercingid = $piercingxml['id'];                  
 }
 else {
  $piercingxml = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/info/piercing/default.xml');
  $location = "default";
 }

And finally to display the data onto the page:

echo $piercingxml->$piercingid[$location]->title;

Any help as to where I went wrong would be greatly appreciated.

2 Answers 2

1

This will hopefully show how to do what your after. I'm using XPath to find the right element with the id that your after.

$location = "antieyebrow";  // $_GET['location'];
$details = $piercingxml->xpath("//piercing[@id='$location']")[0];
echo $details->title;

I've set $location rather than use a parameter - but this is just for demonstration purposes.

The next line which is the XPath says to look for any (the // bit) element called piercing. The bit in [] is the condition, this says I want to find the element which has the id attribute (using @ means attribute value) which is the one your after. As xpath returns a list of matching elements, I've just taken the first one (using [0] at the end of this line).

After this $details contains the element you want and so I can reference things like <title> using $details->title.

Sign up to request clarification or add additional context in comments.

1 Comment

This makes sense and I love how simple it is.Thanks.
1

What you could do is use the $location parameter in an xpath expression or get all the piercing items and use a loop to check for the id attribute. The $item variable is of type SimpleXMLElement and provides an xpath and an attributes method.

For example:

$piercingxml = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/info/piercing/piercings.xml');
$location = $_GET['location'];

// Use SimpleXMLElement xpath
$expression = '/piercings/piercing[@id="' . $location . '"]';
foreach ($piercingxml->xpath($expression) as $item) {
  echo $item->title;
}

// Use SimpleXMLElement and check the 'id' attribute
foreach ($piercingxml->piercing as $item) {
    if ((string)$item->attributes()->id === $location) {
        echo $item->title;
    }
}

Both will give you:

Anti-Eyebrow

1 Comment

I like your solution and it works, but with the structure of my page it was more practical to go with the other answer. Thank you anyways. It gives me another reference for later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.