Skip Navigation

[Resolved] Display number of posts in a view based on conditions

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to display a summary of counts and scores for a table view based on some conditions.

Solution:
This is easily done through a custom Javascript code. Check this reply:
- https://toolset.com/forums/topic/display-number-of-posts-in-a-view-based-on-conditions/#post-1908067

This support ticket is created 3 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+00:00)

This topic contains 13 replies, has 3 voices.

Last updated by Rune Brynestad 3 years, 3 months ago.

Assisted by: Jamal.

Author
Posts
#1903045

I have created a sinple table view here:
hidden link

On the bottom of the page I want to display some calculated output.
Number of posts is displayed with the shortcode [wpv-found-count]
Is it possible to display number of posts based on condition in the columns Branninstruks, Rømningsveier, Slokkerutstyr and Møteplass?
(See bottom of the page for the conditions I want to display)

Thanks in advance.

Kind regards
Rune

#1903619

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

There is no such option available to count the specific values in specific columns but you may do it by adding custom jQuery code.

For example:
For the first row - td having the Ja value is 4 and the count should return 4.

count = jQuery(".js-wpv-view-layout table tr:eq(1) td:contains(Ja)").length;

You will require to adjust/add the required JS code accordingly.

#1904247

Hi Minesh

Thanks for jumping in.

Your custom jQuery code count content that contains in a row. I need to count the content that contains in a column. In my test page, the numbers should be like this:

Number of posts with Branninnstruks = "Ja": 6
Number of posts with Rømningsveier = "Ja": 7
Number of posts with Slokerutstyr = "Ja": 7
Number of posts with Møteplass = "Ja": 7

Is it possible with columns and custom jQuery code count ?

Thanks again for looking into this.

Kind regards
Rune

#1904657

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should try to add the following code to your view's JS box:

jQuery(document).ready(function($){
counter = function(textMatch,index){
    count=0;
    jQuery('.js-wpv-view-layout').find('tbody tr').each(function( index ) {
        //if second <td> contains matching text update counter
        if(jQuery(jQuery(this).find('td')[index]).text() == textMatch){
            count++
        }
    });
    return count;
}

jQuery(".branninnstruks .count").html(counter("Ja",2));
jQuery(".branninnstruks .count").html(counter("Ja",3));
jQuery(".branninnstruks .count").html(counter("Ja",4));
jQuery(".branninnstruks .count").html(counter("Ja",5));

});

You should add your lines where you added the static lines within your Loop Editor as given under:

<div class="branninnstruks"> Number of posts with Branninnstruks = "Ja": <div class="count"> 0 </div> </div>
<div class="romningsveier"> Number of posts with Rømningsveier = "Ja": <div class="count"> 0 </div> </div>
<div class="slokerutstyr"> Number of posts with Slokerutstyr = "Ja": <div class="count"> 0 </div> </div>
<div class="Moteplass"> Number of posts with Møteplass = "Ja": <div class="count"> 0 </div> </div>

#1905779

Hi Minesh

Thanks for your suggestion, but I'm afraid it did not work quite as expected.

It's counting the first row only, not the columns "Branninnstruks" , "Rømningsveier", "Slokkerutstyr" and "Møteplass".

Please check the output:
hidden link

Thanks

Kind regards
Rune

#1906693

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello Rune, Minesh is on a vacation for a couple of days. If you don't mind I'll continue with you on this thread.

I would suggest that you add a special class to each column, then you can use jQuery to count the instances. Check this screenshot, where I added a class "bran" in the browser dev tools to the columns "Branninstruks" and how it calculated the instances with the following code hidden link

jQuery(".js-wpv-view-layout table .bran:contains(Ja)").length;

Does it make sense to you? If not, please allow me temporary access to the admin area and I'll prepare an example for you. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1907611

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

I made a slight change to the class names by removing the (;) at the end. Then I updated the Javascript code with the following:

jQuery(document).ready(function($){
  $('div.bran .count').html($(".js-wpv-view-layout table .bran:contains(Ja)").length);
  $('div.romn .count').html($(".js-wpv-view-layout table .romn:contains(Ja)").length);
  $('div.slok .count').html($(".js-wpv-view-layout table .slok:contains(Ja)").length);
  $('div.mote .count').html($(".js-wpv-view-layout table .mote:contains(Ja)").length);
});

And it worked. Check this screenshot hidden link

All the best,
Jamal

#1907633

Thanks Jamal

This is perfect 🙂

Is it possible to have the values and labels on the same line?

Number of posts with Branninnstruks = "Ja": 6
insteead of
Number of posts with Branninnstruks = "Ja":
6

Thanks again for looking into this.

Kind regards
Rune

#1907957

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Sure Rune,

The counters are put inside a <div> tag which is by default displayed as a block(in its own row or line), I would use an inline tag such as <span> tags. hidden link

I changed it on the view.

#1908035

Perfect 🙂

I also wonder if it's possible to do som math with this script.

I was thinking about calculating a score like this:

Number of posts with Branninnstruks = "Ja" / Number of posts * 100
which should be 6 / 7 * 100 = 85,71 (rounded up to 86, no decimals)

Number of posts with Rømningsveier = "Ja" / Number of posts * 100
which should be 7 / 7 * 100 = 100

and so on

Kind regards
Rune

#1908041

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Would you like to display the counts and the scores or only the scores? Where do you want to display them? Can you add an example of how it should be?

In the meantime, I would like to explain the following:
- The count can be done using $(".js-wpv-view-layout table .bran:contains(Ja)").length.
- The total number can be done using $(".js-wpv-view-layout table .bran").length.
- The calculation and getting 2 decimals after the comma can be done using this ( (count / total) *100 ).toFixed(2)

Let me know how do you want to display the results(count and/or score) and I'll help with it.

#1908045

I would like to display the score under the existing counts like this:
hidden link

It is not necessary to show the end user how scores are calculated. Just the label and the value.

Thanks again.

Kind regards
Rune

#1908067

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello Rune, I built this too using the following code(updated):

jQuery(document).ready(function($){
  var bran = $(".js-wpv-view-layout table .bran:contains(Ja)").length;
  $('div.bran .count').html( bran );
  $('div.bran .score').html( ( ( bran / $(".js-wpv-view-layout table .bran").length ) * 100 ).toFixed(2) );
  
  var romn = $(".js-wpv-view-layout table .romn:contains(Ja)").length;
  $('div.romn .count').html(romn);
  $('div.romn .score').html( ((romn / $(".js-wpv-view-layout table .romn").length ) *100 ).toFixed(2) );
  
  var slok = $(".js-wpv-view-layout table .slok:contains(Ja)").length;
  $('div.slok .count').html(slok);
  $('div.slok .score').html( ((slok / $(".js-wpv-view-layout table .slok").length ) *100 ).toFixed(2) );
  
  var mote = $(".js-wpv-view-layout table .mote:contains(Ja)").length;
  $('div.mote .count').html(mote);
  $('div.mote .score').html( ((mote / $(".js-wpv-view-layout table .mote").length ) *100 ).toFixed(2) );
});

Best regards,
Jamal

#1908997

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.