PHP Programming/Smarty templating system/Simple tutorial
Appearance
(Redirected from PHP Programming/smarty/tutorials/simple)
- Create a directory called Website, in your webserver.
- Copy Smarty's libs directory into it installation.
- Create a directory called compile.
- Create a directory called templates.
- In the Website directory, create a file called index.php and Web.class.php. Make sure that they are blank.
- Web.class.php should look like this:
<?php class Web { function db_connect($db_host,$db_user,$db_pass,$db_db) { $this->link=@mysql_connect($db_host,$db_user,$db_pass) or die("Can't connect to database"); @mysql_select_db($this->link,$db_db) or die("Connected, but can't select the database"); } function db_query($sql) { return @mysql_query($this->link,$sql); } function db_close() { mysql_close($this->link); } } ?>
- index.php should be this:
<?php error_reporting(E_ALL); $db=array("host"=>"localhost","user"=>"root","pass"=>"","db"=>"database"); $tables['content']="test_content"; require_once("Web.class.php"); $web=new Web(); $web->db_connect($db['host'],$db['user'],$db['pass'],$db['db']); require_once("libs/Smarty.inc.php"); $smarty=new Smarty(); $smarty->template_dir="template"; $smarty->compile_dir="compile"; if ( isset($_GET['content_id']) && is_numeric($_GET['content_id']) ) { $sql="SELECT * FROM {$tables['content']} WHERE content_id = '{$_GET['content_id']}' LIMIT 1"; $result=$web->db_query($sql); $rows=array(); while ( $row=mysql_fetch_assoc($result) ) { $rows[]=$row; } if ( count($rows) == 1 ) { $smarty->assign("content_found",true); $smarty->assign("content_content",$rows['0']); } else { $smarty->assign("content_found",false); } $smarty->assign("section","content"); } else { $sql="SELECT content_title,content_date,content_position,content_id FROM {$tables['content']} ORDER by content_position asc"; $result=$web->db_query($sql); $rows=array(); while ( $row=mysql_fetch_assoc($result) ) { $rows[]=$row; } $smarty->assign("section","home"); $smarty->assign("content_content",$rows); } $smarty->display("index.tpl"); $web->db_close(); ?>
- Go to the templates directory
- Create a new file called index.tpl, make sure it's empty
- Create your own html design or anything and in the middle ( where you want the content to be ), write this:
{if $section == "home"} <ul> {foreach from="content_content" item="content_item"} <li><a href="./?content_id={$content_item.content_id}">{$content_item.content_title}</a></li> {/foreach} </ul> {elseif $section == "content"} <div><h1>{$content_content.content_title}</h1></div> <div>{$content_content.content_content}</div> {else} Sorry, there is no such page here! {/if}
- Create new MySQL table, with the following information:
TABLE NAME: test_content PRIMARY KEY: content_id content_id: INTEGER, EXTRA - AUTO_INCREASE content_title: VARCHAR(255) content_date: DATETIME content_content: TEXT content_position: INTEGER
- Modify your index.php and index.tpl as necessary ( notice the $db in index.php, change it to your settings!
- Now, using your MySQL Client (phpMyAdmin[1]/MySQL[2] or other tools), add new rows in your table, with content and its title. Try it with three at the start.
- Now, go to your directory Website through your Web Browser (you might need to upload it to your web server or set one up on your computer) ;)
If you have any problems, go to ask on IRC irc://irc.freenode.org/php or contact me. I haven't tested this script yet so you might find some small mistakes.
- For the later versions of MySQL use the following code:
CREATE TABLE `test_content` ( `content_id` INT(11) NOT NULL AUTO_INCREMENT, `content_title` VARCHAR(255) NOT NULL, `content_date` DATETIME NOT NULL, `content_content` TEXT NOT NULL, `content_position` INT(11) NOT NULL, PRIMARY KEY (`content_id`) ) TYPE = myisam;
References
[edit | edit source]