Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - Smarty for Beginners
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

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

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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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


       · and comming from me, "the smary expert", that must be something...
       · Yes, It's very important tutorial added to so many important ones we always read on...
       · I've used Smarty for two years totally incorrectly and appreciate a beginner's...
       · how about a sequel?
       · I liked the article, It is very useful,Can u send further details of it.I have...
       · Call me dense........but I have tried to get Smarty installed on my Win2K box...
       · What was the error message or something for clue. We can't help you if there are no...
       · Sorry - my mistake. Here are the two messages.Parse error: parse error,...
       · I re-read the tutorial and I saw that on line 12 in libs.inc.php would be this...
       · That did the trick - thanks very much for your reply.I never tried switching the...
       · how to connect mysql tables through php codings?
       · Is there an extension to smarty that will create external stylesheets (*.css...
       · simple jadi cepet ngerti.trims..;)
       · Hi,Nice article like many others on codewalkers.com ! However I'm not really...
       · For those who are interested you can get the solution of the exercice at...
       · I have been asking this same question for some time.I have used PHPLib a pretty...
       · I do not have an html folder in my "smartyrules" folder. I don't see it in other...
       · HiProbems are1)lib.inc.php - define ("SMARTY_DIR",...
       · great tutorial dude....had 3 days to learn a new architecture and to develop...
       · Friend, Just give the relative path or full path to where the folder...
       · It is a simple and informative tutorial about smarty
       · Thank you Hermawan Haryanto for providing such an easy way to start smarty.A good...
       · Hi, I'm Karen from Venezuela, maybe you'll see that my english isn't so good, but I...
       · Im getting error when I try to display page. It doesn't tell us how to upload...
       · But what is smartyu?
       · Nice tutorial, keep it up. There is this other tutorial about smarty templating that...
     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT