PHP Programming/smarty/tutorials/simple

From Wikibooks, open books for an open world
Jump to: navigation, search
  1. Create a directory called Website, in your webserver.
  2. Copy Smarty's libs directory into it installation.
  3. Create a directory called compile.
  4. Create a directory called templates.
  5. In the Website directory, create a file called index.php and Web.class.php. Make sure that they are blank.
  6. Web.class.php should look like this:
     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() {
  7. index.php should be this:
     $web=new Web();
     $smarty=new Smarty();
     if ( isset($_GET['content_id']) && is_numeric($_GET['content_id']) ) {
      $sql="SELECT * FROM {$tables['content']} WHERE content_id = '{$_GET['content_id']}' LIMIT 1";
      while ( $row=mysql_fetch_assoc($result) ) {
      if ( count($rows) == 1 ) {
      } else {
     else {
      $sql="SELECT content_title,content_date,content_position,content_id FROM {$tables['content']} ORDER by content_position asc";
      while ( $row=mysql_fetch_assoc($result) ) {
  8. Go to the templates directory
  9. Create a new file called index.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"}
     <nowiki> <ul></nowiki>
      {foreach from="content_content" item="content_item"}
     <nowiki><li><a href="./?content_id={$content_item.content_id}">{$content_item.content_title}</a></li></nowiki>
     <nowiki> </ul></nowiki>
     {elseif $section == "content"}
     <nowiki>  <div><h1>{$content_content.content_title}</h1></div></nowiki>
      Sorry, there is no such page here!
  11. Create new MySQL table, with the following information:
     TABLE NAME: test_content
     PRIMARY KEY: content_id
     content_title: VARCHAR(255)
     content_date: DATETIME
     content_content: TEXT
     content_position: INTEGER
  12. Modify your index.php and index.tpl as necessary ( notice the $db in index.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 it's title. Try it with three at the start.
  14. 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:// 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:
     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;