阿使得肌肤· 发表于 2017-3-25 08:52:00

简单的php+smarty应用

/*---------------------------php代码-----------------------------------------*/
<?php
//定义数据库连接
$conn = mysql_connect("localhost","root","K2009sense");
//定义要操作的数据库名
$db = mysql_select_db("user");
//执行操作语句
$result = mysql_query("select * from userInfo");
//取出当前表里表头字段的信息
while ($fieldInfo = mysql_fetch_field($result))
{
$field[] = $fieldInfo->name;
//echo $field;
}
print_r($field);
//取出当前表里的数据并转化为数组
while ($row = mysql_fetch_array($result))
{
$info[] = $row;
}
/*//在源文件中提示这个资源的相关信息
var_dump($result);
//打印数组
print_r($info);*/
//关闭数据库
mysql_close($conn);


//引用smarty模板
include_once("Smarty_setup.php");
//构建smarty对象
$smarty = new Smarty();      
$smarty->assign("title","这是我自己的smarty分层代码");
//将表头信息逐个赋值给模板页变量
$smarty->assign("field_id",$field);
$smarty->assign("field_name",$field);
$smarty->assign("field_pwd",$field);
//将存储表中数据的数组赋值给模板页变量
$smarty->assign("bodyInfo",$info);
    //将模板页面加载显示在当前页面
$smarty->display("test3.html");
?>

/*-----------------------------smarty里面的模板----------------------------------------*/
<html>
<head>
   <title>
         {$title}
      </title>
    </head>
    <body>
   <table>
         <thead>
             <tr>
                  <td>{$field_id}</td>
                  <td>{$field_name}</td>
                  <td>{$field_pwd}</td>
                </tr>
            </thead>
            <tbody>
             {foreach from=$bodyInfo item=itemInfo}
                <tr>
               <td>{$itemInfo.id}</td>
                  <td>{$itemInfo.name}</td>
                  <td>{$itemInfo.pwd}</td>
                </tr>
                {/foreach}
            </tbody>
      </table>
    </body>
</html>
页: [1]
查看完整版本: 简单的php+smarty应用