rumeye Posted January 19, 2013 Share Posted January 19, 2013 I am having issue on my PHP photo gallery. The album name is generate by the folder name as below: '. $_GET['album' So what I am trying to do is to show some external xml date in the gallery base on different albums name, I now tested with 3 different albums with the folder name Adriana, Alicia and Eva. Once again the album name is generate by the folder name. $folder_name = $_GET['album']; echo $folder_name; The above code is working to get the title on specific folder name of that album. Next I have a external xml file with different info for each album. Which I would like to show different info for each album. my xml file as below: <?xml version="1.0" encoding="UTF-8"?> <library> <models> <Adriana>This is the model info of Adriana</Adriana> <Alicia>This is the model info of Alicia</Alicia> <Eva>This is the model info of Eva</Eva> </models> </library> I first tested with this code to make sure it can read the "info": <?php $url = 'albums/Adriana/info.xml'; $xml = simplexml_load_file($url); $info=$xml->models->Adriana; echo $info; ?> Above code works fine, but what I really need to do is something like this, and this is the part that I got an error. $info=$xml->models->$folder_name; Please let me know what am i missing on the above line. Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/ Share on other sites More sharing options...
Barand Posted January 19, 2013 Share Posted January 19, 2013 What error did you get? It works for me <?php $str = <<<XML <?xml version="1.0" encoding="UTF-8"?> <library> <models> <Adriana>This is the model info of Adriana</Adriana> <Alicia>This is the model info of Alicia</Alicia> <Eva>This is the model info of Eva</Eva> </models> </library> XML; $xml = simplexml_load_string($str); $folder_name = $_GET['album']; $info = $xml->models->$folder_name; echo $info; //--> This is the model info of Alicia ?> Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1406963 Share on other sites More sharing options...
rumeye Posted January 19, 2013 Author Share Posted January 19, 2013 (edited) its not readying the xml. and there are no error message but it just didn't show anything from my xml Edited January 19, 2013 by rumeye Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1406968 Share on other sites More sharing options...
rumeye Posted January 19, 2013 Author Share Posted January 19, 2013 I see the issue now, because some of my folder name is as follow: <John Doh>This is the model info of Alicia</John Doh> With a space after the first name: and i got an error say : parser error : Specification mandate value for attribute Doh are there anyway to make that work if there will be space in my folder name? Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1406976 Share on other sites More sharing options...
Barand Posted January 19, 2013 Share Posted January 19, 2013 Do you have any error reporting turned on? I get half a dozen warning messages if I attempt to read a tag name with a space in it. Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1406988 Share on other sites More sharing options...
rumeye Posted January 20, 2013 Author Share Posted January 20, 2013 Yes, just when I attempt to add a space in a tag as bellow: <?xml version="1.0" encoding="UTF-8"?> <library> <women_fashion> <Adriana>height 5'10, bust 34, waist 24, hips 35, dress 10, hair brown, eyes green.</Adriana> <Eva>This is the model info of Eva</Eva> <John Doh>This is the model info of John Doh</John Doh> </women_fashion> </library> and I got this error log: Warning: simplexml_load_file() [function.simplexml-load-file]: info.xml:6: parser error : Specification mandate value for attribute Doh in C:\xampp\htdocs\Iconmodels.ca\women_fashion.php on line18 Warning: simplexml_load_file() [function.simplexml-load-file]: <John Doh>This is the model info of John Doh</John Doh> in C:\xampp\htdocs\Iconmodels.ca\women_fashion.php on line 18 Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in C:\xampp\htdocs\Iconmodels.ca\women_fashion.php on line 18 Warning: simplexml_load_file() [function.simplexml-load-file]: info.xml:6: parser error : expected '>' in C:\xampp\htdocs\Iconmodels.ca\women_fashion.php on line 18 Warning: simplexml_load_file() [function.simplexml-load-file]: <John Doh>This is the model info of John Doh</John Doh> in C:\xampp\htdocs\Iconmodels.ca\women_fashion.php on line 18 Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in C:\xampp\htdocs\Iconmodels.ca\women_fashion.php on line 18 And so far I will need a space in between the tag, because I am using the folder name as the album person name. Are there anyway to do a tag with a space? Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1407028 Share on other sites More sharing options...
Barand Posted January 20, 2013 Share Posted January 20, 2013 You are trying to use field values as field names. Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1407070 Share on other sites More sharing options...
salathe Posted January 20, 2013 Share Posted January 20, 2013 (edited) XML element names don't have spaces. When you use something like <John Doh> it is the start tag of a John element with an empty Doh attribute. The errors that you are seeing are because </John Doe> is not valid XML (it expects only the element name there). It would probably make more sense to have a structure that does not rely on the model name being a valid XML element name, perhaps <model name="John Doh">…</model> then you can search the <model> elements by their name attribute. XPath makes this easy, and we can help you with that if you need it. Edited January 20, 2013 by salathe Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1407078 Share on other sites More sharing options...
rumeye Posted January 20, 2013 Author Share Posted January 20, 2013 I guess all I can do at this moment is to add a "-" in between the first and last name. (for now). this is a link in my internal server, might be a bit slow. http://64.56.236.148/iconmodels.ca/women_fashion.php Please find top roll, 3rd image from the left "Alicia-A" is there any way to replace the "-" with a space in the echo''; ? Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1407117 Share on other sites More sharing options...
salathe Posted January 20, 2013 Share Posted January 20, 2013 str_replace Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1407134 Share on other sites More sharing options...
Barand Posted January 21, 2013 Share Posted January 21, 2013 If you were creating a database table you would not name the columns Eva or Alicia. You would have a column "Name" and Eva etc would be values in that column. So why do you insist on giving your XML elements names like "Eva" when they should be values of the element? Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1407174 Share on other sites More sharing options...
rumeye Posted January 21, 2013 Author Share Posted January 21, 2013 I am not very experience with PHP MYsql database, my website is not run on a database. but I try to make the updating part as simple as I can, by ftp a folder with images and the album name will be the folder name. and you are right this is why things getting so messed up. Maybe I should learn how to create a data base gallery. Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1407178 Share on other sites More sharing options...
Barand Posted January 21, 2013 Share Posted January 21, 2013 Better ways to construct your XML would be <?xml version="1.0" encoding="utf-8"?> <models> <model name="Alicia"> <info>This is info about Alicia</info> </model> <model name="John Doe"> <info>This is info about John Doe</info> </model> </models> or <?xml version="1.0" encoding="utf-8"?> <models> <model> <name>Alicia</name> <info>This is info about Alicia</info> </model> <model> <name>John Doe</name> <info>This is info about John Doe</info> </model> </models> Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1407181 Share on other sites More sharing options...
rumeye Posted January 21, 2013 Author Share Posted January 21, 2013 Thanks Barand, that's a better solution for me =) Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1407190 Share on other sites More sharing options...
rumeye Posted January 21, 2013 Author Share Posted January 21, 2013 Better ways to construct your XML would be <?xml version="1.0" encoding="utf-8"?> <models> <model name="Alicia"> <info>This is info about Alicia</info> </model> <model name="John Doe"> <info>This is info about John Doe</info> </model> </models> or <?xml version="1.0" encoding="utf-8"?> <models> <model> <name>Alicia</name> <info>This is info about Alicia</info> </model> <model> <name>John Doe</name> <info>This is info about John Doe</info> </model> </models> One last question, in order to print the info of John Doh, I get nothing with the code below: echo $xml->model->name("John Doh")->info; I think i did somthing wrong there. Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1407203 Share on other sites More sharing options...
Barand Posted January 21, 2013 Share Posted January 21, 2013 Depends on whether name attribute or name element is used foreach ($xml->xpath('//model') as $model) { if ($model['name'] == $folder_name) echo $model->info; // if <name='xyz'> used # if ($model->name == $folder_name) echo $model->info; // if <name>..</name> used } Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1407215 Share on other sites More sharing options...
rumeye Posted January 21, 2013 Author Share Posted January 21, 2013 Thank you it work Quote Link to comment https://forums.phpfreaks.com/topic/273362-problem-with-php-with-external-xml-content/#findComment-1407295 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.