【问题标题】:Loop in twig repeats row 1 of my database 5 times, instead of showing all the rows.树枝中的循环重复我的数据库的第 1 行 5 次,而不是显示所有行。
【发布时间】:2014-10-06 09:28:28
【问题描述】:

本周初我开始使用 Silex 和 Twig。现在我发现了一个奇怪的问题:

我在 index.php 中的代码

//start sql request. teleport it to the twig file, without satanic php
$app->get('/', function() use ($app){
$sql = 'SELECT * FROM projectvdg'  ;

$text = $app['db']->FetchAssoc($sql);

return $app['twig']->render('start.twig', [
  'content' => $text
]);
});

在“start.twig”(我在上面的部分中请求的文件)中

{% for title in content %}
<p> {{ content.title }} </p>
{% endfor %}

据我了解,这应该是一个循环,其中显示了我的数据库的所有 (4) 个标题。但相反,它会生成:

title1
title1
title1
title1
标题1

如果我尝试 FetchAll,而不是 FetchAssoc,我会收到以下消息:

Twig_Error_Runtime:第 16 行的“start.twig”中不存在键为“0、1、2、3”的数组的键“标题”

问题是:我在这段代码中犯了什么错误?例如,我应该使用 FetchAll,然后只更改行的名称吗?

谢谢

【问题讨论】:

    标签: php twig silex


    【解决方案1】:

    您在循环中访问了错误的变量。

    {% for title in content %}
    <p> {{ title }} </p>
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2019-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      • 2017-06-01
      • 2016-06-18
      • 2014-03-19
      相关资源
      最近更新 更多