Skip Navigation

[Resolved] Saving the number of iterations in a loop

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by Minesh 8 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2700367

Tell us what you are trying to do?

Finishing up changes related to: https://toolset.com/forums/topic/count-items-in-using-a-taxonomy-on-a-related-post/

I've got a nested view set up for one of the taxonomy terms, and I used the code snippet for loops from here to get it to show iteration number in each loop item.

https://toolset.com/documentation/programmer-reference/adding-custom-code/count-the-number-of-views-loop-iterations/

Now - two questions

1. How do I display only the final iteration number after the view has completed running? I'm thinking I need to save the value in the shortcode variable to a displayable variable, true?

2. Do I need to create separate nested views for each of the taxonomy terms in the Trails Taxonomy?

Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/adding-custom-code/count-the-number-of-views-loop-iterations/

Is there a similar example that we can see?

What is the link to your site?
hidden link

#2700614

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

As per our support policy - we entertain only one question per ticket. This will help users searching on the forum as well as help us to write correct problem resolution summery.

I will take and try to address your first question:

1. How do I display only the final iteration number after the view has completed running? I'm thinking I need to save the value in the shortcode variable to a displayable variable, true?
====>
Can you please share link of the view you are using?

Do you only want to display the last iteration of the view's loop?

Can you please share problem URL where exactly you added your view?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2701529

Minesh
Supporter

Languages: English (English )

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

As I understand you want to display the total posts count belongs to the winery.

If this is true - I've just added the following shortcode within the "Loop Editor" section:

WNC Iterations: [wpv-found-count]

Is this the result you were expected?

#2701759

No - I got the counter part working.

On the Accounts page (like this one - hidden link) I want to know how many wineries the person has visited that are in each Trail category.

So at the bottom of the page (see the static text I've added):

Trail Counters: this is the one trail I've set up to count:
WNC Cheese Trail: NUMBER OF WINERIES IN THE WNC CHEESE TRAIL VISITED (VARIABLE OF SOME SORT?)
But ultimately, I want this:
Trail Counters
Catawba Valley Wine Trail - number of wineries I've visited in this trail
Chatham County Craft Beverage Trail - number of wineries I've visited in this trail
Cheers Trail - number of wineries I've visited in this trail
Haw River Valley Wine Trail - number of wineries I've visited in this trail
High Country Wine Trail - number of wineries I've visited in this trail
JoCo Beer, Wine and Shine Trail - number of wineries I've visited in this trail
NC Cider Trail - number of wineries I've visited in this trail
NC Mead Trail - number of wineries I've visited in this trail
Sandhills Wine Trail Passport - number of wineries I've visited in this trail
Shallow Ford Wine Trail - number of wineries I've visited in this trail
Southern Gateway Wine Trail - number of wineries I've visited in this trail
Surry County Wine Trail - number of wineries I've visited in this trail
Swan Creek Wine Trail - number of wineries I've visited in this trail
Triangle Mead Trail - number of wineries I've visited in this trail
Uwharrie Mountains Wine Trail - number of wineries I've visited in this trail
WNC Cheese Trail - number of wineries I've visited in this trail

It feels to me like I need to create a counter for each trail, and then a variable for each trail to display here.

#2701845

Minesh
Supporter

Languages: English (English )

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

Ok but the question is - using what variable or how to know that what wineries are visited for what trail - how to track that?

How do you want to count or how can we identify that For X Trail the X wineries is visited by X user?

#2701917

I've got a counter set up for the one Trail - WNC Cheese Trail - per the previous emails.

Or at least I did before you made the change that just shows "1" on each iteration.

As I stated in the original question, I followed the instructions here to create a loop iteration counter:
https://toolset.com/documentation/programmer-reference/adding-custom-code/count-the-number-of-views-loop-iterations/

I set it in the custom code here: hidden link

I put that shortcode that I originally had back in the view appearing here: hidden link
Look for the "LTS:".

The first time WNC Cheese Trail shows up, LTS = 1
The second time WNC Cheese Trail shows up, LTS = 2

I want to create a variable that holds the last number from the iteration (in this case, 2) and be able to show it at the bottom of the page - in this case "2" would show for the "number of wineries I've visited in this trail" for WNC Cheese trail.

It feels to me like I need a variable so I can pass it out of the inner loop of the view to the outer loop and display it.

#2702040

Minesh
Supporter

Languages: English (English )

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

The code you are using to count the loop iteration is for single view - its not for the nested view which is actually your requirement where you have nested view and you want to count the number of iterations.

Can you please check now: hidden link

I've added the following shortcode to "Custom Code" section at bottom of the existing code snippet you have added:

add_shortcode('tss_getcount','func_tss_get_count');
function func_tss_get_count($atts,$content) {
static $count = 0;
      
if($atts['value']=='reset'){
            $count = 0;
 }else if($atts['value']=='add'){
           $count = $count+1;
      }else if($atts['value']=='total'){
           return  $count;
     } 

} 

To the parent level view:
- hidden link
I've added the shortcodde to display the total loop count:

<div> Total Count: [tss_getcount value="total"] [tss_getcount value="reset"] </div>

To the child view - I've added the following shortcode:
- hidden link

LTS:  [tss_getcount value='add'] [tss_getcount value="total"]

Can you confirm it works as expected now.

#2702059

Getting there.

I need that value (and similar values for the other 15 wine trails) to show up at the bottom - where I currently have the "Trail Counters" section.

I need a count for each of those. So the question is - do I need to do 16 separate nested views to do this? And how to I keep track of what number goes with which trail?

This is why I was asking about a variable, because it looks like the loop can just count one thing, not 16 discrete things.

I was thinking to pull the 16 variables (created based on the tss_getcount shortcode) and show them in the output of the "My Winery Visits - with Trail Counter" portion.

Or am I going about this the wrong way?

#2702216

Minesh
Supporter

Languages: English (English )

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

Can you please share a page where you list all those 16 wine trails and once I see how you setup those trails to display it I will be able to guide you in the right direction.

Can you please create a test page where you list all those 16 wine trails as per your actual requirement and based on that I will try to build the solution.

#2702258

Sure. The page is the one that I've been working on is the best example. I've cleaned it up a bit to make it clearer what I'm going for.

On this page: hidden link

The third column lists the wine trails associated with the wineries that I've visited.

At the bottom, I want to show the number of wineries associated with each Trail that I've visited.

So - for example, in the table at the top, you see that the first winery is on the "Swan Creek Trail". Since none of the other wineries I've visited are on the Swan Creek Trail (by looking at the rest of the list), the number at the bottom of the page associate with the Swan Creek Trail would show 1.

If I add a new visit, and the winery is on the Swan Creek Trail, once that visit is published, the number at the bottom for Swan Creek Trail would increase to 2.

Similarly, for the one trail I've been using to try to get this to work (WNC Cheese Trail), you'll see that the fourth item in the list (Pleb Urban Winery) and the sixth item (Barn Door Ciderworks) are on the WNC Cheese Trail winery. Therefore, at the bottom in the list, the number associated with WNC Cheese trail would be 2.

I've made copies of the three Visit views as they stand (the three starting with BB) and made a backup of the site, so you can play with the "My Visit" views as you like.

I appreciate the help with this!

#2702384

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've added the following shortcode to "Custom Code" section:

add_shortcode('tss_getcount','func_tss_get_count');
function func_tss_get_count($atts,$content) {

if($atts['action']=='reset'){
            $_SESSION['winery_count'] = 0;
 }else if($atts['action']=='add'){

           $assigned_wineries = explode(",",$atts['winery']);
    

           if(!empty($assigned_wineries)) {

                       foreach($assigned_wineries as $k=>$v):

                                     if(!isset($_SESSION['winery_count'][$v])) {
                                             $_SESSION['winery_count'][$v] = 1;
                                               
                                      }else{
                                                 $_SESSION['winery_count'][$v]  =  $_SESSION['winery_count'][$v] + 1;
                                      }
                                     
                          endforeach;
 
            }

          
      }else if($atts['action']=='showcount' and is_array($_SESSION['winery_count']) ){
               
               $winery_name = $atts['winery']; 
              

              if(isset($_SESSION['winery_count'][$winery_name])) {
                         return $_SESSION['winery_count'][$winery_name];
              }else{ 
                      return 0;
              }
     } 

} 

I've added the following taxonomy view and added the shortcode [tss_getcount action="showcount" winery="[wpv-taxonomy-title]"] to display the count related to each taxonomy term.
- hidden link

<ul>
   <wpv-loop>
     <li> [wpv-taxonomy-link]: Count: [tss_getcount action="showcount" winery="[wpv-taxonomy-title]"] 
</li>
</wpv-loop>
</ul>

Added the above taxonomy view to display the count to your following view:
- hidden link

<h2>Trail Counters - view</h2>
[wpv-view name="tax-view-show-all-winery"]

It was a tricky one but glad that I'm able to share the solution. I hope you are happy enough now to see the dynamic solution.

#2702416

Fantastic!!!! I'm dancing around my office. This one has been bugging me for way to long. I so appreciate your help and willingness to see it through to a resolution!!!