PHP Programming/Smarty templating system/Simple tutorial
Tools
General
Sister projects
In other projects
| PHP Programming Smarty templating system/Simple tutorial | Flat Frog |
<?phpclassWeb{functiondb_connect($db_host,$db_user,$db_pass,$db_db){$this->link=@mysql_connect($db_host,$db_user,$db_pass)ordie("Can't connect to database");@mysql_select_db($this->link,$db_db)ordie("Connected, but can't select the database");}functiondb_query($sql){return@mysql_query($this->link,$sql);}functiondb_close(){mysql_close($this->link);}}?>
<?phperror_reporting(E_ALL);$db=array("host"=>"localhost","user"=>"root","pass"=>"","db"=>"database");$tables['content']="test_content";require_once("Web.class.php");$web=newWeb();$web->db_connect($db['host'],$db['user'],$db['pass'],$db['db']);require_once("libs/Smarty.inc.php");$smarty=newSmarty();$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();?>
{if$section=="home"}<ul>{foreachfrom="content_content"item="content_item"}<li><ahref="./?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}
TABLENAME:test_contentPRIMARYKEY:content_idcontent_id:INTEGER,EXTRA-AUTO_INCREASEcontent_title:VARCHAR(255)content_date:DATETIMEcontent_content:TEXTcontent_position:INTEGER
If you have any problems, go to ask on IRCirc://irc.freenode.org/php or contact me. I haven't tested this script yet so you might find some small mistakes.
CREATETABLE`test_content`(`content_id`INT(11)NOTNULLAUTO_INCREMENT,`content_title`VARCHAR(255)NOTNULL,`content_date`DATETIMENOTNULL,`content_content`TEXTNOTNULL,`content_position`INT(11)NOTNULL,PRIMARYKEY(`content_id`))TYPE=myisam;
| PHP Programming Smarty templating system/Simple tutorial | Flat Frog |