Ad block is installed and active. Please support us by disabling it.
HomeSaturday, January 20, 2018, 16:43:46
1

RSS, MySQL & Smarty

Tuesday, February 10, 15, 00:32:33, 3 Yaers Ago Via wain, Hits: 3140
Smarty > Publishing MySQL Data in RSS 2.0
config.php

Code:
<?php
$dbhost = 'localhost';
$dbusername = 'root';
$dbpassword = 'mydbpass';
$dbname = 'test';
$conn=mysql_connect($dbhost, $dbusername, $dbpassword);
if(!$conn) :
die('Could not connect: ' . mysql_error());
endif;
$db=mysql_select_db($dbname, $conn);
if(!$db) :
die ('Can't connect to database : . mysql_error());
endif;
?>


rss.php

Code:
<?php
//database connection
include_once('config.php');
include('smarty/Smarty.class.php');
$smarty = new smarty();
$smarty->template_dir = 'templates';
$smarty->compile_dir = 'templates_c';
$smarty->config_dir = 'config';
$smarty->cache_dir = 'cache';
?>
<?php header('Content-type: application/rss+xml'); ?>
<?php echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"; ?>
<?php
$res = mysql_query("SELECT * FROM news ORDER by id DESC LIMIT 20")
or die(mysql_error());
while($row = mysql_fetch_array($res)){
$value[] = $row;
}
$smarty->assign('news', $value);
$smarty->caching = 1;
$smarty->display('rss.tpl');
?>


templates/rss.tpl

Code:
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Example.com</title>
<description>Example WebSite</description>
<link>http://www.example.com</link>
{section name=news loop=$news}
<item>
<title>{$news[news].title|stripslashes|truncate:50} :: Example.com</title>
<description>{$news[news].text|stripslashes|escape:'html'}</description>
<link>http://www.example.com/page.php?id={$news[news].id}</link>
<guid>www.example.com/page.php?id={$news[news].id}</guid>
<pubDate>{$news[news].date|date_format:"%a, %e %b %Y %H:%M:%S"} GMT</pubDate>
</item>
{/section}
</channel>
</rss>

Sign In

Sponsor

Related

PHP Redirect

Header (location) redirect must be processed before any output to the browser.

Upload File with PHP

Basic file uploading with PHP

MySQL to RSS

Publishing MySQL data in RSS

PHP Write to File

PHP Write to File :: PHP Tutorial, requirement(s): PHP server

Bookmark Script

Allow your users to easily bookmark your site, by clicking on a link on the page.

GuestBook PHP Script

Allow visitors to leave messages on your site. It features easy install, admin notification, and control panel to remove unwanted entries.

Syntax highlighting in Smarty

Syntax highlighting for Smarty - Google Code Prettify

Creating a Math CAPTCHA with PHP - PHP Tutorial

This tutorial will show you how to create a form with Math Captcha.

PHP explode

Explode — Split a string by string.

Upload Image with PHP [Example2]

[Example2] Tutorial - How to upload and resize image with PHP
About Author

wain

Date Registered: Sun, Feb 8, 15
Last Time Online: Thu, Jun 15, 17
So far has created 115 entries.
wain
This author has not yet filled in any details.