Math calculations in views ?
- This topic has 11 replies, 2 voices, and was last updated 4 years, 7 months ago by
toni bird.
-
AuthorPosts
-
September 10, 2020 at 10:34 PM #21815
toni bird
ParticipantIs 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
September 11, 2020 at 9:00 AM #21820Long Nguyen
ModeratorHi Toni,
Do you mean to use the operator +-*/ in view? Or create a calculator to show on the frontend?
September 11, 2020 at 8:05 PM #21825toni bird
ParticipantHi 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
September 11, 2020 at 11:23 PM #21832Long Nguyen
ModeratorHi,
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.
September 12, 2020 at 3:34 AM #21834toni bird
ParticipantHi, 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
September 12, 2020 at 10:13 AM #21836toni bird
ParticipantHi, 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
September 16, 2020 at 12:08 AM #21894toni bird
ParticipantHi, 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
September 16, 2020 at 9:06 AM #21897Long Nguyen
ModeratorHi,
The filter
rwmb_{$field_type}_value
orrwmb_{$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.
September 16, 2020 at 8:53 PM #21907toni bird
ParticipantHi Long, thanks for your help again.
cheers
September 21, 2020 at 8:44 PM #22008toni bird
ParticipantHi 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
September 22, 2020 at 8:50 AM #22014Long Nguyen
ModeratorHi 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() %}
.September 22, 2020 at 10:13 PM #22032toni bird
ParticipantHi Long, thanks a lot, got it sorted.
cheers
-
AuthorPosts
- You must be logged in to reply to this topic.