Building a Dynamic WebPage Using PHP, MySQL and Smarty [from devscripts.net]

xiaoxiao2021-03-06  62

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.

news page </ title></p> <p></ hEAD></p> <p><body ID = "top"></p> <p><! - ok display the page header to keep it nice -></p> <p><div id = "header"></p> <p><Span> Steve's News Page </ span></p> <p></ div></p> <p><! - this is where the news article will be going -></p> <p><div id = "bodytext"></p> <p><! - has a title -></p> <p><h1 id = "welcome"> Read all About it </ h1></p> <p><! - ok this is a section Which Will Loop Round for Every Item in $ News (Passed in Through From PHP) -></p> <p>{section = news loop = $ news}</p> <p><! - for every item, display the title -></p> <p><h2 id = "{$ news [news] .id}> {$ news [news] .title} </ h2></p> <p><! - WRITE OUT The Author Information and the date -></p> <p><h3> {$ news [news] .author}, {$ news [news] .date} </ h3></p> <p><! - now show the news articles -></p> <p>{$ news [news] .Aticle}</p> <p>{/ section}</p> <p></ div></p> <p><! - show copyright information etc -></p> <p><div id = "footer"> All Contents Copy Written:) </ div></p> <p><! - close the html tags -></p> <p></ body></p> <p></ html></p> <p>Smarty Facilitates a Convenient Way to loop over arrays of data with the section function.</p> <p>Information about the {section} tag</p> <p>Stage Six - Putting It All Together</p> <p>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</p> <p>I used the folly css stylesheet to get the results Below.</p> <p>H1, h2, h3, #header, #footer {</p> <p>Font-Family: Verdana, Tahoma, Helvetica, Arial, Sans-Serif;</p> <p>}</p> <p>H1 {</p> <p>FONT-SIZE: 200%;</p> <p>Font-weight: bold;</p> <p>TEXT-ALIGN: CENTER;</p> <p>}</p> <p>H2 {</p> <p>FONT-SIZE: 125%;</p> <p>Font-Variant: Small-Caps;</p> <p>Color: Black;</p> <p>}</p> <p>H3 {</p> <p>FONT-SIZE: 85%;</p> <p>Font-style: itract;</p> <p>Color: # 9b9b96;</p> <p>}</p> <p>#header, #footer {text-align: center;</p> <p>Display: block;</p> <p>Border: 1px solution # 999;</p> <p>Padding: 1mm;</p> <p>Background: #ffc;</p> <p>Color: # 000;</p> <p>Color: gray;</p> <p>Font-Variant: Small-Caps;</p> <p>}</p> <p>#header {</p> <p>FONT-SIZE: 250%;</p> <p>Font-weight: bold;</p> <p>Margin-bottom: 5mm;</p> <p>}</p> <p>#footer {</p> <p>FONT-SIZE: 85%;</p> <p>Margin-top: 5mm;</p> <p>}</p> <p>Here is my smarty powed page in action:</p> <p>Stage seven - contsions, Links and Comments</p> <p>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 .</p> <p>Ok Thats the end of the tutorial, Any feedback Would Be AppReciated</p> <p>Email Me As I Have Never Written A Tutorial Before!</p> <p>Any Mistakes, Improvements or Just General Questions Also Feel Free To Get in Touch.</p> <p>Download the Source Files and CSS</p> <p>Here Remembering to Take Note of The Password 'Devscripts'</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-88771.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="88771" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.038</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'Vl_2BQF5Ben4lsABhdK44hHuW1lGm_2Fxan_2BEKkjJujxi59B6Rweqagns4fhD4LlXLZ_2FFYzTWxamjNXtNl_2FR9z3jpA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>