How to update parent column amount if it has children

How can I update the amount to add 500 ,if the parentid have left and right children

example in my data:

1 is the parent of 5 and 6

5 is the parent of 7 and 8

7 is the parent of 9 and 10

so 7,5 and 1 will be updated there amount of 500…Is this possible ?

CREATE TABLE `mytree` (
    `pid` INT(11) NOT NULL,
    `memd` INT(11) NOT NULL,
    `position` CHAR(1) NOT NULL,
    `amount` DECIMAL(10,2) NOT NULL
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM
;


pid          memd     position      amount

0             1                     0.00

1             5          L          0.00

1             6          R          0.00

5             7          L          0.00

5             8          R          0.00

7             9          L          0.00

7             10         R          0.00

9             11         L          0.00

Thank you in advance.

Yes

UPDATE  mytree
SET amount = amount + 500
WHERE memd in (1, 5, 7)

All I could think of would be to iterate through the table along the lines of the recursive function you posted elsewhere and update using that information. I am not experienced enough to do this in SQL, perhaps @jemz you should ask in the SQL area.

(Edited to add the word “not” which I missed out above. Makes me sound less like a bighead).

I agree. You might need to do something like this.

$data = array();
$sql = "SELECT pid, position FROM mytree"; 
$query = $db->prepare($sql);
$query->execute();     
while($row = $query->fetch(PDO::FETCH_ASSOC)){
    $data[$row['pid']][] = $row['position'];
}

$membs = array();
foreach($data as $pid => $arr):
    if(in_array('L',$arr) && in_array('R',$arr)):
        $membs[] = $pid;
    endif;
endforeach;
    
if(!empty($membs)):
    $membs = implode(', ',$membs);
    $sqlu = "UPDATE  mytree
    SET amount = amount + 500
    WHERE memd in ($membs)";    
    $queryu = $db->prepare($sqlu);
    $queryu->execute();
endif;

Thank you for the reply.

This is what I’m working to update the parent

@guido2004, @Drummin, @droopsnoot,. I’m just confuse to use my recursive function on how can I check if this parentid have both left and right children then update the parentid amount,then go up to also his parent then update also there amount and so on…just like in my example when parentid 7 added new member 10 now 7 have left and right children,then 7 will be given 500 amount,then his parent 5 will be given 500 and the parent of 5 which is 1 will be given 500 also.

I think this recursive function will be call after I succesffully added new member.

public function update_amount($parent,$level,$rec = array()){
    try {
        $cmd = $this->connection->prepare('update mytree set amount = amount + 500 where parentid = ?');

        $cmd->execute(array($parent));

        while ( $row =  $cmd->fetch(PDO::FETCH_ASSOC)) {
            $rec= $this->update_amount($row['memberid'], $level + 1,$rec);
        }


        $cmd = null;
        return $rec;

    }
    catch(PDOException $ex){
        return $ex->getMessage();
    }

}

Anyhelp @fretburner ?

Thank you in advance.

I’ve been getting deeper into PDO trying to get away from mysqli_ so I may be off base.
But aren’t you missing a
bindValue ?

I have this table structure and data

CREATE TABLE `mytree` (
    `pid` INT(11) NOT NULL,
    `memd` INT(11) NOT NULL,
    `position` CHAR(1) NOT NULL,
    `amount` DECIMAL(10,2) NOT NULL
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM
;


pid          memd     position      amount

0             1                     0.00

1             5          L          0.00

1             6          R          0.00

5             7          L          0.00

5             8          R          0.00

7             9          L          0.00

7             10         R          0.00

9             11         L          0.00

here is my my tree.

I’m just confuse to use my recursive function on how can I check in SQL statement if this
parentid have both left and right children then update the parentid
amount,then go up also to his parentid then update also there amount and
so on…just like in my example when parentid 7 added new member 10, now
7 have left and right children,then 7 will be given 500 amount,then his
parent 5 will be given 500 and the parent of 5 which is 1 will be given
500 also.

public function update_amount($parent,$level,$rec = array()){
    try {
        $cmd = $this->connection->prepare('update mytree set amount = amount + 500 where parentid = ?');

        $cmd->execute(array($parent));

        while ( $row =  $cmd->fetch(PDO::FETCH_ASSOC)) {
            $rec= $this->update_amount($row['memberid'], $level + 1,$rec);
        }


        $cmd = null;
        return $rec;

    }
    catch(PDOException $ex){
        return $ex->getMessage();
    }

}

Thank you in advance.

@Drummin,

$data = array();
$sql = "SELECT pid, position FROM mytree"; 
$query = $db->prepare($sql);
$query->execute();     
while($row = $query->fetch(PDO::FETCH_ASSOC)){
    $data[$row['pid']][] = $row['position'];
}

$membs = array();
foreach($data as $pid => $arr):
    if(in_array('L',$arr) && in_array('R',$arr)):
        $membs[] = $pid;
    endif;
endforeach;
    
if(!empty($membs)):
    $membs = implode(', ',$membs);
    $sqlu = "UPDATE  mytree
    SET amount = amount + 500
    WHERE memd in ($membs)";    
    $queryu = $db->prepare($sqlu);
    $queryu->execute();
endif;

can I call this after I add new member ?

@Mittineague,

I want to check if the parent has left and right child then update the parentid with amount of 500 then go up to tree update also there parentid to 500 amount. parent 7,5 and 1 receive amount 500 ,when parent 7 completed the left and right.

Yes, but I think you’re going to need to track IF the person was given the 500 because as it stands, they will get the bump every time the script is run. Something like field received 1 or 0. Something like this.

$data = array();
$sql = "SELECT pid, memd, position, received FROM mytree"; 
$query = $db->prepare($sql);
$query->execute();     
while($row = $query->fetch(PDO::FETCH_ASSOC)){
    $data[$row['pid']][] = $row['position'];
    $data[$row['memd']]['received'] = $row['received'];
}

$membs = array();
foreach($data as $pid => $arr):
    if(in_array('L',$arr) && in_array('R',$arr) && $arr['received'] == 0):
        $membs[] = $pid;
    endif;
endforeach;
    
if(!empty($membs)):
    $membs = implode(', ',$membs);
    $sqlu = "UPDATE  mytree 
    SET 
    amount = amount + 500
    , received = 1     
    WHERE memd in ($membs)";    
    $queryu = $db->prepare($sqlu);
    $queryu->execute();
endif;

I think value is passed in the array() as the argument to execute(). Always confuses me too, I explicitly use bindParam rather than this method. I thought this was PDO though.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.