简体   繁体   中英

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 CODE:

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.

Now my problem is that I want to use these assigned variables in smarty dynamically.

In smarty template file if I write {$contributors1038} then I get a correct output as 9. But I want to use it dynamically. 1038 is the ID, and I have that in my template. If I try to concat the two different variables and expecting the result, I am failed :P

I tried this:

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

But the above variable {$contri} gives me result as contributors1038

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

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

I made the smarty variable an array

PHP

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

And in smarty I am calling it as

SMARTY:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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