Home > Database > Mysql Tutorial > Smarty处理mysql查询数组

Smarty处理mysql查询数组

WBOY
Release: 2016-06-01 13:13:01
Original
1073 people have browsed it

Smarty处理mysql查询数组

MySQL的查询结果一般是一个数组,而不是所有结果集。因此我们需要将结果全部存到数组中进行处理,然后就可以很轻松的再Smarty中使用了。

PHP Mysql 代码

$sql="select article_id,article_title from tbl_article order by article_id desc limit 0,5";$result=mysql_query($sql);while($row=mysql_fetch_array($result)){$record[]=array('id'=>$row['article_id'],'title'=>$row['article_title']);}$smarty = new Smarty;$smarty->template_dir ="./template";$smarty->compile_dir ="./templates_c";$smarty->left_delimiter = "{right_delimiter = ">}";//我自己自定义的可以避免与HTML冲突$smarty->assign('doclist',$record);$smarty->display('index.tpl');
Copy after login

Smarty 模板代码

{<foreach from="$doclist" item="curr_item">} <div><a href="show.php?id=%7B<%24curr_item.id>%7D">{}</a></div>{</foreach>}
Copy after login

这样就可以完美解决分页显示和模板的问题了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template