PHP Smarty 模板 section函数 输出表格

从数据库查询数据,浏览器以表格形式显示

注意区别index 和iteration

index为数组下标索引

iteration为序号

模板页面

<table border="1" width="800" align="center">
<caption>用户信息表</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}>
                    没有用户查询出来!
            <{/section}>
</table>

php页面

<?php 
//创建smarty对象
require_once ‘./libs/Smarty.class.php‘;
//定义根目录
define(‘ROOT‘, str_replace("\\", "/",dirname(__FILE__))."/");
//实例化Smarty类
$smarty=new Smarty();
//设定定界符
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
//设置为false 定界符号左右可以有空格
$smarty->auto_literal = false;

//添加一个插件的目录
//$smarty->setPluginsDir(ROOT."/libs/myplugins/");

//注意添加一个插件,要把系统默认设置的路径加入 否则不能使用默认系统的插件
$smarty->setPluginsDir(array(
    ROOT."/libs/plugins/",//系统默认设置的路径
    ROOT."/libs/myplugins/",//自定义的
));

//连接数据库
const  DSN = ‘mysql:host=localhost;dbname=test‘;
const   DBUSER = ‘root‘;
const   DBPWD     = ‘root‘;
try{
    $pdo = new PDO(DSN, DBUSER,DBPWD);
}catch(PDOException $e){
    echo "数据库连接失败:".$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);

//变量输出
$smarty->display(‘hello.tpl‘);

?>

浏览器输出

时间: 2024-12-14 22:12:50

PHP Smarty 模板 section函数 输出表格的相关文章

PHP Smarty 模板 自定义函数function和块函数block

自定义函数 function.yangA.php页面 采用插件形式调用 <?php /*  * 文件名    * function.函数名.php    function.yangA.php  * 声明的函数名规则  * smarty_function_函数名()  smarty_function_yangA  * 参数  * 1.数组 array  * 2.smarty  * 模板使用  * <{yangA content="I am Mr.Yang" color=&qu

PHP Smarty 模板 if函数 foreach函数

从数据库查询数据,浏览器以表格形式显示 模板页面 <table border="1" width="800" align="center"> <caption>用户信息表</caption>             <{foreach $tdname as $val}>                             <th><{$val}></th>  

smarty模板section循环用法

{foreach name=foreach_name key=k item=v from=$arr} {$k}=>{$v}<br/> {/foreach} {section name=sec_name loop=$arr start=0 step=n max= m show=true/false } {if is_array($arr[sec_name])} {section name=sec2 loop=$arr[sec_name]} {$arr[sec_name][sec2]}<

Python之使用转义序列 \n 和 \t 跟 expandtabs 函数输出表格

示例: text = "username\temail\tpassword\nashdfh\tfiodfh@q.com\ty567\nsdfiuh\tadfhisoj@163.com\t423678\nefuih\tosefih@q.com\t78\n" v = text.expandtabs(20) print(v) 输出如下 原文地址:https://www.cnblogs.com/lzn-2018/p/10593145.html

Smarty模板函数

1.{$var=...} 这是{assign}函数的简写版,你可以直接赋值给模版,也可以为数组元素赋值. <{$a = 10}><!--赋值语句--> <{$a}><!--输出语句,输出结果为10--> 2.{appeng} {append}用于在模板执行期间建立或追加模板变量数组. 3.{assign} {assign}用来在模板运行时为模板变量赋值. 4.{block} {block}用来定义一个命名的模板继承源区域.意思就是一个模板可以继承另外一个模板

smarty模板引擎中section循环loop与total的区别

在smarty模板引擎的section循环中 $data=[101,102,103,105,104]; section的两个属性total与loop {section foo $data start=1 step=2} {$smarty.section.foo.total}--输出2 {$smarty.section.foo.loop}--输出5 {/section} 意即:使用total输出的是循环执行的次数,使用loop输出的是所循环数据的count

四、smarty模板的自定义函数

smarty模板的自定义函数(这里介绍的是常用) 分为三个种类: 1.  变量调节器 2.  函数 3.  块函数 三个种类插件的用法: 1.  变量调解器的用法, <{$var|myfun:arg1:arg2}> 2.  函数的用法(和使用HTML标记很像) 如, <{myfun color=”red” size=”7” num=”7”}> 在PHP中定义的函数为: $smarty->registerPlugin(“function”,”myfun”,”one”); fun

在smarty模板中使用PHP函数的方法

在smarty模板中如果要在显示的资料使用php函数时,如果是只有一个参数的函数比如说去空白的trim会写成 sample1 代码如下: <{$colname|trim}> 那如果使用像iconv这样的有三个参数的函数该怎么写呢?如果写成: sample 2 代码如下: <{$colname|iconv:'utf-8':'gbk'}> 一执行就会发现显示error信息. 因此研究一下就会发现,起始在smarty模板页的套用函数用法中,以smaple 1来说,trim的前面$Row-

smarty详细使用教程(韩顺平smarty模板技术笔记)

MVC是一种开发模式,强调数据的输入.处理.显示是强制分离的 Smarty使用教程1.如何配置我们的smarty解压后把libs文件夹放在网站第一级目录下,然后创建两个文件夹templates 存放模板文件templates_c 存放编译后的文件再创建初始化文件smarty.ini.php 注意事项:1.替换变量的标识分隔符一般使用<{}>改动分隔符的两个方法:1.改源码:Smarty.class.php $left_delimiter 不推荐2.动态修改:$Smarty->left_d