PHP generates RSS files for improvement
Last Update:2016-06-13
Source: Internet
Author: User
PHP generates RSS files for improvement
I wrote a PHP code that uses the DOMDocument component to generate an RSS file, but it feels too bloated to encapsulate into a class but has never been successful.
Post-build RSS is basically like this
1 123 Wood
1
Http://www.xxx.com
Id:1,user_name:,pass:123,real_name: Wood
2 456 Small Wood
2
Http://www.xxx.com
Id:2,user_name:,pass:456,real_name: Small Wood
Of course the code is too redundant
$doc = new DOMDocument (' 1.0 ', ' utf-8 ');
$doc->formatoutput = true;
Create a label
Create RSS Labels
$rss = $doc->createelement (' RSS ');
Create a label below the channel
$channel = $doc->createelement (' channel ');
$ctitle = $doc->createelement (' title ');
$clink = $doc->createelement (' link ');
$cdescription = $doc->createelement (' description ');
foreach ($arr as $key = = $val) {
Create Item label
$item = $doc->createelement (' item ');
Create a child label label under item
$user = $doc->createelement (' user ');
$ititle = $doc->createelement (' title ');
$ilink = $doc->createelement (' link ');
$idescription = $doc->createelement (' description ');
Create a user tag
$user _id = $doc->createelement (' user_id ');
$user _name = $doc->createelement (' user_name ');
$user _pass = $doc->createelement (' User_pass ');
$real _name = $doc->createelement (' real_name ');
/* Here is where the database loop calls are needed */
Create content
Create the contents of the label below the item
$c _ititle = $doc->createtextnode ($val [' user_id ']);
$c _ilink = $doc->createtextnode (' http://www.xxx.com ');
$c _idescription = $doc->createtextnode (' ID: '. $val [' user_id ']. ', user_name: '. $val [' user_name ']. ', pass: '. $val [' Pass ']. ', Real_name: '. $val [' real_name ']);
Create the contents of the tag under user
$c _user_id = $doc->createtextnode ($val [' user_id ']);
$c _user_name = $doc->createtextnode ($val [' user_name ']);
$c _user_pass = $doc->createtextnode ($val [' Pass ']);
$c _real_name = $doc->createtextnode ($val [' real_name ']);
Create Item Property value
$a _id = $doc->createtextnode ($val [' user_id ']);
Creating properties
$attributes = $doc->createattribute (' id ');
/* Here is where the database loop calls are needed */
Item-level tag element content inheritance
$ititle->appendchild ($c _ititle);
$ilink->appendchild ($c _ilink);
$idescription->appendchild ($c _idescription);
element content inheritance for user-level tags
$user _id->appendchild ($c _user_id);
$user _name->appendchild ($c _user_name);
$user _pass->appendchild ($c _user_pass);
$real _name->appendchild ($c _real_name);
/* Here is where the database loop calls are needed */
Inherited
$channel->appendchild ($item);
Label inheritance for item-level labels
$item->appendchild ($user);
$item->appendchild ($ititle);
$item->appendchild ($ilink);
$item->appendchild ($idescription);
Id=1
$attributes->appendchild ($a _id);
//
$item->appendchild ($attributes);
Label inheritance for item-level labels
$user->appendchild ($user _id);
$user->appendchild ($user _name);
$user->appendchild ($user _pass);
$user->appendchild ($real _name);
}