Ad block is installed and active. Please support us by disabling it.
HomeSunday, November 19, 2017, 13:52:18
1

RSS, MySQL & Smarty

Tuesday, February 10, 15, 00:32:33, 3 Yaers Ago Via wain, Hits: 2800
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

Link Base 2.0.

Manage your links using a MySQL database. It features easy install, user submission, and an admin panel to add, edit and remove categories and links.

Password Protect With PHP

Password protect tith PHP - tutorial

Prevent the error loading XML – libxml_use_internal_errors

libxml_use_internal_errors() allows you to disable standard libxml errors and enable user error handling.

Images In Textarea - CSS

Tutorial Images in Textarea

MySQL – Select from two tables

Select data from two tables, PHP MySQL

Send This Link PHP Script

A popup form which allows your visitors to send a referral link from your site. It features admin notification and HTML output.

Captcha V2 - Free PHP Script

PHP Captcha protection image Version 2, - Requirement(s): PHP Server, GD Library

Syntax highlighting in Smarty

Syntax highlighting for Smarty - Google Code Prettify

PHP Cookies and Sessions Tutorial

PHP Cookies and Sessions Tutorials

PHP Mail

Sends an email.
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.