This topic is locked

Number of child records

6/28/2011 7:27:52 AM
PHPRunner General questions
J
jackaroo author

I have a mastertable linked to a childtable. I have a need to access the value of the number of childtable records for use in a calculation in a mastertable event. How can I do that?
Thanks,

Jack

C
cgphp 6/28/2011

Jack333,
the following is an example to get total rows from childtable in a mastertable event:

global $conn;

$sql = "SELECT COUNT(*) as total_rows FROM childtable";

$rs = db_query($sql,$conn);

$data=db_fetch_array($rs);



$data['total_rows'] contains the total rows in childtable.
You can add a WHERE condition to the query to filter out values.

G
garethpDevClub member 6/28/2011

Hi
I use the followoing code to check the count and perform an upate based on the result...

global $conn;

$str = "select sum(record) from por_details where grn = '".$values["grn"]."'";

$rs = db_query($str,$conn);

$data = db_fetch_numarray($rs);

IF ($data[0] == "")

{

$strUpdate = "update grn_header set supplier='BLANK' where grn = '".$values["grn"]."'";

db_exec($strUpdate,$conn);

}


por_details is by detail/child table and record is my primary key of this table and grn is the link to the master.

$values["grn"] is the open field on the master table.
Hope this helps.