Adding If statement inside a loop in views
- This topic has 5 replies, 2 voices, and was last updated 3 years, 5 months ago by
Long Nguyen.
-
AuthorPosts
-
October 26, 2021 at 12:29 AM #31504
James
ParticipantHi, Metabox. Can you help me how can I make this work? How can I add if statement inside the for loop? I don't know why this is not working. So basically I add the fields inside the group "upload_documents". Now, I want to check if the "project_documents" is empty or not. Thanks in advance.
{% for clone in post.upload_documents %} {% for item in clone.project_documents %} {% for item in clone.project_documents %} {% if item.project_documents is empty %} <h3>Its empty</h3> {% else %} <h3>Its not empty</h3> {% endif %} {% endfor %} {% endfor %} {% endfor %}
October 26, 2021 at 1:54 PM #31516Long Nguyen
ModeratorHi Kirb,
I see you have two
for
loops that getproject_documents
, you can try to remove one and re-check the result. On another hand, you can share the code that creates your custom fields, I will help you to correct the loop.October 26, 2021 at 2:21 PM #31517James
ParticipantHi, Long. Thanks for the reply.
I also tried that but still doesn't work. So basically the
upload_documents
is a group id for the first loop and the fields inside it are theproject_documents
. What I want to achieve is to see, if data ofproject_documents
is empty then do something if not then do this. Thanks in advance.Here's my custom fields setup:
<?php add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' ); function your_prefix_function_name( $meta_boxes ) { $prefix = ''; $meta_boxes[] = [ 'title' => __( 'Project Details', 'your-text-domain' ), 'id' => 'project-details', 'post_types' => ['project'], 'geo' => [ 'api_key' => 'AIzaSyCxo8eTReDw2Ehp9zYovWw4W9VRwEsD8Cs', ], 'fields' => [ [ 'id' => $prefix . 'custom_html_2avzqnhkuwl', 'type' => 'custom_html', 'std' => 'To upload images in this project, please go to the right sidebar and find the Project Image.', ], [ 'name' => __( 'Project Documents', 'your-text-domain' ), 'id' => $prefix . 'upload_documents', 'type' => 'group', 'collapsible' => true, 'save_state' => true, 'group_title' => '{project_documents}', 'clone' => true, 'sort_clone' => true, 'clone_as_multiple' => true, 'add_button' => __( 'Upload more', 'your-text-domain' ), 'before' => __( 'מסמכי הפרויקט', 'your-text-domain' ), 'fields' => [ [ 'name' => __( 'Add Thumbnail Icon', 'your-text-domain' ), 'id' => $prefix . 'document_icon', 'type' => 'single_image', ], [ 'name' => __( 'Add Document', 'your-text-domain' ), 'id' => $prefix . 'project_documents', 'type' => 'file_advanced', 'max_file_uploads' => 1, ], ], ], [ 'name' => __( 'Project Address', 'your-text-domain' ), 'id' => $prefix . 'project_address_bar', 'type' => 'text', 'before' => __( 'כתובת הפרויקט', 'your-text-domain' ), ], [ 'name' => __( 'Project Map', 'your-text-domain' ), 'id' => $prefix . 'project_map_group', 'type' => 'group', 'before' => __( 'מפת הפרויקט', 'your-text-domain' ), 'fields' => [ [ 'name' => __( 'Address Name', 'your-text-domain' ), 'id' => $prefix . 'address_name', 'type' => 'text', 'before' => __( 'כתובת', 'your-text-domain' ), ], [ 'name' => __( 'Map', 'your-text-domain' ), 'id' => $prefix . 'osm_map', 'type' => 'osm', 'address_field' => 'address_name', 'language' => 'en', 'region' => 'il', 'before' => __( 'מַפָּה', 'your-text-domain' ), ], ], ], ], ]; return $meta_boxes; }
October 26, 2021 at 10:30 PM #31525Long Nguyen
ModeratorHi,
If a clone of a group or a field is not added value, it will not save any data to the database so you needn't check the value of the file uploaded existed. Just show them
{% for clone in post.upload_documents %} {% for item in clone.project_documents %} {{ item.url }} <br> {% endfor %} {% endfor %}
October 27, 2021 at 2:53 AM #31532James
ParticipantHi, I don't have problems showing the item, my problem is how can I put an if statement inside a loop or condition if it is empty.
{% for clone in post.upload_documents %} {% for item in clone.project_documents %} {% if item.url is empty %} <h3>empty</h3> {% else %} <h3>{{ item.url }}</h3> {% endif %} {% endfor %} {% endfor %}
October 27, 2021 at 3:32 PM #31541Long Nguyen
ModeratorHi,
You can check if the field
project_documents
is empty if you add the value to the fielddocument_icon
before. If two fields are empty, the clone group will not be saved and just needn't check value exist.{% for clone in post.upload_documents %} {% if( clone.project_documents is empty ) %} Empty!!! {% endif %} {% for item in clone.project_documents %} {{ item.url }} <br> {% endfor %} {% endfor %}
-
AuthorPosts
- You must be logged in to reply to this topic.