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

谁都会走 提交于 2019-12-25 12:46:56

问题


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


回答1:


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%}


来源:https://stackoverflow.com/questions/23716476/phalcon-how-to-loop-through-associative-array-modelfind-to-output-keys

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!