【问题标题】:show mysql query in smarty loop在 smarty 循环中显示 mysql 查询
【发布时间】:2014-07-08 10:56:37
【问题描述】:

我想在 smarty 循环中显示一些类似标题的查询:

table1 (courses): id, title

table2 (questions): id, title, teachers_id

查询:

$this->ctitle_qtitle = DatabaseHandler::GetAll("
                 SELECT courses.title , questions.title
                 FROM courses
                 JOIN questions
                 ON courses.id = questions.courses_id "); 

此查询有效,并且在 .tpl 文件中我使用循环:

{section name=i loop=$obj->ctitle_qtitle}
    <tr>
        <td><h3><a href="#">{$obj->ctitle_qtitle[i].title</a></h3></td>
        <td>{$obj->ctitle_qtitle[i].title}</td>
        <td><a href="#" class="ico del">Delete</a>
         <a href="#" class="ico edit">Edit</a></td>
    </tr>
{/section}

如何在 smarty 结果中使用 course.title 或 question.title ?

【问题讨论】:

  • var_dump($this-&gt;ctitle_qtitle);

标签: php mysql sql loops smarty


【解决方案1】:

您需要从以下位置修改您的 SQL 查询:

$this->ctitle_qtitle = DatabaseHandler::GetAll("
                 SELECT courses.title , questions.title
                 FROM courses
                 JOIN questions
                 ON courses.id = questions.courses_id "); 

$this->ctitle_qtitle = DatabaseHandler::GetAll("
                 SELECT courses.title AS `ctitle` , questions.title AS `qtitle`
                 FROM courses
                 JOIN questions
                 ON courses.id = questions.courses_id "); 

通过这种方式,您可以为列名创建别名(之前这是个问题,因为 2 列具有相同的名称),现在您可以在 Smarty 中简单地使用 ctitle 和 qtitle,例如:

{section name=i loop=$obj->ctitle_qtitle}
    <tr>
        <td><h3><a href="#">{$obj->ctitle_qtitle[i].ctitle</a></h3></td>
        <td>{$obj->ctitle_qtitle[i].qtitle}</td>
        <td><a href="#" class="ico del">Delete</a>
         <a href="#" class="ico edit">Edit</a></td>
    </tr>
{/section}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 2021-12-05
    • 2015-10-13
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    相关资源
    最近更新 更多