How would I go about updating a Computed Field with every view?

I currently have a CCK content type ('user profile') that has Computed Fields. For example, I have a "First Name" field with the following code:

global $user
$node_field[0]['value'] = $user->$profile_firstname;

This works great. However, if the user changes the profile_firstname field in their profile, this field is not updated in the custom content type (unless I enter and do an edit/resave). Is there anyway to have this field updated automatically?

Comments

uw07’s picture

I tried the following code:

<?php
// This query is used to load all the nids of a specific content type into an array. userprofile is the custom content type
$query = 'SELECT nid
FROM userprofile';

// Create an array to store the query info
$profilenidarray = array();

//exectue the query using db abstraction stuff
$profilenidarray = db_query($query);

// For each nid this will load and then save the node
while ($nodeID = db_fetch_array($profilenidarray)){

//This time limit is important, if it is removed the query will exceed the 30sec limit.
set_time_limit(5);

$PNID = node_load($nodeID["nid"]);
node_save($PNID);

}
// Just so you know its done!
print 'done';
?>

But received: user warning: Table 'drupal.userprofile' doesn't exist query: SELECT nid FROM userprofile in /wwwroot/includes/database.mysql.inc on line 172.

I tried replacing 'userprofile' with 'book' and this worked. I don't understand because 'userprofile' is the name of my content type I created, but it can't find the table??

uw07’s picture

Anyone?

mooffie’s picture

You can store the UID in the field. This number doesn't change. Then, in the "display code", type the code to print this user's profile name.

uw07’s picture

I think I have this working. The downside is that the computed field doesn't actually store anything of value.

Ideally, the computed fields I have would be updated automatically through some sort of script (like the one above, which I still don't understand why it's not working...)

uw07’s picture

Ok, if I have the user's ID, then how do I go about accessing a field in their profile based on their id?

I have for the field value:

$global user;

$node_field[0]['value'] = $user->uid;

How would I display the user's name?

$display = ..... ????
uw07’s picture

For example...

I want to be able to go into the user's 'user profile' custom content types and submit, so as to compute and populate the fields. However, when I do this, it's using my userid. How can I use the id for the profile I'm editing?