Jump to content

Recommended Posts

$xmlFileData = file_get_contents("xml/myxml.xml");
$xmlData = new SimpleXMLElement($xmlFileData);
$xmlName = $xmlData->color[0]->name;

 

--------------------------------------------------

Hi all, using the method above i manage to call text from a specific node.

 

But.. what if i want to have the nodes to be called dynamically like this:

$xmlName = $xmlData->color[$dynamic number]->name;

So i can use only 1 template page.

 

I tried having the number called from the URL:

$xmlName = $xmlData->property[print $_GET[myID]]->name;

 

I also tried echoing etc. however it seems like it just can't work.

 

Here is the output:

0red

 

If i choose "1" like myID=1, the output is:

1red

 

The array number seem to stay as default...

 

Is there a way to have the array'[]' get the number and use it as [1] or [2] based on the URL? basically having the number inside the [] change..

 

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/135651-solved-xml-parsing-problem/
Share on other sites

$xmlFileData = file_get_contents("xml/myxml.xml");
$xmlData = new SimpleXMLElement($xmlFileData);
$xmlName = $xmlData->color[$_GET['myid']]->name;

 

I think you were just using the $_GET call wrong, you do not have to print it if you are using it internally to PHP.

I did try that already and it does nothing, its almost as if the data cannot be called into the []

 

If i use a regular variable such $myVar=0 it works i think that i need to turn the URL value into a numeric value rather than a string...

 

[$myVar] does work.

Are you sure you are passing in a get variable?

 

$myid = isset($_GET['myid'])?$_GET['myid']:0; // if $_GET is not present assign the value default of 0

$xmlFileData = file_get_contents("xml/myxml.xml");
$xmlData = new SimpleXMLElement($xmlFileData);
$xmlName = $xmlData->color[$myid]->name;

 

Just an fyi, it is possible to access elements of the array using an array identifier, so the previous should work as long as "myid" was set using the get such as:

http://www.mysite.com/xml.php?myid=1

 

Chances are the above will not work if you have not been calling the page like 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.