dputlf5431
2019-03-14 14:32
浏览 57
已采纳

如何在Smarty / Prestashop中循环数组?

I use this code in Prestashop

{if (strpos($product.name, 'TVNUMBER1') !== false)}
    THIS PRODUCT IS IN SALE
{/if}

So whenever I want to display that certain products are in sale, I have to go line by line, specifying the same product i.e."TVNUMBER1". I want to be able to write an array detailing all the products I have in sale "TV1, TV2, TV3", and get a code like this:

{if (strpos($product.name, '$array') !== false)}
    THIS PRODUCT IS IN SALE
{/if}

I've tried similar examples found here, but I can't get them to work, either in Prestashop or in PHP testers online. It looks super simple, but I can't get around it.

图片转代码服务由CSDN问答提供 功能建议

我在Prestashop中使用此代码

  {if(strpos)  ($ product.name,'TVNUMBER1')!== false)} 
此产品已售出
 {/ if} 
   
 
 

所以每当我想要的时候 为了显示某些产品在销售,我必须逐行,指定相同的产品,即“TVNUMBER1”。 我希望能够编写一个数组详细介绍我销售的所有产品“TV1,TV2,TV3”,并获得如下代码:

   {if(strpos($ product.name,'$ array')!== false)} 
此产品已售出
 {/ if} 
   
 \ 我已经尝试过类似的例子,但我无法让它们在Prestashop或在线PHP测试人员中工作。 它看起来非常简单,但我无法解决它。 
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • du12197 2019-03-26 03:02
    已采纳

    I think what you want is the in_array php function, that check if a given $needle is or not in an array.

    So what you should do is :

    {if (in_array($product.name, '$array') !== false)}
        THIS PRODUCT IS IN SALE
    {/if}
    

    Then in your controller you can assign the array to smarty :

    $arr = array('TVNUMBER1', 'TVNUMBER2', 'TVNUMBER3');
    $smarty->assign('myArray', $arr);
    
    已采纳该答案
    打赏 评论
  • doulang9521 2019-03-14 14:41

    It seems you are using Smarty as template engine. So you could do something like this (from the doc).

    In the controller

    //Give it to the view
    $arr = array('TVNUMBER1', 'TVNUMBER2');
    $smarty->assign('myArray', $arr);
    

    And in the view

    //In the view, loop over the array
    {foreach from=$myArray item=productName}
        //If your product is among the in-sale ones, show the message
        {if (strpos($product.name, productName) !== false)}
            THIS PRODUCT IS IN SALE
        {/if}
    {/foreach}
    
    打赏 评论

相关推荐 更多相似问题