3

I'm playing around with views, and I am sure on how to calculate fields based on Views data.

I thought Views Calc would do this, but it seems to only give me sums and averages of the fields. For example, suppose I'm making a financial site, and I want to do a price to earnings ratio; I would take price and divide it by earnings. Is it possible to do this in Drupal?

I know I can do this creating a PHP file for page, but to complicate things more, I ultimately want to give users the ability to do this as well; so, if possible, I want to stick with doing this with Views.

I see the view queries in generating; is there a way to modify the queries myself?

1 Answer 1

7

There is two ways to do this:

  1. If your column information resides in a node, you can use Computed Fields in your node to create some new CCK fields with all sort of operations inside (you code directly in php there with access to all others fields of the node. If you're adventurous enough you can also make queries and everything you can imagine.
  2. There's a similar funcionality directly for Views, it's called Views Custom Field and it's basically a Computed Field for Views.

So to summarize, if you need to have this information appearing in a node then Computed Field, if not just use Views Custom Field.

7
  • I might need both. Some results I would want to put in a blog or pages, while most of them will be in standard views and applied to the entire dataset.
    – Lostsoul
    May 18, 2011 at 21:16
  • Also, while using Computed Fields, don't forget to check the option "save field in database" if not it won't be available as a field in Views.
    – tostinni
    May 18, 2011 at 21:36
  • ok I'll try that. I'll def need both. Right now I am playing around with views custom field and its really amazing..looks like it'll get me going on what I want to do. I'm having a problem giving it custom sql commands because not sure how to id external tables but i'm working on it. Its really amazing..thanks again
    – Lostsoul
    May 18, 2011 at 22:21
  • To use external tables, have a look at this documentation How to connect to multiple databases within Drupal, but if you're writing custom SQL, you can juste make queries like this: SELECT columns... FROM database2.table1... granted your MySQL Drupal user have access to this db.
    – tostinni
    May 18, 2011 at 22:29
  • wow it works amazing. I can finally do what I want to do with views. Thank you so much. eventually i'll want to allow users to create views like this..but somehow i suspect giving them access to query my database and execute php commands on my server is a bad idea. That'll be another day. I am really really thankful for your help Tostinni.
    – Lostsoul
    May 19, 2011 at 0:13

Your Answer

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

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