PHP Smarty Template if function foreach function

Source: Internet
Author: User
Tags dsn stmt smarty template

Querying data from a database, the browser is displayed in a tabular format

Template page

<table border= "1"  width= " align=" "center" ><caption> User Information Sheet </caption>             <{foreach  $tdname  as  $val} >                             <th><{$val}></th>             <{/foreach}>             <{foreach  $users  as  $user}>                              <{if [email protected]}>                              <tr bgcolor= "red" >                     <{elseif [email protected]}>                              <tr bgcolor= "yellow" >                     <{elseif [ email protected] is even}>                             <tr  bgcolor= "pink" >                     <{else}>              &nbsP;              <tr bgcolor= " Gray ">                     <{/if}>                                                   <{foreach      $user  as  $val}>                                      <td align= "center" >                                      <{$val}>                                      </td>                              <{/foreach}>                     </tr>                     <{ foreachelse}>                      no user queries come out!             <{/foreach}></table> 

PHP page

<?php //create Smarty Object require_once  './libs/smarty.class.php ';//define root directory define (' root ',  str_replace (" \ \ ", "/", dirname (__file__))." /");//instantiate Smarty class $smarty=new smarty ();//set delimiter $smarty->left_delimiter=" <{"; $smarty->right_ Delimiter= "}>";//set to false  the bounding symbol can have spaces $smarty->auto_literal = false;//add a plugin directory//$smarty- >setpluginsdir (ROOT. " /libs/myplugins/");//note Add a plug-in, to the system default path to add   otherwise cannot use the default system plug-in $smarty->setpluginsdir (array (     root. " /libs/plugins/",//system default set Path     root." /libs/myplugins/",//custom));//connection database const  dsn =  ' mysql:host=localhost;dbname=test '; const    DBUSER =  ' Root ';const   dbpwd     =  ' Root ';try{     $pdo  = new pdo (dsn, dbuser,dbpwd);} Catch (pdoexception  $e) {    echo  "database connection failed:". $e->getmessage ();     exit;} $query  =  "select id, username, password,email from users"; $stmt  =  $pdo Prepare ($query); $stmt  ->execute (); $users  =  $stmt->fetchall (pdo::fetch_assoc);//var_dump ($ users), $smarty->assign (' users ', $users); $query  =  "desc users"; $stmt  =  $pdo Prepare ($query); $smarty->assign ("users", $users); $stmt  ->execute (); $tdname  =  $stmt Fetchall (pdo::fetch_column);//var_dump ($tdname); $smarty->assign (' tdname ', $tdname);//variable output $smarty->display (' HELLO.TPL ');? >

Browser display

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/86/99/wKioL1fFGAiRzc-5AABWwKiXmGM189.png-wh_500x0-wm_3 -wmp_4-s_2544392590.png "title=" Firefox _2016-08-30t05-21-49.768z.png "alt=" wkiol1ffgairzc-5aabwwkixmgm189.png-wh_50 "/ >



This article is from the "jin sha harbor" blog, Please be sure to keep this source http://11410485.blog.51cto.com/11400485/1844261

PHP Smarty Template if function foreach function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.