PHP Smarty Template section function output table

Source: Internet
Author: User
Tags dsn smarty template

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

Note the difference between index and iteration

Index is indexed for array subscript

Iteration as serial number

Template page

<table border= "1"  width= " align=" "Center" ><caption> User Information Sheet </caption>             <th align= "Center" >index</th >            <th align= "Center" > iteration</th>            <{foreach  $tdname  as  $val}>                             <th align= " Center "><{$val}></th>             <{/foreach}>            <{section  Name= "One"  loop= $users  step=2 start=2}>                             <{if $ smarty.section.one.first}>                             <tr bgcolor= "Red"  align= "center" >                     <{elseif  $smarty .section.one.last}>                              <tr bgcolor= "Yellow"  align= "center" >                      <{elseif  $smarty .section.one.iteration is even}>                             <tr  Bgcolor= "Pink"  align= "center" >                     <{else}>                              <tr bgcolor= "Green"  align= "center" >                     <{/if}>                                                       <td><{$ Smarty.section.one.index}></td>                                 <td><{$ smarty.section.one.iteration}></td>                                  <td><{$users [one].id}></td>                                  <td><{$users [one].username}></td>                                  <td><{$users [One].password} ></td>                                 <td><{$users [one].email}></td>                      </tr>                     <{sectionelse}>                      no user queries come out!             <{/section}></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); $smarty Assign (' users ', $users);//var_dump ($users); $query  =  "desc users"; $stmt  =  $pdo Prepare ($query); $stmt  ->execute (), $tdname  =  $stmt->fetchall (pdo::fetch_column);//var_dump ($tdname); $smarty->assign (' Tdname ', $tdname);//variable Output $smarty->display (' Hello.tpl ');? >

Browser output

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/86/9B/wKioL1fFJxKALG6BAAAyQVP1Cy4471.png-wh_500x0-wm_3 -wmp_4-s_909034851.png "title=" Firefox _2016-08-30t06-18-03.986z.png "alt=" wkiol1ffjxkalg6baaayqvp1cy4471.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/1844299

PHP Smarty Template section function output table

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.