HomeSunday, June 24, 2018, 01:41:18
1

RSS, MySQL & Smarty

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

Advanced Contact Form - PHP Script

Advanced Contact Form with email and form field validation. Features: HTML Output(Response), Captcha Image.

Preventing multiple form submissions

Preventing multiple form submissions

PHP/MySQL Bookmarklet - PHP Tutorial

PHP/MySQL bookmarklet, bookmark current page, PHP/MySQL tutorial.

Smarty - Create dynamic Web Pages with PHP & MySQL

Creating Dynamic Web Pages Using Smarty and MySQL - Requirement(s): PHP Server, MySQL

Random MySQL Query

Random MySQL query, requirement(s): PHP Server, MySQL

MySQL Basic Functions

MySQL Basic Functions - select, delete, update..

View content in a zip file with PHP

List all files in a zip archive with PHP

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.

PHP Trim

trim() Strip whitespace (or other characters) from the beginning and end of a string.

Jquery - Formatting International Phone Numbers

How to format international telephone numbers with jquery
About Author

wain

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