【问题标题】:Having some problems showing a query result显示查询结果时出现一些问题
【发布时间】: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 的内容,它是空的吗?

标签: php mysql html pdo


【解决方案1】:

在你的课堂上你应该返回 $this->telefone;

例如:

public function getTelefone() {
    return $this->telefone;
}

编辑:你改了吗?之前是这么说的:

public function getTelefone() {
    return $telefone;
}

【讨论】:

    猜你喜欢
    • 2020-11-01
    • 2011-10-10
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    相关资源
    最近更新 更多