Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Double foreach in one

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
ben4523
Smarty n00b


Joined: 18 Mar 2017
Posts: 1

PostPosted: Sat Mar 18, 2017 11:42 am    Post subject: Double foreach in one Reply with quote

I have some problem with a code. I would like to create triple array with all product images but when i foreach my second array the foreach add some undefined value.

This is my code :
Code:

public function getImages($id_lang, $idprod)
    {
        return Db::getInstance()->executeS('
            SELECT image_shop.`cover`, i.`id_image`, il.`legend`, i.`position`
            FROM `'._DB_PREFIX_.'image` i
            '.Shop::addSqlAssociation('image', 'i').'
            LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
            WHERE i.`id_product` = '.(int)$idprod.'
            ORDER BY `position`'
        );
    }
    public function func_get_images()
    {
        /****************** get products *********************/
                  $products = Db::getInstance()->executeS(
                 'SELECT
                     p.id_product,
                     pl.link_rewrite AS product_link,
                     pl.name AS product_name,
                     cl.link_rewrite AS category_link,
                     c.position,
                     cl.name AS category_name,
                     p.id_category_default
                 FROM '._DB_PREFIX_.'product_lang pl
                 INNER JOIN '._DB_PREFIX_.'product p
                 ON pl.id_product = p.id_product
                 INNER JOIN '._DB_PREFIX_.'category c ON c.id_category = p.id_category_default
                 INNER JOIN '._DB_PREFIX_.'category_lang cl
                 ON cl.id_category = p.id_category_default
                 ORDER BY `position`');
        /******************* /get products********************/
        /****************** get product image *********************/
                 $i=0;
                 foreach ($products as $product_item){
                    //echo $product_item['id_category_default']."\n";
                    $products[$i]['id_product'] = $product_item['id_product'];
                    $products[$i]['id_product_cat'] = $product_item['id_category_default'];
                    if ($product_item['id_category_default'] != 20)
                    {
                        //echo $product_item['id_category_default']."\n";
                        $images = $this->getImages(1, $product_item['id_product']);
                        $y=0;
                        foreach ($images as $images_item){
                                $products[$i][$y]['image_url'] = Tools::getHttpHost(true).__PS_BASE_URI__.$images_item['id_image']."/".$product_item['product_link'].".jpg";
                                $products[$i][$y]['category_name'] = $product_item['category_name'];
                                $products[$i][$y]['legend'] = $images_item['legend'];
                                $y++;
                        }
                        $i++;
                    }
                 }
        /******************* /get product image********************/
                // $this->context->smarty->assign('products', $products);
                // return $this->display(__FILE__, 'displayTop.tpl');
                return $products;
        }

And this is my tpl code
Code:

{foreach from=$products item=product_item}
                                        <br/>  <br/>  <br/>  <br/>  <br/>
                                        <h1>Chagement de cat</h1>
                                        {if $product_item.id_product_cat != 20}
                                          {$i = 0}
                                          {foreach from=$product_item item=product_it}
                                            {$product_item.id_product}
                                            <img src="{$product_it.image_url}">
                                            {$product_it.category_name}
                                            {$product_it.legend}
                                            {$i++}
                                          {/foreach}
                                          {$i}
                                        {/if}
                                      {/foreach}

he work but the second while add more tours. and in my page we have this error
Warning: Illegal string offset 'category_name' ....
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Mon Mar 20, 2017 6:39 pm    Post subject: Reply with quote

I think when you are iterating through $products with foreach(), PHP makes a copy of $products and uses that. Thus, if you want to actually change the elements of the $products array, you need to have the $product_item be associated with the array by reference.

See the PHP manual for foreach control structure.

Code:
foreach ($products as &$product_item){
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP