Skip Navigation

[Resolved] Assistance with modifying custom code to handle calculations.

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 8 replies, has 3 voices.

Last updated by Minesh 1 year, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2643343

We have custom code to handle calculations as follows:

// Put the code of your snippet below this comment.

add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts,$content=null) {
$content = wpv_do_shortcode($content);
$content = eval("return $content;");
return round($content);
}

The Total Points field is showing "NaN" on all the teams that made the playoff rounds in that tournament. The playoff matches included ones that did not have games for Men's Doubles, Women's Doubles, Mixed Doubles A, and Mixed Doubles B, which is what the calculation field is based on. Those playoff matches only used the TeamMaker points system and those do not get added to the Total Points calculation.

We have the code to execute the calculation in a view as follows;

<td class="score-[wpv-post-id item='@team-1-match.parent']">[wpv-conditional if="( $(wpcf-team-1-score-mens-doubles) gt '0' )"][wpv-calculate][types field="team-1-score-mens-doubles"][/types]+[types field="team-1-score-womens-doubles"][/types]+[types field="team-1-score-mixed-doubles-1"][/types]+[types field="team-1-score-mixed-doubles-2"][/types] [/wpv-calculate][/wpv-conditional]</td>

See how we're calculating the sum of scores in four different game types within a match - men's doubles, women's doubles, mixed doubles 2 and mixed doubles 2. This was working fine previously.

But subsequently, we've added another game called TeamMaker. Most games do not use the TeamMaker fields and those are still working fine. But we have some playoff matches that ONLY use the TeamMaker fields. On those matches, the men's, women's, mixed 1 and mixed 2 score fields remain empty. The TeamMaker results are treated kind of like penalty kick shootout goals in soccer. They don't get added to the Total Points. So the calculation is getting thrown off by the presence of matches that do not include the men's, women's, mixed 1 and mixed 2 score fields. Whenever the calculation encounters matches with those fields empty, it is returning output of "NaN" on the page here:

hidden link

I'm not sure what I need to modify in the calculation code to prevent this result of "NaN". Note that it is also possible that the problem does not reside in the calculation code. This Standings by Session page has custom Javascript that Luo Yang set up for us that puts all the match results in the format needed on the page. It is possible that the Javascript is where this "NaN" output is coming from. I'm not sure there.

Any assistance here will be greatly appreciated.

#2643973

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I've adjusted the custom Javascript code shared by Luo on the following page as given under:
=> hidden link

jQuery( document ).ready(function($) {
    $('.total-score').each(function( index ) {
      var teamID = $(this).attr('team-id');
      var totalScore = 0;   
      $( ".score-" + teamID ).each( function(){
       if($(this).text() != '') { 
           totalScore += parseFloat($(this).text());
       }
      });
      $(this).text(totalScore);
    });

    $('.total-win').each(function( index ) {
      var teamID = $(this).attr('team-id');
      var totalWin = $( ".Win.result-" + teamID ).length;
      $(this).text(totalWin);
    });
   
    $('.total-loss').each(function( index ) {
      var teamID = $(this).attr('team-id');
      var totalLoss = $( ".Loss.result-" + teamID ).length;
      $(this).text(totalLoss);
    });
     
    //tablesorter
    $(function() {
      $(".tablesorter").tablesorter({ sortList: [[1,1], [2,1]] });
    });
});

Where as you can see I've wrapped the line of code with if condition as given under for Total Points (totalScore ) column:

if($(this).text() != '') { 
           totalScore += parseFloat($(this).text());
       }

Can you please confirm its working as expected now:
- hidden link

#2644129

No more "NaN"s, so it looks good. I'll have to double check that the math is correct for all teams on the total points column, but I'd assume it is.

I'm curious as to what specifically caused the "NaN" results here, just so I'll know in case I see that again any time.

#2644247

Minesh
Supporter

Languages: English (English )

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

I already explain what was the cause with my previous reply and what line of code was the issue.

Following line of code was the issue:

 totalScore += parseFloat($(this).text());

Which I wrapped with the if condition as given under (which you can see with my previous reply as well):

if($(this).text() != '') { 
           totalScore += parseFloat($(this).text());
       }

We are checking if there is no number available to add then it should not try to add that NaN string.

#2644421

Sorry I didn't understand from the first explanation. I understand that you're doing a check on if there is no number available. I just don't know where "NaN" comes from. I've never seen that before and was curious as to what causes that to be the result.

Separate question that IMO doesn't rise to the level of needing a ticket: In what you have been doing with these tickets of the last few days, did you change anything on the Match template that would cause this:

hidden link

See how the Points column has the totals wrapped in ++1++ and ++2++ ?

I didn't want to fix that just in case it was something you needed with getting the wins and losses tallies correct in the other ticket. But I wanted to ask here if that's something you did just to be sure. If you didn't do it, or if you don't need it that way an y more, I'll go ahead and fix it now. But if you still need it that way, I'll leave it alone until you say otherwise.

#2644731

Minesh
Supporter

Languages: English (English )

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

It seems you already fixed the issue. I see the Point column displays just fine at this end.

#2645197

Yeah, I had to go ahead and fix it Friday night because they were having their tournament Saturday morning. But then their tournament got rained out, so it didn't matter either way. If you need to put those placeholder text bits back in there, feel free to do so as needed.

#2645341

Thank you for sharing these details.

Just wanted to let you know that Minesh is on holiday today.

He'll be back tomorrow and will be able to follow up on this ticket, accordingly.

Thank you for your patience.

#2645513

Minesh
Supporter

Languages: English (English )

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

I'm not sure where you made the change and if its already fix then you are welcome to mark resolve this ticket. You can raise a new ticket if you still have any issues in future.

#2645639

I fixed it in the Match content template and I think in the view that creates the Results page. All good with this one. Feel free to add anything you need with working through the other ticket addressing the wins and losses. We no longer have an immediate need for anything to be correct. It was only on Saturday that I needed everything to display correctly on all the pages other than the Standings one we're still working on.