Jump to content

Recommended Posts

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.

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
?>

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?

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?

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 by salathe

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''; ?

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?

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.

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>

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.

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
}

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.