Jump to content

Recommended Posts

Hey everybody!

 

Having tons of fun with PHP, but I'm trying to figure something out with XML.

 

I've decided to drive my online portfolio with XML instead of typing all the info into a super long switch statement. I have found plenty of documentation on how to iterate through a whole slew of elements using their index id, but very little on how to access a specific element via name.

 

I have a beautiful XML document that has the basic structure of media->category->slide  where slide contains a bunch of child nodes with information about that particular image/movie/website or whatever, along with an image url and such.

 

The category and slide ID are coming from _GET.

 

Basically I want to access a particular slide

 

slide[$mediaID]

 

which is under a particular category

 

category[$categoryID]

 

and display that information on the page.

 

Unfortunately I can't figure out a way to access that particular category without using a control structure and conditional checking... example below:

 

$xml=simplexml_load_file("media.xml");

while(!$catfound)
	{
		$i=0;
		if ((string)$xml->category[$i]->name==$category)
		{
			$catfound=$category;
		}
		$i++;
	}

 

I did find a way to access it via the name using XPath, but it appears the object or array or whatever xpath returns doesn't maintain any of the attributes belonging to XML elements, just the elements themselves.

 

I know there is a much easier way because this is something people must do every day.

 

Second question:

 

Once I do access the correct category and slide

 

$xml->category[$catfound]->slide[$mediaID];

 

is there a way I can copy that particular slide to another array or reference it without having to provide that long path everytime? Basically I want to access elements in it by typing

 

$slide->title;

 

for the rest of my script.

 

 

Sorry if these are slightly obvious questions. I'm trying to do things correctly instead of just doing them.

 

Appreciate any help!

 

Best,

Robert

Problem solved.

 

I was wrong when I said Xpath doesn't maintain attributes... they just don't show up in var_dump.

 

Code below works and shows accessing an element and an attribute.

		$slide=$xml->xpath('/media/category[@name="'.$category.'"]/slide["'.$mediaID.'"]');


	echo "<br/>".$slide[0]->title;
	echo "<br/>".$slide[0]->bigimage['file'];

 

 

Hey how do I mark this thread solved? I don't see any link for that.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.