This topic is locked
[SOLVED]

 Check All Child Records

5/24/2011 1:04:57 PM
PHPRunner General questions
J
joker author

I'm having a small issue I think is tied to my PHP syntax.
I have a list of Parent records each with several child records. I would like to add an action to the List Page: Before Process to check all of the child records. If all of the child records of a particular parent all have the same status of "OK", then move that Parent and Child records to another table.
The problem I'm having with my code is that it loops through all the child records and if ANY of them have a status of "OK", it moves the parent and childs.
Can anyone point me in the right direction so that ALL THE CHILDREN have to have status of "OK" before they are inserted into another table?
All suggestions welcome!
Here is my code:



global $dal;

$dal_TableName = $dal->Table("Table1");



foreach(@$keys as $keyblock){

$arr=split("&",refine($keyblock["Table1_ID"]));

if(count($arr)<1)

continue;

$arr2=array();

$arr2["Table1_ID"]=urldecode(@$arr[0]);

$where = KeyWhere($arr2);

$rstmp = $dal_TableName->Query($where,"");



while ($datatmp=db_fetch_array($rstmp )){

if ($datatmp['STATUS']=="OK"){

for ($i=0; $i<count($keys); $i++){

global $conn;

$strSQLInsert = "insert into Table2 select * from Table1 where Table1_ID='". $keys[$i]["Table1_ID"]."'";

db_exec($strSQLInsert,$conn);

}

}

}

}
J
joker author 5/25/2011

I think the WHILE loop is the wrong direction. I may need to count the number of child records and compare that somehow. Not sure what the PHPRunner syntax for that would be. Has anyone else run across a problem like this?

Sergey Kornilov admin 5/25/2011

I'm not 100% sure I understand what you looking for. Is there a way to rephrase it?
I'm not sure what is "PHPRunner syntax". To the best of my understanding you can use any PHP code here.

J
joker author 5/26/2011



I'm not 100% sure I understand what you looking for. Is there a way to rephrase it?
I'm not sure what is "PHPRunner syntax". To the best of my understanding you can use any PHP code here.


No problem Sergey. For example, I have two parent records each with several child records. When the field "STATUS" of all the child records of a particular parent change to "OK", I would like to move that particular parent and child records to another table.
My issue is that my code works when ANY of the child records have a status of "OK". I need all the children of a parent to be status="OK" before they are moved.
Do you know how I could have phprunner check all the children for the same status?

L
Lena 5/27/2011

Hi,

please find the example code to get records from the parent table which have all child records with STATUS = "OK". Use this code in the List Page: Before Process event on the Events tab.



global $dal;

$dal_TableParent = $dal->Table("table1"); // parent table

$dal_TableChild = $dal->Table("table2"); // children table

$rs = $dal_TableParent->QueryAll();

while ($data = db_fetch_array($rs))

{

if ($data["status"]=="OK"){

//parent record has status = OK

$rsChild = $dal_TableChild->Query("id=".$data['child_id'],"");

$child = array(); // children with status = OK

$countAllChild = 0; // count of all children

while ($dataChild = db_fetch_array($rsChild)){

if ($dataChild["status"]=="OK"){

$child[]=$dataChild;

}

$countAllChild ++;

}

if ($countAllChild==sizeof($child)){

//insert to another table data from $date and $child



}

}



}