PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 18, 2022

[FIXED] How can I create condition "if there is no event belonging to the selected category" in Symfony twig?

 October 18, 2022     if-statement, symfony     No comments   

Issue

I'd like to show an element if there is no event belonging to the category. Before landing on list of events user has selected a category previously in a search form.

How can I write a condition saying 'if there is no event belonging to the selected category' ?

I tried this but I ended up with this error :

Key "category" for array with keys "0" does not exist.

My code on twig :

{% if events.category.id != category.id %}

    <a href="#">Comment participer ?</a>

{% endif %}

Solution

From the error text it seems that your events variable is an array of objects, so you can't use .category.id in a condition without a loop. You need a loop to determine if the event is set with the category you want in the array, if not, display your message

{% set event_isset = false %}
{% for event in events %}
    {% if event.category.id == category.id %}
        {% set event_isset = true %}
    {% endif %}
{% endfor %}

{% if not event_isset %}
    <a href="#">Comment participer ?</a>
{% endif %}


Answered By - Harvey Dent
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing