Php Smarty concat并用作变量
在PHP文件中,我运行一个循环,并在其中为smarty分配一个变量 PHP代码:Php Smarty concat并用作变量,php,smarty,concatenation,Php,Smarty,Concatenation,在PHP文件中,我运行一个循环,并在其中为smarty分配一个变量 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(
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);
}
因此,在smarty中指定的值是contributors1038
,contributors1039
等等
现在我的问题是,我想在smarty中动态使用这些分配的变量
在smarty模板文件中,如果我写入{$contributors1038}
,则得到正确的输出,即9。但我想动态地使用它。1038是ID,我的模板中有。如果我试图将两个不同的变量结合起来并期望得到结果,我就失败了:P
我试过这个:
{assign var='contri'value=“contributors”| cat:$results[res].id}
但是上面的变量{$contri}
将结果作为contributors1038提供给我
我想通过连接两个不同的变量来创建一个新变量,但这使它成为字符串。有人能帮我吗?如果你的唯一目的是输出数据,你可以这样尝试
{$contributors}{$results[res].id}
好吧,我尝试不同的解决方案已经筋疲力尽了,所以最后我要做的就是实现我的目标 而不是下面的代码 PHP 我将smarty变量设置为数组 PHP 在smarty,我称之为 SMARTY:
{$contributed[$results[res].id]}
尝试在
assign
中使用value=$contributors
,也许这会起作用。当我尝试这样的{assign var='contri'value=$contributors | cat:$results[res].id}
时,我只是得到id,即。1048@ntechi你得到的是什么o/p?只有ID,因为没有变量作为$contributors
所有变量都是$contributors1038
,$contributors1039
等
$contributors[$pinId] = $contributorsSql['users'];
$smarty->assign("contributors", $contributors);
{$contributed[$results[res].id]}