Jump to content

inserting Php code


chwebdesigns

Recommended Posts

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
Share on other sites

 $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
Share on other sites

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
Share on other sites

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
Share on other sites

<?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
Share on other sites

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 ("&amp","",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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.