smarty简单应用实例
网络编程 2021-07-05 09:09www.168986.cn编程入门
这篇文章主要介绍了smarty简单应用实例,简单介绍了smarty中数据库查询、数组遍历及调用模板显示数据的相关技巧,简单易懂,具有一定参考借鉴价值,需要的朋友可以参考下
本文讲述了smarty简单应用实例。分享给大家供大家参考,具体如下:
<?php require 'smarty/libs/Smarty.class.php'; $smarty = new Smarty; $smarty->template_dir="smarty/templates/templates"; $smarty->pile_dir="smarty/templates/templates_c"; $smarty->config_dir="smarty/templates/config"; $smarty->cache_dir="smarty/templates/cache"; $hello = "Hello World!"; $smarty->pile_check = true; //$smarty->debugging = true; $smarty->debugging = false; $smarty->caching=true; $conn=mysql_connect("localhost", "root","root"); //打开MySQL服务器连接 mysql_select_db("lava_guess"); //链接数据库 mysql_query("set names utf8"); //解决中文乱码问题 $sql_list="Select * from t_sys_msg Order by id desc limit 0,10"; $result_list=mysql_query($sql_list); //执行sql语句,返回结果 //把记录集转换为数组 while($rs_list=mysql_fetch_array($result_list)) { $msg_array[]=$rs_list; } $array[] = array("newsID"=>1, "newsTitle"=>"第1条新闻"); $array[] = array("newsID"=>2, "newsTitle"=>"第2条新闻"); $array[] = array("newsID"=>3, "newsTitle"=>"第3条新闻"); $array[] = array("newsID"=>4, "newsTitle"=>"第4条新闻"); $array[] = array("newsID"=>5, "newsTitle"=>"第5条新闻"); $array[] = array("newsID"=>6, "newsTitle"=>"第6条新闻"); $smarty->assign("newsArray", $array); $smarty->assign("hello",$hello); //赋值 $smarty->assign("msglist",$msg_array); $smarty->display('index.tpl'); ?>
模板文件:index.tpl
<html> <head> <metahttp-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Smarty</title></head> <body> <hr> {$hello} <hr> {section name=loop loop=$msglist} id: {$msglist[loop].id}--{$msglist[loop].title}----{$msglist[loop].receiver}<br> {/section} <hr> {section name=loop loop=$newsArray} 新闻编号:{$newsArray[loop].newsID}<br> 新闻标题:{$newsArray[loop].newsTitle}<br><hr> {sectionelse} 对不起,没有任何新闻输入! {/section}
希望本文所述对大家基于smarty模板的php程序设计有所帮助。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程