This topic is locked

Using Detail Table Totals in Master Table Variables

1/7/2011 5:44:09 PM
PHPRunner General questions
D
DigitalDean8 author

Hopefully this is an easy answer for someone. I am creating an order management system and am creating Quotation, Invoice, PO forms etc. I use a linked detail table to store "Order Items" and then Master table fields to store Sub Totals of various types and then of course an overall "Order Total" amount. I need to use the linked detail table TOTAL amount as a component of the overall "Order Total". How is that calculated value for the detail table accessed when in the Add/Edit/View/ pages of the Master Page.
Can someone help me with the syntax required to access that details table calculated value? I would like to add up the calculated $3,450 + 500+125+200 = Job Total
Here is an example

D
danaci 1/8/2011

use After record added event on the Events tab for this purpose.
Here is just a sample:
global $strTableName,$dal;

$rs = CustomQuery("select sum(FieldName) from DetailTableName where DetailKey=".$_SESSION[$strTableName."_masterkey1"]);

$data = db_fetch_numarray($rs);
$dal->MasterTableName->Value["TotalFieldName"] = $data[0];

$dal->MasterTableName->Param["MasterKey"] = $_SESSION[$strTableName."_masterkey1"];

$dal->MasterTableName->Update();

D
DigitalDean8 author 1/8/2011

Thank you for this help, I think that we are close but I am not quite sure I am using the correct table names in the snippet you posted as it is throwing a syntax error: Here is some additional information from my specific project:
Master Table Name: JOBS

Master Table - Order Total Field Name: JOB_TOTAL
Detail Table Name: JOB_CALL

Detail Table Record Total Field name: CALL_TAMOUNT
So this is how I edited the code you posted:
============================================================================

global $strJobs,$dal;

$rs = CustomQuery("select sum(Call_TAmount) from Job_Call where DetailKey=".$_SESSION[$strJobs."_masterkey1"]);

$data = db_fetch_numarray($rs);
$dal->MasterTableName->Value["Job_Total"] = $data[0];

$dal->MasterTableName->Param["MasterKey"] = $_SESSION[$strJobs."_masterkey1"];

$dal->MasterTableName->Update();

============================================================================
I am not sure but I think the issue lies with this section: DetailKey=".$_SESSION[$strJobs."_masterkey1"]
any ideas on what I am doing incorrectly?

D
danaci 1/8/2011

global $strTableName,$dal;

$rs = CustomQuery("select sum(CALL_TAMOUNT) from JOB_CALL where DetailKey=".$_SESSION[$strTableName."_masterkey1"]);

$data = db_fetch_numarray($rs);
$dal->JOBS->Value["JOB_TOTAL"] = $data[0];

$dal->JOBS->Param["input master table key here "] = $_SESSION[$strTableName."_masterkey1"];

$dal->JOBS->Update();

D
DigitalDean8 author 1/8/2011

Still getting a Syntax Error with this code. See the details below:
Error type 256

Error description You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

URL 64.250.242.220/octanejobs/Active_Jobs_edit.php?

Error file C:\xampp\htdocs\OctaneJobs\include\dbconnection.php

Error line 36

SQL query select sum(CALL_TAMOUNT) from JOB_CALL where DetailKey=
More info
Call stack

File: line Function Arguments

0. include\dbconnection.php:36 db_query 1. select sum(CALL_TAMOUNT) from JOB_CALL where DetailKey=;

  1. Resource id #11;

    1. include\dal.php:9 CustomQuery 1. select sum(CALL_TAMOUNT) from JOB_CALL where DetailKey=;

    2. include\Active_Jobs_events.php:430 eventclass_Active_Jobs->AfterEdit 1. Array ( [Job_Status] => Active Job [Job_Modified] => 2011-01-08 11:42:32 [Show_Name] => Exhibitor Show [Show_City] => Las Vegas [Show_Venue] => LVCC [Show_Start] => 2011-03-27 ...;


  2. JobID=5;
  3. Array ( [JobID] => 5 [Job_Status] => Active Job [Job_Created] => 2011-01-07 13:51:00 [Job_Modified] => 2011-01-08 10:48:31 [Show_Name] => Exhibitor Show [Show_City] => Las Vega...;
  4. Array ( [JobID] => 5 ) ;
  5. ;

    3. Active_Jobs_edit.php:2049 Global scope N/A



D
DigitalDean8 author 1/8/2011

Is it also possible that since I am using a "Custom View" rather than the source table that this is causing an issue? The actual table name is "Jobs" but the custom view I am using is "Active_Jobs" which is just a filtered view of Jobs. I am attempting to update the actual table name of Jobs which I think is correct. Could this be the issue?

D
danaci 1/8/2011

hi,
use trigger of this problem.

ex.
delimiter //

create trigger total on job_call before insert

for each row

begin

update jobs set jobtotal= jobtotal+ new.cal_tamount where id=new.id;

end;
delimiter ;
please customize this code your database.

D
DigitalDean8 author 1/9/2011

The Trigger also throws a syntax error. I am researching the MySQL variant of it but so far nothing works.

D
danaci 1/9/2011

detail table on Add page after record added event try this code.
global $conn;

$str = "select sum(call_tamount) from job_call where detailkey = '{$values["detailkey"]}' ";

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

$data = db_fetch_numarray($rs);

$total=$data[0];
$strUpdate = "update jobs set job_total= '{$total}' where mastertablekey = '{$values["detailkey"]}'";

db_exec($strUpdate,$conn);
warning ! detailkey and mastertablekey is your table detailkey, mastertablekey..