Hi - I have a custom content type which is made up of over 50 computed_field module fields, along with normal text, number, and node_reference fields.
Within my node template, I not only use print statements like <?php print$node->field_customer_name[0]['view']; ?> to display the field data, but I also use other php statements to define other variables which show up on the template as well.
I'm trying to figure out how I can get the value from one of my variables into my node. I tried using computed_field to do this, but it doesn't seem to recognize that the variable is there, I think perhaps because it's not stored in the database. Is this something that the variable_get() function would do? Or could an insert statement work for this? Any help would be greatly appreciated.
Comments
I use ....
'value' where I use CCK fields...
maybe you are doing something slightly different to me but I don't see a 'view' version if I print out my page.tpl variables
Hi and thanks for your reply.
Hi and thanks for your reply. There are 'view' and 'value', I use both depending on what I want to show on the template.
But, the variables I am wanting to pull in are not in the database. They are temporary variables that I set only in the template like this example:
During a certain phase in this nodes workflow however, I'd like to be able to pull in the value of $calc_total_edgelf and insert its value into a phyiscal database field. I am unsure of how to tell Drupal to take the value of $calc_total_edgelf and put it in this field...
_
variable_get will work provided you've used variable_set to define the variable; it gets stored in the database, so should be available from pretty much anywhere.
Pete.
Ok I'd try...
$variables['node']->field_customer_name[0]['view']if that doesn't work I'd do the following at the top of your page.tpl to check exactly where your variables are
_
If the calculated value varies, you'd perhaps be better using a session variable -
Pete.
@pbarnett ok, thanks for all
@pbarnett
ok, thanks for all your input. So, instead of declaring my variable using this
You say I can define it using variable_set. The example shown here has {variable}...do I replace this with the name of my variable? in my example above I'd put $total?
And yes, the calculated value varies with each node.
Thanks!
_
Nooo!
That's the name of the table!!!
You set it using something like
and retrieve it with
That way, there's a different variable for each node. You may need several variables per node; just make sure the names are unique as variable_set just overwrites any pre-existing variable of that name.
The actual variable doesn't have to be a scalar, it can be an associative array or even more complex datatype; variable_set and variable_get handle all the necessary serialisation and recovery.
Pete.
Whoa! Glad I asked! I'm
Whoa! Glad I asked! I'm obviously new to trying php code on my own, (and clearly failing)!
This worked great by the way. The only thing that has me baffled is why the value doesn't retain the number formatting. It is rounding a number from $10,770.98 to $10,771.00. So I'll be playing around with that.
Thanks for your help!
_
Looks like it's saving it as an integer. Weird. I tried this -
and got the correct value back.
Are you sure you're saving the correct value?
BTW, if you're dealing with money, it's best to multiply by 100 and save as an integer value to avoid binary rounding errors; just divide by 100 on retrieval.
Pete.
Well, no, I'm not sure, I
Well, no, I'm not sure, I guess. I build with Drupal, but when it comes to wanting more custom functions, I suck! I wish I understood php better! :)
I had a computed field named total, so within my node template I had this:
Now, not being versed in php, I think your code says to populate my node->field_total with the value from my $total variable (they are seperate, btw) which I had set by use of this:
This initially spit out the right value, but was formatted incorrectly, as I stated earlier. So, I tried to correct it, which in turn screwed something up, because now, all it does is give me either a 10 or a 0. I either screwed it up (very likely) or since node->field_total was a computed field, when I resaved the node, it overrode the variable_set instructions.
Other questions I had when working with this: Does a field need to exist in the node for variable_set to populate? My guess is yes. Should it be a regular field vs. a computed field? or does it matter?
I've been reading about db_query and INSERT statements. Can something along those lines help me get my $total variable value into the database? or is variable_set and variable_get better? What's the difference?
I know you probably don't like dealing with noobs, but I do appreciate your time and patience with me! Thanks.
_
OK; best if you test this a bit at a time.
What I generally do to test code snippets is go to node/add/page, set the input format to PHP code, put my code snippet into the page and hit the preview button.
variable_set and variable_get are fine for saving and retrieving values; it's just your implementation that's not quite right.
Look, email me the node template and I'll have a glance at it.
Pete.