Now continuing with PHP – today I will show you how to use a simple xml file and to get its data to a PHP file in the Internet. Not as difficult as it seems actually – XML files are quite OK, mainly due to the fact that their logic is really easy to understand. And PHP handles them pretty well. Thus, imagine that you want to build the following site:
This is what you would need as XML for this data:
Not bad. Anyone can understand the idea of XML just by reading the file. Now, let’s see how we build the site above, using the resources from the XML:
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <football_team> <name>CSKA Sofia</name> <city>Sofia</city> <nickname>The Reds</nickname> <history> <champion>31</champion> <cup>19</cup> </history> <founded>1948</founded> </football_team> |
Not bad! That is all! Really the “$team->olala->lalala” is working great!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<html> <body> <?php $football_team = simplexml_load_file("football_team.xml"); echo "<h2>Facts for $football_team->name</h2><hr>"; echo "Name: $football_team->name<br />"; echo "From: $football_team->city<br />"; echo "Nickname: $football_team->nickname<br />"; echo "<hr>"; echo "Titles and cups (2015) :<br />"; echo "The team is " . $football_team->history->champion. " times champion of Bulgaria.<br />"; echo "The team has won the cup " . $football_team->history->cup . " times.<br />"; echo "The team is founded in: $football_team->founded"; ?> </body> </html> |