Ismail
Ismail

Reputation: 9592

phalcon - how to loop through associative (array) model::find(); to output keys

I want to loop through the columns of the model::find results. What I thought was possible is to cast the returning object to an array to be able to loop throught the columns, but that does not work.

Here is my Controller code:

<?php
class ManageController extends ControllerBase
{
    public function indexAction()
    {
        $this->view->setVar("pages",(array) Pages::find());
    }
}

And view code:

    {% for key,value in pages %}
    <p>key: {{key}}</p>
    {% endfor%}

Any help would be usefull

Upvotes: 0

Views: 1467

Answers (1)

cvsguimaraes
cvsguimaraes

Reputation: 13240

Use this;

<?php
class ManageController extends ControllerBase
{
    public function indexAction()
    {
        $this->view->setVar("pages", Pages::find());
    }
}

And view code:

{% for page in pages %}
   {# in this case the key is just "0,1,2,3..." #}
   {# so we use the loop index (or loop.index0 for zero based) #}
   <p>This is the page #{{ loop.index }}</p>
   <p>{{ page.title }}</p>
{% endfor%}

But if you really need to loop through the keys too, use:

{% for key, value in items %}
    Key: {{ key }}
    Value: {{ value }}
{% endfor%}

Upvotes: 2

Related Questions