We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Undefined variable (Twig)

Dear Phalcon community,

I'm having some issues with a foreach loop that prints duplicate data.

IndexController:

  public function indexAction()
  $this->view->robots = robots::find();  
  $this->view->robots2 = robots2::find();  

index.volt:

<table>
{% for robot in robots  %}
    {% for robot2 in robots2 %}
    <tr> 
    <td> robot.name </td>
    <td> robots2.name </td>
    </tr>
    {% endfor %}
{% endfor %}
</table>

Is there anyway to do this better? I tried it without the foreach loop but it is needed else i get: Undefined variable.

Thanks in advance,

edited Oct '15

I am not sure what is the problem :/ Can u give us more details what u want to accomplish?

I think u has error in <td> robots2.name </td> - must be robot2. (maybe only in example).

I want to pull data from 2 different tables in the database. It does work when using foreach loops, but i get duplicated data. So i was wondering what the best way is to pull data from 2 different database tables without getting the error Undefined variable and best way to not get duplicated data from volt..

Still didnt get the problem. I never had a issue when use that type of foreach's. Even i have 4 invested foreach without to loose some var, duplicated or undefined variable. So... i do not know how to help you :/ if u give more details about your case, maybe we can find the issue.

Let the force be with u