Custom Fields
Re-Envisioned
Support MB Views Math calculations in views ?

  • Creator
    Topic
  • #21815
    Resolved toni bird
    Participant

    Is it possible do do calculations in views? (front end) any documentations as starting point would be much appreciated .

    if not is there any work around?

    thanks

Viewing 10 replies - 1 through 10 (of 11 total)
  • Author
    Replies
  • #21820
    Long Nguyen
    Moderator

    Hi Toni,

    Do you mean to use the operator +-*/ in view? Or create a calculator to show on the frontend?

    #21825
    toni bird
    Participant

    Hi Long, thanks for your reply,

    I need to get number of days between 2 dates ( start and finish ) both values come from mb fields.
    I also need to add a couple of + and – operations based on other field values from this form.

    many thanks

    #21832
    Long Nguyen
    Moderator

    Hi,

    Of course, we can use operators to calculate the data (number) in View, like coding with PHP. Also, this article might help you to get the number of days between two dates https://stackoverflow.com/questions/30839618/how-to-get-days-difference-in-twig.

    #21834
    toni bird
    Participant

    Hi, many thanks,

     {% set difference = date(post.site_start|date('d/m/Y')).diff(date(post.site_finish|date('d/m/Y'))) %}
                  {% set leftDays = difference.days %}
                  {{leftDays}}

    Not sure what is wrong…. but is not working….

    got the add and multiplier functions working, but the dates are not working…. I just get a “0” as result.

    thanks

    #21836
    toni bird
    Participant

    Hi, Got it sorted, code

    {% set contract = date(post.site_finish  | date( 'Y/m/d') ).diff(date(post.site_start  | date( 'Y/m/d' ))) %}
              {% set contractDays = contract.days %}
              {% if contractDays == 1 %}
               1 day
              {% else %}
           {{ contractDays }} days
          {% endif %} 

    cheers

    #21894
    toni bird
    Participant

    Hi, not sure if I should open another topic, but it is still on the same matter…. I have a report system that my client creates a report on daily basis. client fills a custom fields form and when he publishes a post on views he gets a report page with a sum from values added to the fields. next day he duplicates the post ( post duplicator plugin) and just change the date and add new values to the fields.. My question is. how do I store the sum of values from yesterday’s post (previous post) and sum with the new values from today’s post ? and so on …. is it possible in views to do that? any work around?

    many thanks

    #21897
    Long Nguyen
    Moderator

    Hi,

    The filter rwmb_{$field_type}_value or rwmb_{$field_id}_value can help you to sum old value and new value of a field type or field id. Add this sample code in the file functions.php or use Code Snippets.

    add_filter( 'rwmb_fieldID_value', function( $new, $field, $old ) {
        $new_value = $new + $old;
        return $new_value;
    }, 10, 3 );
    

    For more information, please follow the documentation https://docs.metabox.io/filters/#rwmb_field_type_value.

    #21907
    toni bird
    Participant

    Hi Long, thanks for your help again.

    cheers

    #22008
    toni bird
    Participant

    Hi again Long,

    Sorry for being a pain, but I don’t know how to call the filter in views. I have added the code into my theme’s functions.php file, but not sure how to call the function in views .
    I’ve tried {% set value = mb.rwmb_soma_staff_d_value %} but nothing happens. is this correct?

    thanks

    #22014
    Long Nguyen
    Moderator

    Hi Toni,

    The function in the file functions.php should return something to use in View. Such as:

    function return_a_value() {
        return 'testing';
    }

    Then use in View via proxy {% set value = mb.return_a_value() %}.

Viewing 10 replies - 1 through 10 (of 11 total)
  • You must be logged in to reply to this topic.