Hi,
I want to be able to use a variable in simplexml statement like this:
$thename = $_GET['NAME'];
$xml = simplexml_load_file("countries.xml");
$thename = $xml->$country->name;
however, it does not allow me to use the variable ($country) this way. Is there a way to do this?
I need to be able to select which country I am working with based on the user's selection.
Thanks,
Mike
Using Variables in Simplexml
Moderator: General Moderators
Re: Using Variables in Simplexml
i'm not 100% sure as have never used xml with php and am learning myself but....would you not concatenate the $country variable?
THIS...
INSTEAD OF...
???
It is a wild guess so apologies if incorrect.
THIS...
Code: Select all
$thename = $xml.$country->name;
Code: Select all
$thename = $xml->$country->name;
???
It is a wild guess so apologies if incorrect.
Re: Using Variables in Simplexml
No that did not work.
If I hard code it, it looks like this
And this works.....
What I need to do is use a variable in place of bolivia.....like this:
Where $country is variable containing the name of a country in string form.
Mike
If I hard code it, it looks like this
Code: Select all
<img src="<?php echo $xml->bolivia[0]->flag->path; ?> />What I need to do is use a variable in place of bolivia.....like this:
Code: Select all
<img src="<?php echo $xml->$country[0]->flag->path; ?> />Mike
Re: Using Variables in Simplexml
You're venturing into the territory of variable variables.
Code: Select all
$xml->{$country}->name