I run a website for a neighborhood association and am trying to build something to track our events in particular the attendance, expenses and just some general notes on how the event went. I'm working through the expense reporting right now and here is what I have so far.
A small content type (we'll call it expense) where users enter their expenses with the following fields:
Title: Name of the expense
Decimal: Cost of item
Taxonomy Term: Name of event expense is associated with
I have another content type (we'll call it Event Log) that brings together these expenses with node reference.
I have the Taxonomy Search mod installed so within the node reference you can only display expenses for a specific category. However, as users enter their expenses into expense, I need to go into Expense Log and select these additional expenses from the multi list box.
What I'm hoping a few of you wonderful people can offer me:
1. Am I approaching this the best way possible? Does anyone else have any suggestions for a simple content type for collecting and displaying these expenses?
2. If this is the best way, how can I get Event Log to automatically include the new expenses as users submit them?
3. Since I'm not a PHP coder at all, I sure would appreciate some help putting together the computed code to total these expenses from the node reference.
Thank you for your consideration.
Comments
Calculate total of node reference
I sure would like a response to this so I can finish this content type. At this point I would be happy if I could just get an answer to #3:
If someone could provide me an example of the computed code required for the calculate field to sum figures from a node reference I could call this completed even though I'm not 100% happy with how I'm bringing in the expenses to the Event Log.
To summarize, I have my expenses in their own nodes. The expense is put into a decimal CCK field and then pulled into Event Log via node reference. Can I sum them with the calculated field in my Event Log and if so, how?
Thank you for your help!
Possible computed field code
With the help of my husband who programs in C#, but has no experience with PHP, we came up with the following for my computed field:
Unfortunately nothing happened. I didn't get a total in my Event_Log node, but I didn't get any errors either. Can anyone help me out?
I have the display set to the default of
$display = $node_field_item['value'];...
There might be utility modules to ease this. A quick search reveals 1 2 3, but I don't know how functional they are.
I don't know.
Perhaps you can use ubercart for this.
I read in another post you're going to try views_calc. FWIW, here's a fix to the code:
Note that we load each 'expense', into $item, and then access the 'cost of item' field.
I appreciate the help!
I decided not to play with this mod right now as I was hoping to keep this simple and be able to add some numbers within the node and maybe start to grasp a little PHP along the way (yeah... right!).
Thank you for the code snippet, but it's still not kicking out the results of adding together the numbers in field_expense. I'm not a coder, but it seems like it should work. field_expense is the right name of the nodereference and the actual number (decimal) lives in a field I named field_cost so I simply changed your field_cost_of_item to field_cost.
I looked at the code in contemplate for field_total and here it is in case that would be helpful:
I also tried unchecking the store in database checkbox to see if that would display a result (or even an error!) and nothing.
Thank you for your help!
...
The code works for me. (FWIW, I configured he Computed Field not to store the value in the DB.)
But you don't really need to use Computed Field. You can embed the code directly in your contemplate snippet.
Replace the code you just showed me with:
If it doesn't work, then let's first try to "x-ray" the
$node->field_expensevariable to see what it contains:(Paste the result here.)
Next we can x-ray all the expense nodes:
(Paste the result here.)
--
Help save BLOCKQUOTE
It works!
I got it to work!
I went into Event_Log and simply reselected my expenses and saved and then it worked. Basically it needed a "refresh" of sorts. Thanks again for these code snippets. Hopefully they will help others in the future.
On a related topic, are there any PHP tutorials that you could recommend to me, specially anything that ties into Drupal? Anything out there that might have put me on the right path to coming up with this code on my own? I'm finding my lack of any coding ability is what is hurting me the most on building this website.
...
I'm glad it works.
Yeah, that's why I configured the Computed Field to not save the value in the DB. By not storing it you solve the "refresh" problem --and it has the added benefit that if you modify the cost of some expense the total stays correct.
I don't know of specific ones.
Yes, it's very useful to have some coding ability.
A lot of people come here with little or no PHP knowledge, and just by being exposed to the code snippets here, little by little, they learn quite a lot. You already learned how to loop, load a node, sum variables......
If you're a total beginner to programming then you don't have to look for tutorials that specifically use PHP. Programming is programming, and it can be tough in almost any language. Surf the web till you find something you covet. But don't put Drupal/PHP on hold: just use it; learning will occur by itself.
--
Help save BLOCKQUOTE
Contemplate
This may be a dumb question, but if you change the HTML around the PHP in contemplate, should that affect the computed field? I did just that and it's not printing the total now. Here is contemplate for both the teaser and body for 'expense':
And here it is for 'event_log' for just the total:
Viewing the generated HTML for 'event_log' reveals nothing was printed for the total field (ie, there is nothing between the span tags). Any ideas why this is happening if I didn't touch the PHP tags?
...
You're right, it won't work this way. I've just investigated this and filed a bug report against ComputedField,
It turns out you need to write it thus:
(That is, write 'value' instead of 'view'.)
The moral of the story is: when something doesn't work, don't blame yourself. Blame others! :-)
--
Help save BLOCKQUOTE