2016-09-21 87 views
1

因此,使用Latte引擎我似乎无法遍历我的表中的每个数据行。我只能得到第一个,我已经没有想法了!PHP - foreach没有循环遍历整个表

$query = $this->db->query("SELECT id,title FROM table"); 

    $array = array(); 
    while($fetch = $query->fetch_assoc()) { 
     $array = $fetch; 
    } 

    $Template["listing"] = $array; 

模板:

{foreach $listing as $item} 
    {$item["title"]}} 
{/foreach} 

回答

3

Simpliest解决方案(如果你不需要在其他地方$array变量):

$query = $this->db->query("SELECT id,title FROM table"); 

while($fetch = $query->fetch_assoc()) { 
    $Template["listing"][] = $fetch; 
} 
+0

谢谢!有趣的解决方案:)像魅力一样工作。只要有可能,将标记为正确。 –

3

相反的$array = $fetch,运行$array[] = $fetch,以增加更多的项目。

你正在做的是覆盖每个循环中的数组。

+0

感谢您的输入,但我已经尝试过,并且对我无效。 –

+0

它为什么不起作用?当你这样做时你会得到相同的结果吗? – Phiter

+1

这比接受的答案要好,因为它实际上解释了OP做错了什么。 – Andy

0
$query = $this->db->query("SELECT id,title FROM table"); 

$array = array(); 
while($fetch = $query->fetch_assoc()) { 
    $array[] = $fetch; 
} 

$Template["listing"] = $array; 
+0

尽管此代码可能会回答问题,但提供有关为何和/或代码如何回答此问题的其他上下文可提高其长期价值。不鼓励使用仅有代码的答案。 – Ajean

0
$query = $this->db->query("SELECT id,title FROM table"); 

$array = array(); 
while($fetch = $query->fetch_assoc()) { 
    $array[] = $fetch; 
} 
$Template = $array;