doulin6088 2013-06-29 13:03
浏览 43
已采纳

Smarty 2变量尝试在同一个字符串中运行2时出错

I currently have the following in my database

tn_pic1

tn_pic2

They hold a filename for each one, for example tn_X_wm.jpg

X = the number and jpg could be gif, png, bmp

So for example in tn_pic1 I could have tn_1_wm.jpg but in tn_pic2 I could have tn_2_wm.png

In my .tpl file I currently have:

{if $item.pic_count > 0}
   {rand(1,$item.pic_count) assign="yourVar"}

   <img src="users/images/{$item.id}/{$item.tn_pic1}" width="100" height="100" class="smallpic" alt="" />
{/if}

I would like to use the rand number in $item.tn_pic1

I have tried {$item.tn_pic{$yourVar}} but get an error as I have 2 {}

So what I am wanting is {$item.tn_picX}

X being the rand number.

I am new'ish to smarty and not really sure how to sort it.

$item. pull from the database via a .php file

  • 写回答

1条回答 默认 最新

  • dqkx69935 2013-06-29 13:12
    关注

    You have to create whole key you want to read, assign it into variable and then access it without additional set of curly braces, something like:

    {capture assign=pictureKey}tn_pic{rand(1,$item.pic_count)}{/capture}
    <img src="users/images/{$item.id}/{$item[$pictureKey]}" width="100" height="100" class="smallpic" alt="" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀