0

Scenario:

I have two tables (Parent and Child relationship) = (tblreqslip, tblreqdetails)

tblreqslip = column fields (parent_id, client_name, date)
tblreqdetails = column fields (child_id, parent_id, subtotals)

Needs help:

In getting all the values in my child table (field ="subtotals") and add them all up "simple addition Math function".

Note: My child tblreqdetails field =(differ in values) Ex: Parent ID no="1" has child field value of 10 subtotal fields, Parent ID no="2" has a child field value of 15 subtotal fields.

Here is where I am stuck at:

$p_id=$_GET['parent_id']; //get from Post URl
$query = "SELECT * FROM tblreqdetails WHERE child_id='$p_id'";
$select = mysql_query($query) or die(mysql_error());
$rw = mysql_fetch_array($select);

(I am loosing a line here to get the values from "subtotal" field, ADD them all up no matter how many are the subtotal values in the field and just echo the TOTAL amount) Ex: ID="1" $TOTAL value =(200.50 + 1000 + 3000 .... so on till how many subtotal my child table has a value depending on my Parent ID)

Thanks so Much in Advance.

4

1 回答 1

0

我假设你有这两个表:

tblreqslip
+++++
 ID +
+++++
  1 |
  2 |
  3 |

tblreqdetails
++++++++++++++++++++++
child_id | subtotals +
++++++++++++++++++++++
  1      |  4500     |
  1      |  6000     |
  2      |  3000     |
  3      |  1500     |
  3      |  1000     |

更新 试试这个,它适用于我:

$p_id=$_GET['parent_id'];
$query = "SELECT * FROM tblreqdetails WHERE child_id='$p_id'";
$select = mysql_query($query) ;
$i = 0;
while ($rw = mysql_fetch_array($select)){
$i++;
$subtotals[$i] = $rw['subtotals'];
}
$TOTAL = array_sum($subtotals); // sum all the subtotals field with selected child_id
echo "id : $p_id total is ".$TOTAL;

现在,您得到了小计的 TOTAL。

于 2013-10-26T02:08:16.870 回答