【发布时间】:2012-01-18 21:47:27
【问题描述】:
我在这个 mysql 表中有一些数据,但在 html 表中没有显示任何内容。
但我几乎可以肯定代码没有错。
(Obs:我正在使用“Smarty PHP 模板”,只是为了不将 html 与 php 混合)
另一项观察,我没有粘贴 (pesquisa.tpl) 的完整代码。
想象一下 aluno=user 和 pesquisa=search
-> pesquisa_aluno.class.php
<?php
class PesquisaAluno {
private $nome;
private $sobrenome;
private $rg;
private $email;
private $telefone;
public function __construct($nome, $sobrenome, $rg, $email, $telefone) {
$this->nome = $nome;
$this->sobrenome = $sobrenome;
$this->rg = $rg;
$this->email = $email;
$this->telefone = $telefone;
}
public function getNome() {
return $this->nome;
}
public function getSobrenome() {
return $this->sobrenome;
}
public function getRg() {
return $this->rg;
}
public function getEmail() {
return $this->email;
}
public function getTelefone() {
return $this->telefone;
}
}
?>
->pesquisa.php
<?php
include("classes/pesquisa_aluno.class.php");
$alunos = array();
foreach ($connection->query("SELECT * FROM alunos") as $row) {
$aluno = new PesquisaAluno($row["nome"], $row["sobrenome"], $row["rg"], $row["email"], $row["telefone"]);
$alunos[] = $aluno;
}
$smarty->assign('alunos', $alunos);
?>
-> pesquisa.tpl
{foreach from=$alunos item=aluno}
<tr>
<td>{$aluno->getNome()}</td>
<td>{$aluno->getSobrenome()}</td>
<td>{$aluno->getRg()}</td>
<td>{$aluno->getEmail()}</td>
<td>{$aluno->getTelefone()}</td>
</tr>
{/foreach}
【问题讨论】:
-
你能在 smarty 赋值之前插入一个
var_dump($alunos)吗? -
抱歉,我的 handler.php 页面中没有包含 php 文件...这是错误的。
-
@RamonSaraiva - 在您的
foreach循环之后尝试使用print_r($alunos);显示$alunos的内容,它是空的吗?