简体   繁体   English

Smarty concat并用作变量

[英]Smarty concat and use as a variable

In PHP file I am running a loop and within it I am assigning a variable for smarty. 在PHP文件中,我正在运行一个循环,并在其中分配了smarty变量。

PHP CODE: PHP代码:

foreach($pindata as $Idx => $Val)
{
    $pinId = $Val['id'];
    $sql = mysql_query("select count(userID) as users from pinrest_supporters where pin_id = $pinId;");
    $contributorsSql = mysql_fetch_assoc($sql);
    $contributors = $contributorsSql['users'];
    $smarty->assign("contributors$pinId", $contributors);

}

So the values assigned in smarty are contributors1038 , contributors1039 and so onnn. 因此,在smarty中分配的值是contributors1038contributors1039等。

Now my problem is that I want to use these assigned variables in smarty dynamically. 现在我的问题是我想在smarty中动态使用这些分配的变量。

In smarty template file if I write {$contributors1038} then I get a correct output as 9. But I want to use it dynamically. 在smarty模板文件中,如果我写{$contributors1038}则得到正确的输出为9。但是我想动态地使用它。 1038 is the ID, and I have that in my template. ID是1038,我在模板中有ID。 If I try to concat the two different variables and expecting the result, I am failed :P 如果我尝试连接两个不同的变量并期望结果,我将失败:P

I tried this: 我尝试了这个:

{assign var='contri' value="contributors"|cat:$results[res].id}

But the above variable {$contri} gives me result as contributors1038 但是上面的变量{$contri}给了我作为贡献者的结果

I want to make a new variable by joining two different variable, but this makes it as string. 我想通过连接两个不同的变量来制作一个新变量,但这使它成为字符串。 So can anyone help me? 有人可以帮我吗?

如果您唯一的目的是输出数据,则可以尝试这种方式

{$contributors}{$results[res].id}

Ok I am exhausted trying different solutions, so finally I am doing this to achieve my target. 好吧,我筋疲力尽尝试不同的解决方案,所以最终我这样做是为了实现我的目标。

Instead of below code PHP 而不是下面的代码PHP

$contributors = $contributorsSql['users'];
$smarty->assign("contributors$pinId", $contributors);

I made the smarty variable an array 我把聪明的变量做成一个数组

PHP 的PHP

$contributors[$pinId] = $contributorsSql['users'];
$smarty->assign("contributors", $contributors);

And in smarty I am calling it as 聪明的我称之为

SMARTY: 机巧:

{$contributed[$results[res].id]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM