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条)

报告相同问题?

悬赏问题

  • ¥15 asp.textbox后台赋值前端不能显示什么原因
  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误