简体   繁体   English

将数组从php传递到Smarty .tpl

[英]Passing Array from php to Smarty .tpl

I have been stuck with this for a good while... And I can´t find the way of doing what I want. 我已经坚持了好一阵子...而我找不到想做的方式。 Heres is my code on my "module.php". 这是我在“ module.php”上的代码。

$libroarray = array(306,303,302,307);

foreach ($libroarray as $librorow) 
        {
            $sql = 'SELECT * FROM '._DB_PREFIX_.'image WHERE `id_product` = '.$librorow;
            $results = Db::getInstance()->ExecuteS($sql)    ;

                foreach ($results as $row) 
                    {
                    $var = $row['id_image'].' ---- '.$row['id_product'].'<br />';                       
                    }
        }   

        $this->smarty->assign('libros', $var);
        return $this->display(__FILE__, 'module.tpl');

And I have this on my "module.tpl" 我在我的“ module.tpl”上有这个

{foreach $libros as $item}
{$item}
{/foreach}

My problem is that it only displays the last result of "$var" 我的问题是,它仅显示“ $ var”的最后结果

256 ---- 307

I tried everything I know... no success!!... I will never understand how to work with arrays. 我尝试了我所知道的一切……没有成功!!……我永远也不会理解如何使用数组。 I can´t work them out... I know it only needs a little touch to work, but I simply can´t figure it out 我无法解决这些问题...我知道这只需要一点点的努力就可以解决,但我只是无法解决

Pleaseee... can anybody help? Pleaseee ...有人可以帮忙吗?

$var is a string not an array... use $var[] $var是字符串而不是数组...使用$var[]

$libroarray = array(306,303,302,307);

$var = array();
foreach ($libroarray as $librorow) 
    {
        $sql = 'SELECT * FROM '._DB_PREFIX_.'image WHERE `id_product` = '.$librorow;
        $results = Db::getInstance()->ExecuteS($sql)    ;

            foreach ($results as $row) 
                {
                $var[] = $row['id_image'].' ---- '.$row['id_product'].'<br />';                       
                }
    } 



        $this->smarty->assign('libros', $var);
        return $this->display(__FILE__, 'module.tpl');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM