22, August 2008

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

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ 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

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


« on: Aug 27, 2007, 12:55:52 AM »

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: 8020
41077 credits
Members referred : 3



« Reply #1 on: Aug 27, 2007, 12:03:42 PM »

Just put the elements in reverse order Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Nikolas' Servant Child
*
Posts: 30
184 credits
Members referred : 0


« Reply #2 on: Aug 27, 2007, 12:22:30 PM »

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, 03: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: 6300
38626 credits
Members referred : 374


It's time to use PHP5!


« Reply #4 on: Aug 27, 2007, 04: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?


Last blog : Create custom backups from your website using cURL
Kill the googlebot
*
Gender: Male
Posts: 6
44 credits
Members referred : 0


« Reply #5 on: Aug 27, 2007, 04: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: 6300
38626 credits
Members referred : 374


It's time to use PHP5!


« Reply #6 on: Aug 27, 2007, 05: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


Last blog : Create custom backups from your website using cURL
Kill the googlebot
*
Gender: Male
Posts: 6
44 credits
Members referred : 0


« Reply #7 on: Aug 29, 2007, 12:03:11 AM »

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?
Nikolas' Servant Child
*
Posts: 30
184 credits
Members referred : 0


« Reply #8 on: Aug 29, 2007, 09: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, 08: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');
?>
Nikolas' Servant Child
*
Posts: 30
184 credits
Members referred : 0


« Reply #10 on: Aug 29, 2007, 11: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....

Topic sponsors:
Get a permanent link here for $1.99!


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?
Aug 22, 2008, 01:36:58 AM





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!


Forum Statistics
Total Posts: 36.084
Total Topics: 7.439
Total Members: 3.807
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: marthawelch

38 Guests, 2 Users online :

16 users online today:



Readers

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