Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

PHP Programming/Smarty templating system/Simple tutorial

From Wikibooks, open books for an open world
<PHP Programming |Smarty templating system
Smarty templating system/FunctionsPHP Programming
Smarty templating system/Simple tutorial
Flat Frog
  1. Create a directory calledWebsite, in your webserver.
  2. Copy Smarty'slibs directory into it installation.
  3. Create a directory calledcompile.
  4. Create a directory calledtemplates.
  5. In theWebsite directory, create a file calledindex.php andWeb.class.php. Make sure that they are blank.
  6. Web.class.php should look like this:
    <?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);}}?>
  7. index.php should be this:
    <?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();?>
  8. Go to thetemplates directory
  9. Create a new file calledindex.tpl, make sure it's empty
  10. Create your own html design or anything and in the middle ( where you want the content to be ), write this:
    {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}
  11. Create new MySQL table, with the following information:
    TABLENAME:test_contentPRIMARYKEY:content_idcontent_id:INTEGER,EXTRA-AUTO_INCREASEcontent_title:VARCHAR(255)content_date:DATETIMEcontent_content:TEXTcontent_position:INTEGER
  12. Modify yourindex.php andindex.tpl as necessary ( notice the$db inindex.php, change it to your settings!
  13. 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.
  14. Now, go to your directoryWebsite 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 IRCirc://irc.freenode.org/php or contact me. I haven't tested this script yet so you might find some small mistakes.

  1. For the later versions of MySQL use the following code:
    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;

References

[edit |edit source]
  1. http://www.phpMyAdmin.net/
  2. http://dev.mysql.com


Smarty templating system/FunctionsPHP Programming
Smarty templating system/Simple tutorial
Flat Frog
Retrieved from "https://en.wikibooks.org/w/index.php?title=PHP_Programming/Smarty_templating_system/Simple_tutorial&oldid=4337472"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp