How to use xml data in php
March 15, 2010 at 10:09 am | Posted in PHP | Leave a commentTags: PHP, xml
www ေအာက္မွာ(in wamp server) folder တစ္ခုေဆာက္ၿပီး ဒီ xml code ေတြကို notepad မွာ pase လုပ္ၿပီး address.xml ဆိုတဲ့အမည္နဲ႔ save လိုက္ပါ။
<?xml version="1.0" encoding="utf-8"?>
<users>
<user>
<firstname>Sheila</firstname>
<surname>Green</surname>
<address>2 Good St</address>
<city>Campbelltown</city>
<country>Australia</country>
<contact>
<phone>1234 1234</phone>
<url>http://example.com</url>
<email>pamela@example.com</email>
</contact>
</user>
<user>
<firstname>Bruce</firstname>
<surname>Smith</surname>
<address>1 Yakka St</address>
<city>Meekatharra</city>
<country>Australia</country>
<contact>
<phone>4444 4444</phone>
<url>http://yakka.example.com</url>
<email>bruce@yakka.example.com</email>
</contact>
</user>
<user>
<firstname>Davo</firstname>
<surname>White</surname>
<address>The Only Way</address>
<city>Whitefall</city>
<country>Australia</country>
<contact>
<phone>8888 8888</phone>
<url>http://white.example.com</url>
<email>davo@white.example.com</email>
</contact>
</user>
<user>
<firstname>Shazza</firstname>
<surname>Green</surname>
<address>222 Great Western Hwy</address>
<city>Bathurst</city>
<country>Australia</country>
<contact>
<phone>6666 6666</phone>
<url>http://shazza.green.example.com</url>
<email>shazza@green.example.com</email>
</contact>
</user>
</users>
ၿပီးရင္ ဒီphp code ေတြကို notepad မွာ pase လုပ္ၿပီး index.php ဆိုၿပီး save လုပ္လိုက္ပါ။
<?php
if( ! $xml = simplexml_load_file('address.xml') )
{
echo 'unable to load XML file';
}
else
{
foreach( $xml as $user )
{
echo 'Firstname: '.$user->firstname.'<br />';
echo 'Surname: '.$user->surname.'<br />';
echo 'Address: '.$user->address.'<br />';
echo 'City: '.$user->city.'<br />';
echo 'Country: '.$user->country.'<br />';
echo 'Email: '.$user->contact->phone.'<br />';
echo 'Email: '.$user->contact->url.'<br />';
echo 'Email: '.$user->contact->email.'<br /><br />';
}
}
?>
ဒါကို browser မွာ run ရင္ေတာ႔ xml data ကို php မွာ ဘယ္လိုေခၚယူအသံုးခ်တယ္ဆိုတာသေဘာေပါက္သြားမွာပါ။ file ကမဟုတ္ဘဲ string ကေခၚရင္ေတာ႔ simplexml_load_string() ကိုသံုးပါတယ္။
like this
<?php
$xml_string = '<?xml version="1.0" encoding="iso-8859-1"?>
<users>
<user>
<firstname>Sheila</firstname>
<surname>Green</surname>
<address>2 Good St</address>
<city>Campbelltown</city>
<country>Australia</country>
<contact>
<phone>1234 1234</phone>
<url>http://example.com</url>
<email>pamela@example.com</email>
</contact>
</user>
</users>';
if( ! $xml = simplexml_load_string( $xml_string ) )
{
echo 'Unable to load XML string';
}
else
{
echo 'XML String loaded successfully';
}
?>
How to create RSS feeds
March 9, 2010 at 4:20 pm | Posted in PHP | Leave a commentTags: PHP, rss, xml
ကၽြန္ေတာ္တို႔ blog ေလးမွာ RSS feed ဘယ္လိုထည့္မလဲဆိုတာေဆြးေႏြးၾကတာေပါ႔။ RSS ဆိုတာ xml file တစ္ခုပါဘဲ။
ကၽြန္ေတာ္တို႔ရဲ႕ blog မွာ articles ေတြကို သိမ္းထားတဲ့ table တစ္ခုရွိတယ္ ဆိုပါေတာ႔။ ဒါကေတာ႔ ကၽြန္ေတာ္တို႔ table ရဲ႕ sql ပါ။
CREATE TABLE 'articles' ( 'article_id' INT NOT NULL AUTO_INCREMENT , 'article_title' VARCHAR( 200 ) NOT NULL , 'article_text' TEXT NOT NULL , 'article_url' VARCHAR( 300 ) NOT NULL , 'article_author' VARCHAR( 70 ) NOT NULL , 'article_category' VARCHAR( 40 ) NOT NULL , 'article_date' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , PRIMARY KEY ( 'article_id' ) )
ဒါကေတာ႔ rss.php ပါ။
<?xml version="1.0"?> <rss version="2.0"> <channel> <title>Your Site Name</title> <link>http://www.example.com</link> <description>A feed for articles on web development</description> <language>en-us</language>
<?php
//Connect to the database here
require_once('connect.php');
//Create the SELECT statement and execute it
$query = "SELECT *, DATE_FORMAT(article_date, '%a, %d %b %Y %T PST') AS article_pubdate FROM articles ORDER BY article_date LIMIT 5";
$result = mysql_query($query);
//Iterate over the rows to create each item
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) {
echo '<item>
<title>'.$row['article_title'].'</title>
<link>'.$row['article_url'].'</link>
<description>'.$row['article_text'].'</description>
<author>'.$row['article_author'].'</author>
<category>'.$row['article_category'].'</category>
<pubDate>'.$row['article_pubdate'].'</pubDate>
<source url="http://www.example.com/rss.php">Your Site Name</source>
</item>
}
?>
</channel> </rss>
php while loop ကေန ကၽြန္ေတာ္တို႔ရဲ႕ ေနာက္ဆံုး aritcle 5 ခုကိုေအာက္ကလို ထုတ္ေပးမွာျဖစ္ပါတယ္။
<item> <title>Tutorial: How to Create RSS Feeds</title> <link>http://www.example.com/article.php?id=60</link> <description>You've probably seen them on the web, even if you don't know what they're used for, those little orange buttons that look like they're broadcasting something. They're links to RSS feeds. In this tutorial, we'll explore some of the uses for RSS feeds, why your website should have them, and you'll also learn how to make them with and without PHP.</description> <author>email@example.com (generic author name)</author> <category>RSS</category> <pubDate>Tue, 08 Jul 2008 22:31:45 EDT</pubDate> <source url="http://www.example.com/rss.php">Your Site Name</source> </item>
သင့္ရဲ႕ blog က rss feed ရေၾကာင္းကို browser မွာေပၚေစဖို႔ သင့္ရဲ႕<head> section ထဲမွာဒီ link ေလးထည့္ေပးရပါမယ္။
<link rel="alternate" type="application/rss+xml" title="Your Site Article RSS Feed" href="http://www.example.com/rss.php" />
Ref:http://www.virtuosimedia.com/tutorials/tutorial-how-to-create-rss-feeds
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.