0

I want to keep a progress field in my content type so i can update value in it. But it doesn't take the previous value if saved in for loop.

for($i = 1; $i<=5; $i++) {
    $mynode = node_load(6);
    $myprevious = $mynode->field_progress[0]['value'];
    drupal_set_message("My Previous".$myprevious);
    $mynode->field_progress[0]['value'] = $myprevious." ".$i." ";
    node_submit($mynode);
    node_save($mynode);
}
$mynode = node_load(6);
drupal_set_message("My Value".$mynode->field_progress[0]['value']);
$myprevous_progress = $mynode->field_progress[0]['value'];
$mynode->field_progress[0]['value'] = $myprevous_progress.' end.'.$myprevous_progress;
node_save($mynode);

Output on page :

My Previous
My Previous
My Previous
My Previous
My Previous
My Value
2
  • are you on 6 or 7? Feb 2, 2013 at 7:11
  • @MohammedShameem 6. but i solved it check my answer Feb 2, 2013 at 7:12

1 Answer 1

0

Solved It by using

$mynode = node_load(6); //Load the node before for loop
for($i = 1; $i<=5; $i++) {
    $myprevious = $mynode->field_progress[0]['value'];
    drupal_set_message("My Previous".$myprevious);
    $mynode->field_progress[0]['value'] = $myprevious." ".$i." ";
    node_submit($mynode);
    node_save($mynode);
}
drupal_set_message("My Value".$mynode->field_progress[0]['value']);
$myprevous_progress = $mynode->field_progress[0]['value'];
$mynode->field_progress[0]['value'] = $myprevous_progress.' end.'.$myprevous_progress;
node_save($mynode);
2
  • I really can't see what/why you are doing/trying to accomplish here, but, if a node is already in the drupal database, eg, you can node_load() it, I can't see a reason to node_submit() it before node_save()ing it again.
    – Jimajamma
    Feb 3, 2013 at 15:16
  • I just want to append a value with existing value. Like in loop. But if i place node_load inside for loop then it doesn't work. I placed it outside of for loop then it works. Feb 6, 2013 at 6:08

Your Answer

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

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