Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Smarty for Beginners
MISCELLANEOUS

Smarty for Beginners
By: Hermawan Haryanto
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 156
    2003-12-28

    Table of Contents:
  • Smarty for Beginners
  • File Creation
  • The Basics
  • What about accessing a database?
  • Some fun stuff

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Smarty for Beginners - What about accessing a database?


    (Page 4 of 5 )

    Nice question. I'll try to explain it in here. First you need to know your database configuration such as username, password and your database name. If you have them already, then you can create your table using your Database Administration tools. I prefer using phpMyAdmin, you get the latest version of it at http://phpmyadmin.net.

    Create your table using this query:

    CREATE TABLE `friends` (
      `friends_id` int(10) unsigned NOT NULL auto_increment,
      `friends_name` varchar(255) default NULL,
      UNIQUE KEY `friends_id` (`friends_id`)
    ) TYPE=MyISAM;
    INSERT INTO `friends` VALUES (1, 'Paul');
    INSERT INTO `friends` VALUES (2, 'Matt');
    INSERT INTO `friends` VALUES (3, 'John');
    INSERT INTO `friends` VALUES (4, 'Mike');
    INSERT INTO `friends` VALUES (5, 'Rendi');
    INSERT INTO `friends` VALUES (6, 'Pailul');

    Open your index.php file and add these lines:

    $db_host = "localhost";
    $db_user = "database_user";
    $db_pass = "database_pass";
    $db_data = "database";
    $conn = mysql_connect ($db_host, $db_user, $db_pass) OR DIE ("Can't connect to database");
    @mysql_select_db ($db_data, $conn);
    $query = "SELECT * FROM friends";
    $result = mysql_query ($query, $conn);
    if (mysql_num_rows ($result) >= 1)
    {
      $friends = array();
      while ($rows = mysql_fetch_array ($result, MYSQL_ASSOC)) array_push ($friends, $rows);
      $smarty->assign ("friends", $friends);
    }

    Open your index.html page and add these lines:

    Friends List:
    {section name=i loop=$friends}
      {$friends[i].friends_name}<br>
    {/section}

    Now you'll see the list of friends on your page. You can do some other formatting such as nl2br, truncate, upper, or lower directly from html template page, not from your script. That is why we use Smarty, to separate between business logic and presentation logic. The script should only work to pull data from db but not to format the result. The formatting of the result is done in the html page.

    More Miscellaneous Articles
    More By Hermawan Haryanto

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap