By chrispeat on
I am trying to update content_profile fields when I add a node, using rules.
Under the PHP evaluation section I have added some code, but when I look at the output I realise that the PHP Variables are not being loaded. How do I access rules PHP variables in the PHP evaluation section?
The code I am using is:
$result_user = db_query("SELECT uid FROM users WHERE name = '" . $user_added . "'");
print "SELECT uid FROM users WHERE name = '" . $user_added . "'";
while($data_user = db_fetch_array($result_user)) {
$users_id = $data_user['uid'];
}
$sql = "SELECT field_firstname_value, field_lastname_value, field_address1_value, field_address2_value, field_citytown_value, field_county_value, field_country1_value, field_postcode_value, field_tel_value FROM content_type_studentmembers WHERE nid = '" . $node . "'";
print "SELECT field_firstname_value, field_lastname_value, field_address1_value, field_address2_value, field_citytown_value, field_county_value, field_country1_value, field_postcode_value, field_tel_value FROM content_type_studentmembers WHERE nid = '" . $node . "'";
$result = db_query($sql);
while($data = db_fetch_array($result)) {
db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('1', '" . $users_id . "', '" . $data['field_firstname_value'] . "')");
db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('2', '" . $users_id . "', '" . $data['field_lastname_value'] . "')");
db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('3', '" . $users_id . "', '" . $data['field_address1_value'] . "')");
db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('4', '" . $users_id . "', '" . $data['field_address2_value'] . "')");
db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('5', '" . $users_id . "', '" . $data['field_citytown_value'] . "')");
db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('6', '" . $users_id . "', '" . $data['field_county_value'] . "')");
db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('7', '" . $users_id . "', '" . $data['field_country1_value'] . "')");
db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('8', '" . $users_id . "', '" . $data['field_postcode_value'] . "')");
db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('9', '" . $users_id . "', '" . $data['field_tel_value'] . "')");
}
Does anyone have any ideas, I cant find a forum post on this other than one saying to use TOKEN's but this causes an error. Help would be really great as I'm sure this is very close...
Thank you
Chris
Comments
Token update
I have managed to get around the issue with tokens, which turned out to work.
As an example this is how the code changed:
The query still remains though, how do you access the PHP variables, or cant you?
HTH Other people with the same query...
Chris
Chris, you can access node
Chris, you can access node PHP variables in Rules.
A snippet from your SQL in the first example says, "... WHERE nid = '".$node."' ...".
But $node is the whole node object. The node id is $node->nid.
There's a lot you can do with $node (and the other variables Rules can see as documented on the Rules forms themselves).
To see what's there you can create a text area field and populate it with a dump of $node using Rules:
$text = print_r($node, true);return array(0=>array('value'=>$text));If instead of "CCK:Populate a field" you choose the "PHP:Execute custom PHP" action then you can do a whole load of stuff in one place. You can change field values etc there by updating them in $node and then doing
return array("node" => $node);as documented above the code text area. It's really powerful!Specify the fields value with PHP code : Node referrence
Hello; I created a Triggered rule
Created content's field 'field_xxx' has value
I must set a value : OK, no problem
but I want to use : Advanced: Specify the fields value with PHP code
For example, this works:
but I must use a node referrence
$noderefnode = node_load($field_yyy[0][nid]);
$noderefnode->field_zzz[0]['nid']
How to integrate the code above with return array.... (the php code of the value...)
Any idea? Thanks :)
+ 1
+ 1
PHP Evaluation Variables don't work
Hello,
I'm having a problem wich I describe here
http://drupal.org/node/1455240#comment-5751152
and apparently I'm not the only one.
Since the subject seems very similar to the one discussed here, maybe you can help me.
Thanks
Cheers
^
Couldn't read/understand what you needed to do actually, but looks like you just need a dpm() (devel module) to get the vars available.
When a Rule is executed in a certain event, almost every variable is available to the View. On user insert, user's data are just available in given variables. I never needed to use database queries to grab these data.
and last: There is no good reason to use Rules and PHP together. You can either do it all in just php, or do it in Rules' Load user, Load node, and other given actions.
Sorry if my reply doesn't make sense or crappy.
What's new and changing in PHP 8.4