HomeFriday, July 1, 2016, 15:17:13
4

Smarty - Create dynamic Web Pages with PHP & MySQL

Monday, February 9, 15, 03:04:15, 1 Year Ago Via wain, Hits: 1438
Creating Dynamic Web Pages Using Smarty and MySQL - Requirement(s): PHP Server, MySQL
config.php:
Code:
<?php
//MySQL Configuration
//DB Host (Normally 'localhost')
$dbhost = 'localhost';
//DB Database Username
$dbusername = 'root';
//DB Database User Password
$dbpassword = 'mypassword';
//DB Database Name
$dbname = 'mydbname';
//mysql_connect function
$conn=mysql_connect($dbhost, $dbusername, $dbpassword);
if(!$conn) :
die('Could not connect: ' . mysql_error());
endif;
$db=mysql_select_db($dbname, $conn);
if(!$db) :
die ('Cant connect to database : ' . mysql_error());
endif;
?>

install.php
Code:
<?php
//make a MySQL connection
include('config.php');
//create a MySQL table in the selected database
mysql_query("CREATE table `page` (
`linkid` int(11) NOT NULL auto_increment,
`text` text collate utf8_unicode_ci,
`linkurl` text collate utf8_unicode_ci,
PRIMARY KEY (`linkid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci AUTO_INCREMENT=1")
or die(mysql_error());
?>

index.php
Code:
<title>Index Page</title>
<center><h2>INDEX</h2></center><BR>
<?php
//make a MySQL connection
include('config.php');
echo "&nbsp;<a href=\"insert.php\">INSERT PHP</a><br>";
//get all the data from the page table
//*=select everything from the table page
$result = mysql_query("SELECT * FROM page")
or die(mysql_error());
while($link=mysql_fetch_array($result)){
//echo (display) a link using $link[linkid]
echo "&nbsp;<a href=\"webpage.php?id=$link[linkid]\">Web Page 1</a><br>";
}
?>

insert.php
Code:
<title>Insert Page</title>
<center><h2>Insert Page</h2></center><br>
<a href="index.php">Index Page</a>
<?php
//make a MySQL connection
include('config.php');
//to check if a submit button was clicked, use this...
if(isset($_POST['submit']))
{
$text = $_POST['text'];
$linkurl = $_POST['linkurl'];
//the INSERT INTO statement is used
//to add new records to a database table
mysql_query("INSERT INTO page (text,linkurl)
VALUES ('$text','$linkurl')")
or die(mysql_error());
}else{
?>
<form method="post" action="insert.php">
<table><tr><td>
Title:
</td><td>
<input name="text" size="60" maxlength="255">
</td></tr>
<tr><td>
URL:
</td><td>
<input name="linkurl" size="60" maxlength="255">
</td></tr>
<tr><td>
<input type="submit" name="submit" value="submit">
</td></tr></table>
</form>
<?php
}
?>

webpage.php
Code:
<?php
////make a MySQL connection
include('config.php');
// smarty configuration
include('Smarty.class.php');
$smarty = new smarty();
$smarty->template_dir = 'templates';
$smarty->compile_dir = 'templates_c';
$smarty->cache_dir = 'cache';
//get ID from URL
$id = $_GET['id'];
//get linkid, text, linkurl from the page table
//..or..*=select everything from the table page
$result = mysql_query("SELECT linkid, text, linkurl
FROM page WHERE linkid='$id' ")
or die(mysql_error());
while($myrow = mysql_fetch_assoc($result))
{
$values[] = $myrow;
}
$smarty->assign('page', $values);
$smarty->caching = 1;
//$REQUEST_URI will create a separate cache file
//for each unique URL when you call webpage.tpl.
$smarty->display('webpage.tpl',$REQUEST_URI);
?>

webpage.tpl
Code:
{section name=page loop=$page}
<head><title>{$page[page].text}</title></head>
<h1>{$page[page].text}</h1>
<a href="{$page[page].linkurl}">{$page[page].text}</a>
{/section}
Visit www.printearly.com/products/businesscards to find out more regarding print business cards online

William Monday, November 29, 2010, 01:48:09

Nice script :)

george Thursday, January 13, 2011, 08:57:58

Thanks for Tut.

Sign In

Web Hosting
First Month Free with Coupon Code phpmonth
Unlimited Domains - Unlimited Storage
Unlimited Transfer - Unlimited Email
Related

Smarty Paginate Example - Smarty Pagination with PHP

Smarty Paginate Example, Smarty Pagination with PHP. SmartyPaginate: a class/plugin for data set pagination within the Smarty template environment

Contact Us 2.0. :: PHP Form

Version 2.0. - A contact us form with email and form field validation. It allows your visitors to send you an email directly from your website.

How to set up password for phpMyAdmin in Wamp Server

Password for phpMyAdmin in Wamp

PHP $_POST Function

Collect values from a form sent with method post

How to check checkbox checked

Checking if checkbox is checked with jQuery

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.

E-Cards 1.1. PHP Script

This script will allow your visitors to send e-cards to their friends. It features admin notification and HTML output.To use the "e-cards" script completely free , the "powered by" link at the bottom of the pages must stay intact.

Creating a CAPTCHA with PHP :: PHP Tutorial

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

Filter Words in PHP - PHP Tutorial

Filter bad words in PHP, requirement(s): PHP server

Contact Us Form With Captcha

A contact us form with email and form field validation. It allows your visitors to send you an email directly from your website. Features: Captcha Image, HTML output.
About Author

wain

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