28, May 2012

Having Trouble Properly Adding Items to Newsfeed with PHP Script - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Having Trouble Properly Adding Items to Newsfeed with PHP Script
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: Having Trouble Properly Adding Items to Newsfeed with PHP Script  (Read 2220 times)
Kill the googlebot
*
Gender: Male
Posts: 6
44 credits
Members referred : 0


« on: Aug 26, 2007, 11:55:52 pm »

Hello, all.
I'm having trouble with a PHP script I'm writing to update an RSS newsfeed.  It adds a new <item> to the newsfeed but it adds it at the bottom instead of at the top of the list.  How can I have it add the new <item> as the first one on the list instead of the last?

Here is the script:
Code:
<?php
$xml 
simplexml_load_file('test.xml');
$cat1 'cat one';
$descrip 'The description.';
$perma time() . 'mysite-dot-net' rand(010000);
$TheLink 'http://www.example.com/';
$ThePubDate strftime('%a, %d %b %Y %H:%M:%S %z');
$TheTitle 'The New Title 3';
$NewOne $xml->channel->addChild('item');
$NewOne->addChild('category'$cat1);
$NewOne->addChild('description'$descrip);
$NewOne->addChild('guid'$perma);
$NewOne->addChild('link'$TheLink);
$NewOne->addChild('pubDate'$ThePubDate);
$NewOne->addChild('title'$TheTitle);
$xml->asXML('test.xml');
?>
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #1 on: Aug 27, 2007, 11:03:42 am »

Just put the elements in reverse order Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Cyberpunk Wannabe
*
Posts: 34
208 credits
Members referred : 0


« Reply #2 on: Aug 27, 2007, 11:22:30 am »

You could use the DOM XML functions instead of SimpleXML. Then you can use insert_before to insert the new node before the first current item.
Kill the googlebot
*
Gender: Male
Posts: 6
44 credits
Members referred : 0


« Reply #3 on: Aug 27, 2007, 02:31:24 pm »

Quote
You could use the DOM XML functions instead of SimpleXML. Then you can use insert_before to insert the new node before the first current item.

That sounds good.  Would that mean re-writing the code or just adding a line or two?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #4 on: Aug 27, 2007, 03:53:07 pm »

Quote
You could use the DOM XML functions instead of SimpleXML. Then you can use insert_before to insert the new node before the first current item.

That sounds good.  Would that mean re-writing the code or just adding a line or two?
using dom xml is a new code snippet,

anyway, is the feed wrong after you added the node?

Kill the googlebot
*
Gender: Male
Posts: 6
44 credits
Members referred : 0


« Reply #5 on: Aug 27, 2007, 03:56:39 pm »

The feed is still valid but I want to follow the example of many other sites that cooperate with mine and add new items to the top of the list.
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #6 on: Aug 27, 2007, 04:19:47 pm »

The feed is still valid but I want to follow the example of many other sites that cooperate with mine and add new items to the top of the list.
maybe you can do it different:

parse the existing feed and use the array + the new item(s) and create a new feed Wink

Kill the googlebot
*
Gender: Male
Posts: 6
44 credits
Members referred : 0


« Reply #7 on: Aug 28, 2007, 11:03:11 pm »

I can't make much progress with this.

Code:
$elements = $dom_xml->getElementsByTagName("item");
$element = $elements->item(0);
$NewNode = $element->insertBefore($element, $element);
$elements is a DOMNodeList with 10 items. I want to zero in on the first item (index "0") so I try to do that with the 2nd line of code. But the 3rd line of code keeps giving me errors. I want to insert a new <item></item> just before the first item.

I get this:
Fatal error: Uncaught exception 'DOMException' with message 'Hierarchy Request Error'

What does that even mean? Can anyone tell me what I'm doing wrong? I'm guessing I should pass different arguments to the insertBefore command but how do I know which arguments to pass?
Cyberpunk Wannabe
*
Posts: 34
208 credits
Members referred : 0


« Reply #8 on: Aug 29, 2007, 08:42:44 am »

You can't insert the same element again --- you have to clone it first, or create a new one. Also, you need to insert it into the parent node, otherwise your new node becomes a child of the reference node.

This works:

Code:
$dom_xml=DOMDocument::loadXML("<a><b>hello</b><b/></a>");
echo $dom_xml->saveXML();

$elements = $dom_xml->getElementsByTagName("b");
$parent = $dom_xml->getElementsByTagName("a")->item(0);
$element = $elements->item(0);
$NewNode = $parent->insertBefore($element->cloneNode(true), $element);
echo $dom_xml->saveXML();
Kill the googlebot
*
Gender: Male
Posts: 6
44 credits
Members referred : 0


« Reply #9 on: Aug 29, 2007, 07:25:09 pm »

anthonyw, I wanted to thank you for your help.  My script is running now and doing what I wanted.  One question, though: now that I know how to clone an existing node:
$element->cloneNode(true)

How do I create a new one?  For example, a new <item></item> (it doesn't have to have anything between the tags).

My working script:
Code:
<?php
$xml 
simplexml_load_file('test.xml');
$cat1 'cat one';
$cat2 'cat two';
$descrip 'The description.';
$perma time() . 'gearsonline-dot-net' rand(010000);
$TheLink 'http://www.example.com/';
putenv('TZ=PST8PDT');
$ThePubDate strftime('%a, %d %b %Y %H:%M:%S %z');
$TheTitle 'The New Title';

$dom_xml dom_import_simplexml($xml);
$elements $dom_xml->getElementsByTagName("item");
$element $elements->item(0);
$parent $dom_xml->getElementsByTagName("channel")->item(0);
$NewNode $parent->insertBefore($element->cloneNode(true), $element);
$xml simplexml_import_dom($dom_xml);

$xml->channel->item[0]->category[0] = $cat1;
$xml->channel->item[0]->category[1] = $cat2;
$xml->channel->item[0]->description $descrip;
$xml->channel->item[0]->guid $perma;
$xml->channel->item[0]->link $TheLink;
$xml->channel->item[0]->pubDate $ThePubDate;
$xml->channel->item[0]->title $TheTitle;

unset(
$xml->channel->item[10]);
$xml->asXML('test.xml');
?>
Cyberpunk Wannabe
*
Posts: 34
208 credits
Members referred : 0


« Reply #10 on: Aug 29, 2007, 10:33:51 pm »

anthonyw, I wanted to thank you for your help.  My script is running now and doing what I wanted.  One question, though: now that I know how to clone an existing node:
$element->cloneNode(true)

How do I create a new one?  For example, a new <item></item> (it doesn't have to have anything between the tags).

Code:
$element->cloneNode(false)
will create a new empty element of the same type as the original. Alternatively
Code:
$dom_xml->createElement("item")
  will create an entirely new node of a specified element type.
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=7044
Tags : rss xml php Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Having Trouble Properly Adding Items to Newsfeed with PHP Script
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 09:11:50 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.