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.

  • 写回答

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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效