Stage ONE - Introduction to this Tutorial
Welcome to the Devscripts Dot Net Tutorial on Now Build A Dynamic Web Page Utility PHP, MySQL and Smarty.
This is a step by step guide demonstrating how one would go about building themselves a webpage, or indeed a website using some of the prewritten libraries that are available to the webmaster or web developer.
For this Tutorial, you will NEED Access To A Web Server Capable of Parsing PHP Files, with smarty installed and a mysql data running.
IF you do not have this set up you you shouth take a look at the links on
Stage two of the tutorial.
Stage Two - Tutorial Prerequisite Reading and Useful Links
Ok i know what php and mysql is, what the hell is smarty ??
Smarty Is A Template Engine for PHP. More Specify, IT Facilitates a Managable Way To Separate Application Logic and Content from Its Presentation.
This is best described in a situation where the application programmer and the template designer play different roles, or in most cases are not the same person. For example, let's say you are creating a web page that is displaying a newspaper article. The article headline , Tagline, Author and Body Are Content Elements, They Contain No Information About How They Will Be Presented.
They are passed into Smarty by the application, then the template designer edits the templates and uses a combination of HTML tags and template tags to format the presentation of these elements (HTML tables, background colors, font sizes, style sheets, etc.)
One day the programmer needs to change the way the article content is retrieved (a change in application logic.) This change does not affect the template designer, the content will still arrive in the template exactly the same. Likewise, if the template designer wants to completely redesign the templates, this requires no changes to the application logic. Therefore, the programmer can make changes to the application logic without the need to restructure templates, and the template designer can make changes to templates without breaking application logic.Useful links to Read Before Starting Coding Through this, or to just reason:
Smarty Home Page
Crash Course in Using Smarty
Popular Smarty Forum
PHP HOME PAGE
PHP MANUAL
MySQL Home Page
MySQL Manual
Linux, Apache, MySQL, PHP SETUP
Windows, Apache, MySQL, PHP SETUP
Stage Three - Creating a Data Source and Entering Data
In this Tutorial We Will Be Writing a news page. In the (mysql) Database Will Be News Articles Updated by Several Different Authors Using An Administration Panel (Not Covered Here).
What Weavers Five MOST Stories On Our Page With The Minimal Amount of Database Interaction As We Run A Very Busy Site.
We Will Be Making Use of Smarty's Caching Functionality To Achieve this.
To Start with We Will Need To Define The Database Structure.
We want to store a unique recordid ID, The Author WHO Submitted the article, The date and time itself.
Using SQL We Cream Create A Table Like this with the following code:
Create Table `News`
`id` int not null auto_increment,
`Title` VARCHAR (255) Not null,
`Author` Varchar (255) Not null,
`date` int not null,` article` longtext not null,
Primary key (`ID`),
FullText
`article`
)
) TYPE = Myisam Comment = 'this table stores our news articles';
You can use
PHPMYADMIN or Something Similar To Fill In a Few Bogus News Articles for Testing!
Ok So Now the data is set up, we need to actually get it back and displayed on the screen !!
Stage Four - Creating PHP Code To Retrieve The Data from MySQL
IF you read the crashcourse for smarty you will see a php page is Made Up of Two Distinct Parts:
I) The template - this is how the page shop DISPLAY THE DATA
II) The PHP - This Pulls Out The Data and Sends It to Template.
In the case of the spread.
The PHP Code Is Very Simple and Is Fully Comment, So Either Copy It, or Read THROUGH IT TO Understand What It Going ON
// Thase Are The Smarty Files
Require 'libarty.class.php';
// this is a file Which Abstracts the db connection functionality - check out pear
Require 'db.php';
$ smarty = new smarty;
$ smarty-> compile_check = true;
$ Smarty-> Debugging = false;
$ Smarty-> USE_SUB_DIRS = FALSE;
$ Smarty-> Caching = true;
// this SQL Statement Will Get The 5 MOST Recessly Added New Items from the Database
$ SQL = 'SELECT *';
$ SQL. = 'from `News`';
$ SQL. = 'Order By `ID` Desc Limit 0, 5';
$ results = mysql_query ($ SQL) OR Die ("Query Failed:". MySQL_ERROR ());
// for Each Result That We got from the database
While ($ line = mysql_fetch_assoc ($ result)) {
$ Value [] = $ LINE;
// Assign this array to smarty ...
$ Smarty-> Assign ('NEWS', $ VALUE);
}
// Display the news page through the news template? // DISPLAY
$ Smarty-> Display ('news.tpl');
?>
Notice That We set smarty to use caching
Smarty also has built-in caching capabilities to help speed up the page rendering. A copy of the template output is stored in a text file, then that is displayed upon subsequent calls to the request instead of dynamically rendering the page each time. This can SPEEDUP Page RENDERING SUBSTANTILY, ESPECIALLY The IS A LOT OF Processing Involved to Create The Page Such As Database Calls and Variable Assignments.
This Is All the PHP Code That We Will Need To Write for this Example, So Lets Move ONTO THE Good Stuff!
Stage FIVE - CREANG A Template To Repesent Our Data
This is Basically a html page with the addition of some {sections} Which is the smarty section.
hEAD>
Steve's News Page span>
div>
{section = news loop = $ news}
body>
html>
Smarty Facilitates a Convenient Way to loop over arrays of data with the section function.
Information about the {section} tag
Stage Six - Putting It All Together
Now That WE Have a Template, and some php to pop set, we shop becomle to load up the php file and get some news articles returned
I used the folly css stylesheet to get the results Below.
H1, h2, h3, #header, #footer {
Font-Family: Verdana, Tahoma, Helvetica, Arial, Sans-Serif;
}
H1 {
FONT-SIZE: 200%;
Font-weight: bold;
TEXT-ALIGN: CENTER;
}
H2 {
FONT-SIZE: 125%;
Font-Variant: Small-Caps;
Color: Black;
}
H3 {
FONT-SIZE: 85%;
Font-style: itract;
Color: # 9b9b96;
}
#header, #footer {text-align: center;
Display: block;
Border: 1px solution # 999;
Padding: 1mm;
Background: #ffc;
Color: # 000;
Color: gray;
Font-Variant: Small-Caps;
}
#header {
FONT-SIZE: 250%;
Font-weight: bold;
Margin-bottom: 5mm;
}
#footer {
FONT-SIZE: 85%;
Margin-top: 5mm;
}
Here is my smarty powed page in action:
Stage seven - contsions, Links and Comments
Now in this example it is quite simple to follow, and there is not really a clear reason behind why you would put in the extra effort to seperate the business and presentation logic, but on larger sites this is a very very useful thing to do .
Ok Thats the end of the tutorial, Any feedback Would Be AppReciated
Email Me As I Have Never Written A Tutorial Before!
Any Mistakes, Improvements or Just General Questions Also Feel Free To Get in Touch.
Download the Source Files and CSS
Here Remembering to Take Note of The Password 'Devscripts'