Jump to content

Getting XML Data Based Off Of URL Parameter


BPJesseToxik

Recommended Posts

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="default">
  <title>Piercing Not Specified</title>
  <names>N/A</names>
  <gauge>0</gauge>
  <healing>0</healing>
  <risk>None</risk>
  <description>You must first choose a specific piercing from the menu to view information.</description>
  <aftercare>Aftercare tips will be available once you choose a specific piercing from the menu.</aftercare>
  <avoid>Things you should avoid will be listed once a specific piercing has been chosen.</avoid>
  <img>http://via.placeholder.com/350/000000/676767?text=Not%20Specified</img>
  <additionalimgs>
   <additionalimg>http://via.placeholder.com/350/000000/676767?text=Not%20Specified</additionalimg>
  </additionalimgs>
 </piercing>
 
 <piercing id="incomplete">
  <title>Listing Incomplete</title>
  <names>N/A</names>
  <gauge>0</gauge>
  <healing>0</healing>
  <risk>None</risk>
  <description>The listing for this piercing is incomplete. Once completed the data will appear on this page. Please check back later.</description>
  <aftercare>Aftercare tips will be available once you the listing is complete.</aftercare>
  <avoid>Things you should avoid will be listed once the listing is complete.</avoid>
  <img>http://via.placeholder.com/350/000000/676767?text=Incomplete</img>
  <additionalimgs>
   <additionalimg>http://via.placeholder.com/350/000000/676767?text=Incomplete</additionalimg>
  </additionalimgs>
 </piercing>
 
 <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://jessetoxik.com/img/display/stock/antieyebrow_default.jpg</img>
  <additionalimgs>
   <additionalimg>http://jessetoxik.com/img/thumb/stock/antieyebrow_1.jpg</additionalimg>
   <additionalimg>http://jessetoxik.com/img/thumb/stock/antieyebrow_2.jpg</additionalimg>
   <additionalimg>http://jessetoxik.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. URL: http://example.com/piercing.php?location=antieyebrow.

 

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;

So far nothing from the file is being displayed. How do I accomplish this?
Any help as to where I went wrong would be greatly appreciated.

 

Link to comment
Share on other sites

$location = $_GET['location'];
$piercingid = $piercingxml['id'];
$piercingxml->$piercingid[$location]->title
I don't know why you expect that to work.

 

1. Get the location from $_GET and validate it. Making sure it's just letters is probably good.

2. Search the XML for a with that id.

3. Get its title.

 

$location = $_GET["location"];
$piercing = null;
if (ctype_alpha($location)) {
	$piercing = current($piercingxml->xpath("/piercings/piercing[@id='{$location}']"));
	if (!$piercing) {
		// error - piercing not found
	}
} else {
	// error - invalid location
}
echo $piercing->title;
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.