Forums

Change Value in model loop

NickOg 14 Mar, 2013
Morning

This has me puzzled.

I have a value 'originalNumberPlantsRemaining' as a field in my database. In my loop I want to save the current value 'NumberPlantsRemaining' to that 'originalNumberPlantsRemaining' value before I edit the value.

I do it so
foreach ($form->data['mdlPatchMembers'] as $aPatchMember) {
// save current plants remaining
$aPatchMember['originalNumberPlantsRemaining'] = $aPatchMember['NumberPlantsRemaining'];
dump ($aPatchMember['originalNumberPlantsRemaining'],'orig');
dump( $aPatchMember['NumberPlantsRemaining'],'current');

.....

My problem is that when I look at the output after the loop so

<?php
} // foreach ($form->data['mdlpickedToken'
dump ($form->data['mdlPatchMembers'],'out');
?>


That value is lost.

This is the dump output


Application: site
Lock Window Refresh Close Window Expand all Collapse all

    [string] orig = "5" Tooltip
    [string] current = "5" Tooltip
    [string] orig = "3" Tooltip
    [string] current = "3" Tooltip
    [string] orig = "1" Tooltip
    [string] current = "1" Tooltip
    [string] orig = "1" Tooltip
    [string] current = "1" Tooltip
    [string] orig = "1" Tooltip
    [string] current = "1" Tooltip
    [string] orig = "2" Tooltip
    [string] current = "2" Tooltip
    [string] orig = "2" Tooltip
    [string] current = "2" Tooltip
    [string] orig = "1" Tooltip
    [string] current = "1" Tooltip
    [array] out Tooltip
        [array] 0
            [string] Acquired_Stock_ID = "0"
            [string] NumberPlantsRemaining = "5"
            [string] Number_Plants = "5"
            [string] Propagated_Stock_ID = "9"
            [string] RPM_ID = "18"
            [string] RPM_Plant_Date = "2010-07-31"
            [string] Reveg_Patch_ID = "4"
            [string] dttmChanged = "2012-08-31 23:46:40"
            [string] id_stock = "P9"
            [null] originalNumberPlantsRemaining
        [array] 1
        [array] 2
        [array] 3
        [array] 4
        [array] 5
        [array] 6
        [array] 7


J!Dump v2012-10-31


I have clearly missed something but can't work out what!

Any suggestions?

Thanks

Nick
GreyHead 17 Mar, 2013
Hi Nick,

I find the code a bit hard to understand but I supect that the problem is that when you do
foreach ($form->data['mdlPatchMembers'] as $aPatchMember) {
then $aPatchMember has a scope limited to the foreach() loop and after that is lost or meaningless.

I'm not quite sure what is it supposed to be any how.

If you want to keep the old value then you can just take a copy of the whole array under a different name.

Bob
NickOg 23 Mar, 2013
Hi Bob,

Of course - that was exactly it. Thanks.

Nick
This topic is locked and no more replies can be posted.