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

Smarty处理mysql查询数组

WBOY
WBOYOriginal
2016-06-01 13:13:011118browse

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');

Smarty 模板代码

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

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

Statement:
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