Search code examples
countcategoriesphalconvolt

Phalcon count in volt


I have a counting problem in phalcon volt. I have a table named category and there I have two columns id and cname, and also have a table blog and there is a column category. I want to show how many post have in each category.
When I insert a post into blog table, in category column I'm inserting its category id. First of I just retrieve list of all category like this:

[controller]
$categories = Category::find();
$this->view->setVar('category', $categories);
$cx = Blogs::find();
$this->view->setVar('cates',$cx);

[Volt]
{% for categories in category %}
<a href="blog/category/{{categories.cname}}" class="tags">{{ categories.cname }} 
<span>[ 
{% for cx in cates %}
    {%if cx.category === categories.id %}
        <?php echo(count($cx->category)); ?>
    {% endif %}
{% endfor %}
]</span></a>
{% endfor %}

Its render like "1 1 1" or "1 1" or "1" but it should render like "3" or "2" or "1" whats my wrong?

I also tried like this but did not get the expected output:

{% for categories in category %}
<a href="blog/category/{{categories.cname}}" class="tags">{{ categories.cname }} 
<span>[ 
{% for cx in cates %}

{%if cx.category === categories.id %}
{% if loop.first %} {{ loop.length }} {% endif %}

{% endif %}

{% endfor %}
]</span></a>
{% endfor %}

Solution

  • No Sir, its not working. But i just solved my problem like this :

    [controller]
    $categories = Category::find();
    $this->view->setVar('category', $categories);
    
    [volt]
    
    {% for categories in category %}
    <a href="blog/category/{{categories.cname}}" class="tags">{{ categories.cname }} 
    <span>[ 
    <?php 
    $catcount = $this->modelsManager->executeQuery("SELECT Blogs.category FROM Blogs WHERE Blogs.category = $categories->id");echo(count($catcount));
    ?>
    ]</span></a>
    {% endfor %}
    

    Now its working as expected. and here i dont make any relations ion model.Is it ok Sir. Please! Thnx