怎样用smarty的模板显示mysql数据库里的表格显示出来?
本人新手刚接触php.对于smarty更是少,前面用php写了个页面,简单的用数据库的内容显示用表的方式显示到网页上.
但是问题来了,表越来越多,刷新一下页面每次都会占大部分CPU,发现这样不行,于是搜索到了有关缓存的问题.接着发现了 smarty
这几天也看了不一些例子,和一些视频,自己也做过很多测试,还是不知道如何下手讲数据库表用 smarty模板 显示出来..
表格很简单.如下
数据库表名.my_db
id 名字 性别 年龄 身高 体重 身份证号码 家庭住址 联系电话
1 张三 男 32 173cm 66 100******** 北京市东城区 134*******
2 李四 男 30 178cm 65 100******** 北京市西城区 134*******
3 王五 男 30 179cm 63 100******** 北京市东城区 134*******
4 赵六 男 29 181cm 67 100******** 北京市东城区 186*******
5 小红 女 12 143cm 40 100******** 北京市房山区 无电话
6 小明 男 14 163cm 55 100******** 北京市房山区 无电话
7 小李 男 12 155cm 45 100******** 北京市西城区 无电话
我想要显示的是 按年龄的大小 显示到 网页上但是用smarty我真的不知道如何下手,不知道该怎么写..论坛上也没找到这样的例子应该怎么写,希望会的朋友给我做个式利帮我写一下.非常感谢!!
一下是热心人给出的源码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
//table方法,用来查询数据库
function table(){
//初始化连库函数,这里$conn是之前实例化的pdo
globle $conn;
//按照条件查询
$sel=$conn->query("select * from my_db order by age");
//返回变量到函数
return $sel->fetchAll();
}
$smarty->assign('table',table());
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
include("smarty_inc.php");
$con = mysql_connect("127.0.0.1","user","123456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//$db=new pdo('mysql:127.0.0.1;mysql','user','123456','true');
mysql_query("set names gbk");
mysql_select_db("test",$con);
$result=mysql_query("select * from my_db");
<?php include("smarty_inc.php");
$con = mysql_connect("127.0.0.1","user","123456");
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_query("set names gbk");
mysql_select_db("test",$con);
$result=mysql_query("select * from my_db order by age");
while($row=mysql_fetch_assoc($result)){
$table[]=$row;
}
$smarty->assign('table',$table);
<br><font color="#e78608">------解决方案--------------------</font><br>前台利用smarty的assign赋值给变量。然后利用smarty的section进行循环显示。 <div class="clear"></div>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号