smarty 中循环打印数据库提取的内容
我是刚刚接触 smarty 模版引擎,所以对 smarty 理解不是很透彻.在用 smarty 时出现了这样的问题。
在做一个网页,需要用 smarty 循环打印从数据库提取的内容,遇到了困难。首先是提取数据传递的问题,按书上说的是把从数据库提取的内容放在一个数组中,传递给 smarty 模版,然后在模版中用 section 语句循环打印,但是始终没有成功。因为对 section 的语法不太理解。在循环打印时,如果按语法写 $article[line].id 就没有输出,我尝试改成 $article.id,这样编程循环打印其中某一个 id 的值很多遍,而不是把数据库中的每一个id值打印出来。
我反思了一下,出现这些问题的原因就是 smarty 的理解不够,只知其然而不知其所以然。 php 对我来说其实也是新事物,出现这些问题和对 php 中对数据库的 select 也有关。我的 php 的 select 返回的结果也不是很清楚,书上只教了怎么把结果用 while 循环打印,而没有说返回的结果怎么保存。我自己根据
<code>$result = mysql_query("SELECT * FROM article"); while($row = mysql_fetch_array($result)){ echo $row[id]; } </code>
这段代码,尝试把返回的结果放在一个数组中,像这样
<code>$result = mysql_query("SELECT * FROM article"); $row = mysql_fetch_array($result); $article = array('id' = $row[id], 'title' = $row[title]); </code>
然后把 $article 传递给 smarty,代码如下:
<code>$smarty->assign('article',$article); $smarty->display(index.tpl); </code>
但是在 index.tpl 中没有输出传过去的数据,所以我想是不是我的提取并传递数据库数据的方法有问题,因为不能深入理解,所以需要大家的帮忙了。我是一个新人,希望大家帮助我时能讲的详细一些,让我能理解内涵,做到知其然并知其所以然。
回复内容:
我是刚刚接触 smarty 模版引擎,所以对 smarty 理解不是很透彻.在用 smarty 时出现了这样的问题。
在做一个网页,需要用 smarty 循环打印从数据库提取的内容,遇到了困难。首先是提取数据传递的问题,按书上说的是把从数据库提取的内容放在一个数组中,传递给 smarty 模版,然后在模版中用 section 语句循环打印,但是始终没有成功。因为对 section 的语法不太理解。在循环打印时,如果按语法写 $article[line].id 就没有输出,我尝试改成 $article.id,这样编程循环打印其中某一个 id 的值很多遍,而不是把数据库中的每一个id值打印出来。
我反思了一下,出现这些问题的原因就是 smarty 的理解不够,只知其然而不知其所以然。 php 对我来说其实也是新事物,出现这些问题和对 php 中对数据库的 select 也有关。我的 php 的 select 返回的结果也不是很清楚,书上只教了怎么把结果用 while 循环打印,而没有说返回的结果怎么保存。我自己根据
<code>$result = mysql_query("SELECT * FROM article"); while($row = mysql_fetch_array($result)){ echo $row[id]; } </code>
这段代码,尝试把返回的结果放在一个数组中,像这样
<code>$result = mysql_query("SELECT * FROM article"); $row = mysql_fetch_array($result); $article = array('id' = $row[id], 'title' = $row[title]); </code>
然后把 $article 传递给 smarty,代码如下:
<code>$smarty->assign('article',$article); $smarty->display(index.tpl); </code>
但是在 index.tpl 中没有输出传过去的数据,所以我想是不是我的提取并传递数据库数据的方法有问题,因为不能深入理解,所以需要大家的帮忙了。我是一个新人,希望大家帮助我时能讲的详细一些,让我能理解内涵,做到知其然并知其所以然。
<code>$result = mysql_query("SELECT * FROM article"); while($row = mysql_fetch_array($result)){ $article = array('id' = $row[id], 'title' = $row[title]); } </code>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
