0

Create an array in smarty template? This does not answer my question. I have a list that I am retrieving from the database using a PHP class and in the template, I have a foreach loop.

What I want to do is create a list from the data retrieved from the database. This what I have.

{assign list=[]}
{foreach from=$prescriptions item=prescription}
    {$list[]=$prescription->drug}
    {$prescription->drug|escape:'html'}
{/foreach}
{$list|print_r}<--- Really not needed as the above prints to the screen.

What's happening is the data is not being appended to the list. It is overwriting the last entry with each pass of the loop.

What I want is the list to be a string drug1,drug2,drug3,drug(n).

Now I get an error message.

/templates/prescription/general_list.html line 150]: syntax error: unrecognized tag: $list[]=$prescription->drug

Can't seem to find the right syntax for adding values to the array.

user1794918
  • 1,131
  • 2
  • 16
  • 34
  • Can't you do this in php file? It is rather an application logic and shouldn't be put into a template. If it has to be in the template, you could try using function `append` [link](https://www.smarty.net/docs/en/language.function.append.tpl) – Pati K Jan 24 '19 at 14:25
  • You are absolutely correct! I end up moving the logic to the controller and sending the results to the display as a constant. – user1794918 Jan 27 '19 at 20:27

0 Answers0