I feel like this is a very basic question, but after a couple of years it is the first time I need something like this and I cannot wrap my mind around the structure and get an idea of how to acomplish it.

Lets say I have a content type called "Fruit" where I store data in the fields "Name" (text), "Description" (text), "Price" (integer), "Date" (date). In a view I'd like to select the fruit with names "Pear" and "Apple" with their respective prices for the dates inserted, but also make mathematical operations on the prices of the fruit (Like, "The price of apples was the 3/4 of the price of pears"). I cannot think of a way to do this since it is actually the same field, only grouped by some other field.

Any suggestions?

  • a proper answer really depends on where you want to do the calculations, and what you want to do with the results
    – Geoff
    Commented Jun 28, 2015 at 14:52
  • I want to do the calculations per result, like "Divide the prices of the apples with the prices of pears on every given date" and display a graph with the results. The graph part is covered, I am looking for a way to get the result as an integer. Commented Jun 28, 2015 at 15:19

2 Answers 2


Install views_php module. This module adds a field to the view and it also provides a few tokens. You can use the current row values. Note that you have to insert the main fields before the PHP field and exclude the Price field from display, Then the $results,$data,$row objects will give you the value of the row, Do the mathematical calculations and output the result.

  • This is an interesting prespective but since the PHP field is part of a row, how is this going to allow me to do calculations with the field of another row? Lets say I filter the fruit by the latest price of Pear and Apple. It will give me two rows with something like: "Pear", "2", "28/6/2015" "Apple", "3", "28/6/2015" How am I going to divide the price of apples with the price of pears "3 / 2" for example? Commented Jun 28, 2015 at 15:12
  • The $row object returns the value of the current row but $results object returns the entire value of the view. Install Devel module and use kpr($results) and kpr($row) too see the available objects. But be sure it works
    – M a m a D
    Commented Jun 28, 2015 at 15:34

If you want to perform mathematical operations, then you have to create view in normal way and in the view query alter functions, you have to append your condition, then you will get your expected output.

  • mathematical calculations between field elements are not done as part of the sql query, adding a condition would do nothing, that would only restrict what values are returned
    – Geoff
    Commented Jun 28, 2015 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.