Cause:
Today, the project encountered a problem: need to dynamically update the picture on the home page, to show that the site is not finished even if, is someone has been maintaining. Well, the demand has, how to achieve?!
My idea is as follows:
Picture storage location: put in a folder; the type of the picture: can not be fixed, as long as the picture will be able to upload the display; Picture name: According to the original name a bit irregular, should be renamed.
Display location: This requires the image to specify the appropriate ID, the ID is fixed, to the image one by one corresponding. So there is a record one by one corresponding to the relationship file, you can choose the CSV file, select the database records, finally decided to choose XML, this thing in school has not learned, I have been avoiding the use of this thing, afraid of trouble. Today is a challenge, spent an afternoon, finally some harvest.
Learning steps:
Clear goal: 1, understand the structure of XML, 2, how to build XML file dynamically, 3, how to read and modify the XML file
first, the structure of XML is a tree-shaped structure:
That's a good understanding. Simply write one:
Copy CodeThe code is as follows:
1
Pic 1
2
Pic 2
3
Pic 3
second, I use PHP to create:
1. Define a DOM object: $dom = new DomDocument (' 1.0 ');
2. Add child elements: $dom->appendchild ($dom->createelement ("Pictures"))
The in-memory prototypes are:
Continue to add child elements: *->appendchild ($dom->createelement ("picture"));
Continue add: **->appendchild ($dom->createelement ("id"));
No child elements, plus node: ***->appendchild ($dom->createnode ("1"))
The above * represents the code on the previous line, so you can write a line:
$dom->appendchild ($dom->createelement ("Pictures"))->appendchild ($dom->createelement ("Picture"))
->appendchild ($dom->createelement ("id"))->appendchild ($dom->createnode ("1"));
This should be the case in memory now: 1
Clearly the demand is far away, it is easy to see the ignorant.
So generally write: $pictures = $dom->appendchild ($dom->createelement ("Pictures"));
$picture = $pictures->appendchild ($dom->createelement ("picture"));
$id = $picture->appendchild ($dom->createelement ("id"));
$id->appendchild ($dom->createnode ("1"));
You can also create the name node below:
$name = $picture->appendchild ($dom->createelement ("name"));
$name->appendchild ($dom->createnode ("Pic 1");
The next step is to create the picture node:
$picture = $pictures->appendchild ($dom->createelement ("picture"));
In fact, these troublesome things can be written for a loop to achieve.
To generate an XML file:
$dom->formatoutput = true;//To set the formatted output
$dom->save ("Erhsh.xml");//Save XML file
third, read the XML file.
1, or define a DOM object; $dom->new DomDocument ();
2. Load XML file: $dom->load ("Erhsh.xml");
3, according to the node's name to obtain the node collection: $dom->getelementbytagname ("pictures");
This method is a bit cumbersome, refer to the file:
Http://www.jb51.net/article/25853.htm
But there is a way I like it: simplexml_load_file ("Erhsh.xml");
This method can convert the contents of the XML file into the form of an object, using the "--" and "[]" to easily go to the content of XML;
But there was a problem in development:
When executed: Print_r ($xml->pictures), when the output is a SimpleXMLElement object, ([picture] = = Array ([0]=>array (...) [1]=>array (...)]);
Re-execute: Print_r ($xml->pictures->picture), the output is n separate objects.
Execution: Print_r ($xml->pictures->picture[0]->id), or an object that is output. This is not understood, it should be a string. Finally, the internet says "iterative object",
The echo output should be used, Print_r (), and the Var_dump () output is inaccurate. Reference Address: http://www.jb51.net/article/25852.htm
Of course, you can also modify the value of XML through this method.
The writing is very bad, only for my memo.
http://www.bkjia.com/PHPjc/322735.html www.bkjia.com true http://www.bkjia.com/PHPjc/322735.html techarticle Cause: Today the project encountered a problem: need to dynamically update the picture on the home page, to show that the site is not finished even if, is someone has been maintaining. Well, the demand has, how ...