1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP include of script inside smarty template file

Discussion in 'PHP' started by Jim_Westergren, Dec 16, 2005.

  1. #1
    I need help!

    Been trying for hours and also by googling and asking other people but don't get this shit to work.

    I have a php script that displays a feed from my blog. An example can be seen on netmarketing.us

    I want to set this up on my directories. My directories use the phplinkdirectory script which is done in smarty.

    If I try this in the template file:

    {php}include('rss.php');{/php}

    Nothing gets output as HTML. There is nothing in the source code at all.

    If I add this to the index.php:

    include('rss.php');
    $tpl->assign('RSS', $RSS);


    And then use {$RSS} in the template file what happens is that it works but the feed gets displayed on the top of the file before all other HTML code and not where I want it.

    I really need some help on this and can pay if needed. The only solution I could think of is to use iframes that calls a php file and on that file I can run the script, but iframes are not good SEO wise ...

    The rss.php looks like this:

    
    
    <?php
    
    /*
    Created by Global Syndication's RSS Parser
    http://www.globalsyndication.com/rss-parser
    */
    
    set_time_limit(0);
    
    #### Here is the feed URL:
    
    $file = "http://www.jimwestergren.com/category/online-marketing/feed/";
    
    $rss_channel = array();
    $currently_writing = "";
    $main = "";
    $item_counter = 0;
    
    function startElement($parser, $name, $attrs) {
       	global $rss_channel, $currently_writing, $main;
       	switch($name) {
       		case "RSS":
       		case "RDF:RDF":
       		case "ITEMS":
       			$currently_writing = "";
       			break;
       		case "CHANNEL":
       			$main = "CHANNEL";
       			break;
       		case "IMAGE":
       			$main = "IMAGE";
       			$rss_channel["IMAGE"] = array();
       			break;
       		case "ITEM":
       			$main = "ITEMS";
       			break;
       		default:
       			$currently_writing = $name;
       			break;
       	}
    }
    
    function endElement($parser, $name) {
       	global $rss_channel, $currently_writing, $item_counter;
       	$currently_writing = "";
       	if ($name == "ITEM") {
       		$item_counter++;
       	}
    }
    
    function characterData($parser, $data) {
    	global $rss_channel, $currently_writing, $main, $item_counter;
    	if ($currently_writing != "") {
    		switch($main) {
    			case "CHANNEL":
    				if (isset($rss_channel[$currently_writing])) {
    					$rss_channel[$currently_writing] .= $data;
    				} else {
    					$rss_channel[$currently_writing] = $data;
    				}
    				break;
    			case "IMAGE":
    				if (isset($rss_channel[$main][$currently_writing])) {
    					$rss_channel[$main][$currently_writing] .= $data;
    				} else {
    					$rss_channel[$main][$currently_writing] = $data;
    				}
    				break;
    			case "ITEMS":
    				if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
    					$rss_channel[$main][$item_counter][$currently_writing] .= $data;
    				} else {
    					$rss_channel[$main][$item_counter][$currently_writing] = $data;
    				}
    				break;
    		}
    	}
    }
    
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    if (!($fp = fopen($file, "r"))) {
    	die("could not open XML input");
    }
    
    while ($data = fread($fp, 4096)) {
    	if (!xml_parse($xml_parser, $data, feof($fp))) {
    		die(sprintf("XML error: %s at line %d",
    					xml_error_string(xml_get_error_code($xml_parser)),
    					xml_get_current_line_number($xml_parser)));
    	}
    }
    xml_parser_free($xml_parser);
    
    // output HTML
    
    ### NOT NEEDED ####  print ("<div class=\"channelname\">" . $rss_channel["TITLE"] . "</div>"); 
    
    if (isset($rss_channel["ITEMS"])) {
    	if (count($rss_channel["ITEMS"]) > 0) {
    # SIFFRAN NUMMER 5 NEDAN ANGER HUR MÃ…NGA SKA VISAS
    		for($i = 0;$i < 5;$i++)  {
    			if (isset($rss_channel["ITEMS"][$i]["LINK"])) {
    			print ("\n<div class=\"itemtitle\"><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>");
    			} else {
    			print ("\n<div class=\"itemtitle\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</div>");
    			}
    			 print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div><br />"); 		}
    	} else {
    		print ("<b>There are no articles in this feed.</b>");
    	}
    }
    
    ?>
    
    Code (markup):
     
    Jim_Westergren, Dec 16, 2005 IP
  2. WhatiFind

    WhatiFind offline

    Messages:
    1,789
    Likes Received:
    257
    Best Answers:
    0
    Trophy Points:
    180
    #2
    WhatiFind, Dec 16, 2005 IP
  3. hans

    hans Well-Known Member

    Messages:
    2,923
    Likes Received:
    126
    Best Answers:
    1
    Trophy Points:
    173
    #3
    I am using Zfeeder from
    http://zvonnews.sourceforge.net/

    it's a php script, very easy to setup and allows SSI of own RSS feeds as well as SSI of remote RSS feeds and even to offer your own RSS feeds in WAP format. to the WAP world ... something quiet often used on my site ever since i have it a year ago
     
    hans, Dec 16, 2005 IP
  4. ScottBannon

    ScottBannon Well-Known Member

    Messages:
    408
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    168
    #4
    Smarty does seem to act up a bit with feeds when you try to display them with an include. I've found 3 ways around this.

    1) Check out the Smarty wiki pages, there are a couple of Smarty plugins available for RSS. I don't like these options simply because they place the work of parsing the feed on Smarty, and even though it's able to do it I prefer to utilize Smarty strictly for presentation/display and always use php for scripting functions.

    2) If your site is backed by a database, pull the feed into a db table and then extract and assign to Smarty variable for display. I prefer this option.

    3) Last resort if your site isn't backed by a db, create a normal php script that grabs and parses the RSS feed you want, have it 'write' the output to a second file called RSSdisplay.html (or whatever you want to call it). Set up a CRON job to run this php script every few hours, or however often you want the feed to update. In your Smarty template file(s) use a Smarty file include statement to include the RSSdisplay.html file.

    HTH,
    Scott
     
    ScottBannon, Dec 16, 2005 IP
    HN Will likes this.
  5. Jim_Westergren

    Jim_Westergren Notable Member

    Messages:
    1,882
    Likes Received:
    247
    Best Answers:
    0
    Trophy Points:
    235
    #5
    Thanks. The script I have is good enough but I will check out this other one.

    They were not able to help me. And I don't have Newstopia so I don't think that the scripts for that product will help me.

    Thanks, however it was not what I was looking for.

    Hi Scott,

    you seem to know this. My site is using a db yes. I would like help from you as I am a newbie in this. I can pay you a bit for help, like $15. I would be happy if you assisted me.
     
    Jim_Westergren, Dec 16, 2005 IP
  6. HN Will

    HN Will Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Scotts #3 above is money. Chron to write static is excellent advice.
     
    HN Will, Dec 16, 2005 IP
  7. Jim_Westergren

    Jim_Westergren Notable Member

    Messages:
    1,882
    Likes Received:
    247
    Best Answers:
    0
    Trophy Points:
    235
    #7
    Hi,

    Scott will help me tomorrow with number 2. I thank him in advance.
     
    Jim_Westergren, Dec 16, 2005 IP