Table of Contents
回复内容:
Home Backend Development PHP Tutorial smarty 中循环打印数据库提取的内容

smarty 中循环打印数据库提取的内容

Jun 06, 2016 pm 08:50 PM
php 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>
Copy after login
Copy after login

这段代码,尝试把返回的结果放在一个数组中,像这样

<code>$result = mysql_query("SELECT * FROM article");
$row = mysql_fetch_array($result);
$article = array('id' = $row[id], 'title' = $row[title]);
</code>
Copy after login
Copy after login

然后把 $article 传递给 smarty,代码如下:

<code>$smarty->assign('article',$article);
$smarty->display(index.tpl);
</code>
Copy after login
Copy after login

但是在 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>
Copy after login
Copy after login

这段代码,尝试把返回的结果放在一个数组中,像这样

<code>$result = mysql_query("SELECT * FROM article");
$row = mysql_fetch_array($result);
$article = array('id' = $row[id], 'title' = $row[title]);
</code>
Copy after login
Copy after login

然后把 $article 传递给 smarty,代码如下:

<code>$smarty->assign('article',$article);
$smarty->display(index.tpl);
</code>
Copy after login
Copy after login

但是在 index.tpl 中没有输出传过去的数据,所以我想是不是我的提取并传递数据库数据的方法有问题,因为不能深入理解,所以需要大家的帮忙了。我是一个新人,希望大家帮助我时能讲的详细一些,让我能理解内涵,做到知其然并知其所以然。

<code>$result = mysql_query("SELECT * FROM article");
while($row = mysql_fetch_array($result)){
$article = array('id' = $row[id], 'title' = $row[title]);
}
</code>
Copy after login
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles