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