chwebdesigns Posted February 25, 2008 Share Posted February 25, 2008 Hi, I have this code which inserts, when a form is filled in, into an rss feed. THe only problem is that it inserts it after the closing tags (</channel></rss>) Is there a way of making the php code insert it before these tags? Thanks From Cal Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/ Share on other sites More sharing options...
chwebdesigns Posted February 25, 2008 Author Share Posted February 25, 2008 I CANT SEEM TO PUT MY PASTE MY CODE IN (I forget the first time ) ANYBODY GOT ANY IDEAS ON HOW TO POST CODE? Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-475968 Share on other sites More sharing options...
revraz Posted February 25, 2008 Share Posted February 25, 2008 Copy, paste Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-475969 Share on other sites More sharing options...
chwebdesigns Posted February 25, 2008 Author Share Posted February 25, 2008 i have tried that! but it comes up with a meesage screen saying "Error 403 website declined to show you this page) ??? ??? Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-475971 Share on other sites More sharing options...
uniflare Posted February 25, 2008 Share Posted February 25, 2008 lol, try this thread on posting: http://www.phpfreaks.com/forums/index.php/topic,96050.0.html ----- without the code... i believe you could use: str_replace("</channel></rss>",$feed."</channel></rss>",$rssfeed); there may be other more efficient ways of doing this, it basically replaces the end tags with the content+end tags. hope this helps, Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-475973 Share on other sites More sharing options...
chwebdesigns Posted February 25, 2008 Author Share Posted February 25, 2008 $fe = fopen ('feedy.xml','a'); if(!$fe) { echo "<h1 align=\"center\">Error opening RSS Feed! Please try again. If the error continues please contact the webmaster</h1>"; exit; } $item = "<item><title>" . $HTTP_POST_VARS['title']; $item .= "</title><description>" . $HTTP_POST_VARS['news'] ; $item .= "</description><link>" . $HTTP_POST_VARS['anchor']; $item .= "</link><pubDate>" . date("D, d M Y H:i:s ") . "GMT</pubDate></item>" ; $item = str_replace("\r\n","<BR>",$item); $item .= "\r\n"; fwrite ($fe, $item); if(!fclose ($fe)) { echo "Error closing file! Please try again. If the error continues please contact the webmaster"; exit; lol thanks for the tip on inserting! Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-475983 Share on other sites More sharing options...
revraz Posted February 25, 2008 Share Posted February 25, 2008 What PHP version are you using? Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-475986 Share on other sites More sharing options...
chwebdesigns Posted February 25, 2008 Author Share Posted February 25, 2008 Erm.... i honestly don't have a clue, I don't have loads of experience in PHP, just dribs and drabs. Whether this helps, here is a snippet from some of the previous code, it may help you work out which version! I feel so much like an idiot!!! if(!$HTTP_POST_VARS['title']) { echo "<h1 align=\"center\">You must enter a title <a href=\"javascript:history.back(-1)\"> Go back</a></h1>" ; exit; } if(!$HTTP_POST_VARS['news']) { echo "<h1 align=\"center\">You must enter some news <a href=\"javascript:history.back(-1)\"> Go back</a></h1>"; exit; } Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-475991 Share on other sites More sharing options...
revraz Posted February 25, 2008 Share Posted February 25, 2008 I just noticed you're using HTTP_POST_VARS which is pretty old and you should redo your code and change them out. Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-476001 Share on other sites More sharing options...
chwebdesigns Posted February 25, 2008 Author Share Posted February 25, 2008 is this more up-to-date coding? <?php $name = $_POST['name']; $title = $_POST['title']; $anchor = $_POST['anchor']; $news = $_POST['news']; $password = md5($_POST['password']); if($password == '20d96dc5d58462f86a37ad43dc1a7bf9') { if($name == 'Select your name'){ echo "Please Select your Name"; } if($title == ''){ echo "Please type a title"; } if($anchor == ''){ echo "Please type an achor"; } if($news == ''){ echo "Please enter some news"; } else{ echo "Welcome $name ! You want to post some news with the title $title "; } }else {echo"Bad Password";} ?> Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-476195 Share on other sites More sharing options...
chwebdesigns Posted February 25, 2008 Author Share Posted February 25, 2008 <?php $file = fopen ( $feedfile, "a"); $feeddate = date("D, d M Y H:i:s"); $content = "<item><title>$title</title><description>$news</description><link>$anchor</link><pubDate>$feeddate GMT</pubDate></item>"; $closingtags = "</channel></rss>"; $writing = str_replace($closingtags,$content); fwrite ($file, $writing"\n"$closingtags); fclose ($file); echo "News Created"; ?> I have changed my code and used this, but it doesn't seem to work ??? Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-476223 Share on other sites More sharing options...
richardw Posted February 25, 2008 Share Posted February 25, 2008 This may be a little more than you want, but try substituting the MySQL call with your form data. Youu can always send your form data to the database and use php for the RSS feed. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>RSS Feed Code Display</title> <?php // Insert here the the title of your feed $rss_title= "PHP RSS feed"; // Insert your site, in the format site.com $rss_site= "www.YourSite.com" ; // Insert the description of your website $rss_description= "Helpful RSS Tips"; // Applicable language of the feed. For spanish, change to "es" $rss_language="en"; // Address of the logo file. It can be called whatever you want it to be! $rss_logo="rss.jpg"; // the feed's author email $emailadmin="YourName"; // set the file's content type and character set // this must be called before any output @header("Content-Type: text/xml;charset=iso-8859-1"); // Open the database mysql_connect("localhost","username","password"); @mysql_select_db("database_NameHere") or die("Unable to select DB"); // Select all the records from the Articles/Blog table - whatever you're using it for $query = "SELECT *,DATE_FORMAT(DATE_ADD(date, INTERVAL 5 HOUR), '%a, %d %b %Y %H:%i:%s GMT') AS pubDate FROM YOUR_DATABASE ORDER BY date DESC LIMIT 0,10"; // DATE_FORMAT(date, '%a, %d %b %Y %H:%i:%s') AS pubDate $result = @mysql_query($query) or die("Query failed") ; // dont forget the RSS specification when matching your form to the RSS feed echo '<?xml version="1.0" encoding="ISO-8859-1" ?> <rss version="2.0"> <channel> <title>'.$rss_title.'</title> <link>http://www.'.$rss_site.'</link> <description>'.$rss_description.'</description> <language>en-en</language> <image> <url>'.$rss_logo.'</url> <title>'.$rss_site.'</title> <link>http://www.'.$rss_site.'</link> </image>'; ?> <?php for($i=0;$i<10; $i++) //will loop through 10 times to generate 6 RSS items { // $photo_name = 'where-ever a photo for this article could be found - needs to be http://www.yoursite.com/images/whatever.gif'; $subject = @mysql_result($result,$i,'title'); //subject line for the RSS item $subject = str_replace ("’","'",htmlspecialchars(strip_tags($subject))); // Pass the record URL_product to the variable $url_product. It also // has to include the relative path, like in: "path/product1.htm" $url_product = 'php4me.com/press/article.php'; //define the URL of where people could read this blog/article entry // Define a description of the item $description = @mysql_result($result,$i,'description'); //easiest way is by grabbing the content // Clean the description $description = str_replace ("&","",htmlspecialchars(strip_tags($description))); $description2 = str_replace ("’","'",htmlspecialchars(strip_tags($description))); // ’ “” $description3 = str_replace ("“","\"",htmlspecialchars(strip_tags($description2))); $description4 = str_replace ("”","\"",htmlspecialchars(strip_tags($description3))); // Pass tags to describe the product - this has been left ouf of this example //This is a teaser of your article, basically what RSS readers will show the user in their inbox. //This is how you entice users to come over and read the full article //the easiest way is to just take the first few hundred characters of the content (description) $short_description = substr($description4,0,500) . " ..."; //so you can define when it was published // $timestamp = mysql_result($result,$i,'date'); //cleans the timestamp into an RSS friendly format $pubdate = @mysql_result($result,$i,'pubDate'); // date("r", strtotime($timestamp)); //outputs the RSS item $quest = "?id="; $id = @mysql_result($result,$i,'id'); echo ' <item> <title>'.$subject.'</title> <link>http://www.'.$url_product.$quest.$id.'</link> <guid isPermaLink="true">http://www.'.$url_product.$quest.$id.'</guid> <description>'.$short_description.'</description> <pubDate>'.$pubdate.'</pubDate> </item> '; } //end of the for-loop mysql_close(); //close the DB echo //close the XML file ' </channel> </rss>'; ?> <?php highlight_file($_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF']); ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-476242 Share on other sites More sharing options...
uniflare Posted February 25, 2008 Share Posted February 25, 2008 <?php $file = fopen ( $feedfile, "a"); $feeddate = date("D, d M Y H:i:s"); $content = "<item><title>$title</title><description>$news</description><link>$anchor</link><pubDate>$feeddate GMT</pubDate></item>"; $closingtags = "</channel></rss>"; $writing = str_replace($closingtags,$content."\n".$closingtags); fwrite ($file, $writing); fclose ($file); echo "News Created"; ?> try this Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-476283 Share on other sites More sharing options...
chwebdesigns Posted February 26, 2008 Author Share Posted February 26, 2008 nothing seems to work when that happens, ??? ??? thanks anyway Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-477240 Share on other sites More sharing options...
uniflare Posted February 26, 2008 Share Posted February 26, 2008 do you get any errors? Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-477278 Share on other sites More sharing options...
chwebdesigns Posted February 26, 2008 Author Share Posted February 26, 2008 all i get is a blank white page. ------ That's what happens if i have errors in my code, on my old server, it used to tell me the errors, but since ive moved it doesn't which is a pain. Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-477370 Share on other sites More sharing options...
uniflare Posted February 26, 2008 Share Posted February 26, 2008 try the following at the beginning of your scripts: <?php // Report all PHP errors (bitwise 63 may be used in PHP 3) error_reporting(E_ALL); // Same as error_reporting(E_ALL); ini_set('error_reporting', E_ALL); ?> see if it shows errors Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-477536 Share on other sites More sharing options...
chwebdesigns Posted March 2, 2008 Author Share Posted March 2, 2008 ok i have tried again, and inseted that code to look for errors, the doesn't seem to be any errors which come up (it says "News Created") but nothing is inserted into the rss feed Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-481795 Share on other sites More sharing options...
uniflare Posted March 5, 2008 Share Posted March 5, 2008 try this <?php $file = fopen ( $feedfile, "a"); $feeddate = date("D, d M Y H:i:s"); $content = "<item><title>$title</title><description>$news</description><link>$anchor</link><pubDate>$feeddate GMT</pubDate></item>"; $closingtags = "</channel></rss>"; $writing = str_replace($closingtags,$content."\n".$closingtags,$file); fwrite ($file, $writing); fclose ($file); echo "News Created"; ?> i forgot arg 3 on str_replace Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-483417 Share on other sites More sharing options...
chwebdesigns Posted March 7, 2008 Author Share Posted March 7, 2008 That inserts Resource id #2Resource id #2 after the closing brackets. Which has really left me stumped ??? Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-486420 Share on other sites More sharing options...
uniflare Posted March 7, 2008 Share Posted March 7, 2008 Sorry my bad again i havn't tested this code (obviously) but this should work now: (i forgot the fread function, look at www.php.net/fread for more info) <?php $file = fopen ( $feedfile, "a"); $contents = fread($file, filesize($feedfile)); $feeddate = date("D, d M Y H:i:s"); $content = "<item><title>$title</title><description>$news</description><link>$anchor</link><pubDate>$feeddate GMT</pubDate></item>"; $closingtags = "</channel></rss>"; $writing = str_replace($closingtags,$content."\n".$closingtags,$contents); fwrite ($file, $writing); fclose ($file); echo "News Created"; ?> hope this helps, Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-486499 Share on other sites More sharing options...
chwebdesigns Posted March 8, 2008 Author Share Posted March 8, 2008 This stops adding the Resource Id# at the end of the code, but nothing is inserted now. Arrggghh ??? Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-486937 Share on other sites More sharing options...
uniflare Posted March 8, 2008 Share Posted March 8, 2008 lol lets debug this: <?php $file = fopen ( $feedfile, "a+"); echo($feedfile."<br />"); $contents = fread($file, filesize($feedfile)); echo($contents."<br />"); $feeddate = date("D, d M Y H:i:s"); echo($feeddate."<hr />"); $closingtags = "</channel></rss>"; $content = "<item><title>$title</title><description>$news</description><link>$anchor</link><pubDate>$feeddate GMT</pubDate></item>".$closingtags; echo($content."<hr />"); $newfile = str_replace($closingtags,$content."\n".$closingtags,$contents); echo($newfile."<hr />"); fwrite ($file, $newfile); fclose ($file); echo "News Created"; ?> tell us what the page says (the html source of the page to be precise) Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-486964 Share on other sites More sharing options...
chwebdesigns Posted March 8, 2008 Author Share Posted March 8, 2008 hey, this is the HTML code i get <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> feed.xml<br /><?xml version="1.0" ?> <rss version="2.0"> <channel> <title>Test News Feed</title> <description>Test News feed from CH Webdesigns Testing Centre</description> <link>http://chwebdesigns.co.uk</link> </channel> </rss><br />Sat, 08 Mar 2008 20:31:55<hr /><item><title>jjjjjj</title><description>jjjjjjjjjj</description><link>jjjjj</link><pubDate>Sat, 08 Mar 2008 20:31:55 GMT</pubDate></item></channel></rss><hr /><?xml version="1.0" ?> <rss version="2.0"> <channel> <title>Test News Feed</title> <description>Test News feed from CH Webdesigns Testing Centre</description> <link>http://chwebdesigns.co.uk</link> </channel> </rss><hr />News Created </body> </html> Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-487175 Share on other sites More sharing options...
uniflare Posted March 9, 2008 Share Posted March 9, 2008 ok i get it, there is a line break between </channel> and </rss> so the str_replace wont work, try: <?php // Set current date for replacement/new content $feeddate = date("D, d M Y H:i:s"); // set file path/filename $feedfile = "test.rss"; $path = "./"; // set replacement/new content $replacement = "<item>\r\n<title>$title</title>\r\n<description>$news</description>\r\n<link>$anchor</link>\r\n<pubDate>$feeddate GMT</pubDate></item>\r\n</channel>\r\n</rss>"; // Get file contents $contents = file_get_contents($path.$feedfile); // set the REGEX Pattern used to find the closing tags, even if there are several line breaks and/or tabs between them. $preg_pattern = "/<\/channel>\r*\n*\t*<\/rss>/i"; // Replace $preg_pattern with $replacement in the $contents variable set eariler use file_get_contents() $writing = preg_replace($preg_pattern,$replacement,$contents); // Write the new contents of the file $bytes = file_put_contents($path.$feedfile,$writing); // Echo with number of Bytes written to file echo "News Created, $bytes bytes written"; ?> i scrapped fopen way and used file_get_contents/file_put_contents - seemed a little neater/easier to look at and understand, this way the file_put_contents with Overwrite any contents already in the file. Tested on Apache 2.2.8/PHP mod_ssl/2.2.8 OpenSSL/0.9.8e PHP/5.2.5 on a XP x64 system hope this helps, Link to comment https://forums.phpfreaks.com/topic/92913-inserting-php-code/#findComment-487384 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.