This topic is locked

Need Newbie Help

4/21/2007 8:31:34 AM
PHPRunner General questions
G
Greeham author

Hi,
I have 3 tables I need to interact with each other. I have table A (The master) that has field A (an ID) and also field B an integer. Table B is child to Table A and has an ID linked to that of A and also has an integer in Field B.
In Table C i have another ID field the same as Table A and B and I want a field to contain the total of the integers from thje fields I mentioned in Table's A and B.
Firstly, is this possible with PHP runner in the advanced sql section? Would i need to amend the resulting output php files before publishing?
I am not a mysql/php expert so if anyone could point me in the right direction that would be a great help.
Thanks,
G

T
thesofa 4/22/2007

here is the test i set up
this file makes table a

/*

MySQL Data Transfer

Source Host: 192.168.0.44

Source Database: test

Target Host: 192.168.0.44

Target Database: test

Date: 23/04/2007 01:17:37

*/
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------

-- Table structure for a

-- ----------------------------

CREATE TABLE `a` (

`id` int(11) NOT NULL,

`field_b` int(11) default NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------

-- Records

-- ----------------------------

INSERT INTO `a` VALUES ('1', '33');

INSERT INTO `a` VALUES ('2', '25');

INSERT INTO `a` VALUES ('3', '15');

INSERT INTO `a` VALUES ('4', '45');


this makes table b

/*

MySQL Data Transfer

Source Host: 192.168.0.44

Source Database: test

Target Host: 192.168.0.44

Target Database: test

Date: 23/04/2007 01:17:45

*/
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------

-- Table structure for b

-- ----------------------------

CREATE TABLE `b` (

`id` int(11) NOT NULL,

`field_b` int(11) default NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------

-- Records

-- ----------------------------

INSERT INTO `b` VALUES ('1', '55');

INSERT INTO `b` VALUES ('2', '56');

INSERT INTO `b` VALUES ('3', '871');

INSERT INTO `b` VALUES ('4', '12');


This makes table c

/*

MySQL Data Transfer

Source Host: 192.168.0.44

Source Database: test

Target Host: 192.168.0.44

Target Database: test

Date: 23/04/2007 01:17:55

*/
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------

-- Table structure for c

-- ----------------------------

CREATE TABLE `c` (

`id` int(11) NOT NULL,

`calc_total` int(11) default NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------

-- Records

-- ----------------------------

INSERT INTO `c` VALUES ('1', null);

INSERT INTO `c` VALUES ('2', null);

INSERT INTO `c` VALUES ('3', null);

INSERT INTO `c` VALUES ('4', null);


amend the sql query in phpr to this

SELECT

`a`.`id`,

`a`.`field_b`,

`b`.`field_b`,

`a`.`field_b`+`b`.`field_b`as 'pinkone'

FROM

`a`

Inner Join `b` ON `a`.`id` = `b`.`id`

Left Join `c` ON `c`.`id` = `a`.`id`



this will allow you to see the values from table a andf b at the same time in the php scripts
It also adds the fields from a and b to give a total which I have called pinkone

you then use an after record saved event to put the value of `pinkone` into table c
hth

G
Greeham author 4/23/2007

here is the test i set up

this file makes table a

/*

MySQL Data Transfer

Source Host: 192.168.0.44

Source Database: test

Target Host: 192.168.0.44

Target Database: test

Date: 23/04/2007 01:17:37

*/
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------

-- Table structure for a

-- ----------------------------

CREATE TABLE `a` (

`id` int(11) NOT NULL,

`field_b` int(11) default NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------

-- Records

-- ----------------------------

INSERT INTO `a` VALUES ('1', '33');

INSERT INTO `a` VALUES ('2', '25');

INSERT INTO `a` VALUES ('3', '15');

INSERT INTO `a` VALUES ('4', '45');


this makes table b

/*

MySQL Data Transfer

Source Host: 192.168.0.44

Source Database: test

Target Host: 192.168.0.44

Target Database: test

Date: 23/04/2007 01:17:45

*/
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------

-- Table structure for b

-- ----------------------------

CREATE TABLE `b` (

`id` int(11) NOT NULL,

`field_b` int(11) default NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------

-- Records

-- ----------------------------

INSERT INTO `b` VALUES ('1', '55');

INSERT INTO `b` VALUES ('2', '56');

INSERT INTO `b` VALUES ('3', '871');

INSERT INTO `b` VALUES ('4', '12');


This makes table c

/*

MySQL Data Transfer

Source Host: 192.168.0.44

Source Database: test

Target Host: 192.168.0.44

Target Database: test

Date: 23/04/2007 01:17:55

*/
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------

-- Table structure for c

-- ----------------------------

CREATE TABLE `c` (

`id` int(11) NOT NULL,

`calc_total` int(11) default NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------

-- Records

-- ----------------------------

INSERT INTO `c` VALUES ('1', null);

INSERT INTO `c` VALUES ('2', null);

INSERT INTO `c` VALUES ('3', null);

INSERT INTO `c` VALUES ('4', null);


amend the sql query in phpr to this

SELECT

`a`.`id`,

`a`.`field_b`,

`b`.`field_b`,

`a`.`field_b`+`b`.`field_b`as 'pinkone'

FROM

`a`

Inner Join `b` ON `a`.`id` = `b`.`id`

Left Join `c` ON `c`.`id` = `a`.`id`



this will allow you to see the values from table a andf b at the same time in the php scripts
It also adds the fields from a and b to give a total which I have called pinkone

you then use an after record saved event to put the value of `pinkone` into table c
hth


That worked great when running through Navicat, what I am unsure of (being a PHPRunner virgin) is how to apply that to my own project. The 'pinkone' value, should that be a field already named in Table C? I also edited the SQL manually, but it only showed the fields I am calculating, there are other fields I want listed in the list page as well as 'pinkone'.
And one last question, how would I add the event I require to update the Table C 'pinkone' value?
Many questions and probably very simple, but I am on a steep learning curve and thanks for your help so far <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=17191&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
G

G
Greeham author 4/23/2007

To add to my last post I have it working kind of but there is another small twist:-
SELECT

`tab1`.`id`,

`tab1`.`total1`,

`tab2`.`runtotal`,

`tab1`.`total1`+ `tab2`.`runtotal` AS 'pinkone'

FROM

`tab1`

Inner Join `tab2` ON `tab1`.`id` = `tab2`.`id`

Left Join `tab3` ON `tab3`.`id` = `tab1`.`id`
I really would like to have tab2.runtotal for any one id SUM'ed before adding to total1 to make pinkone.
I hope that makes sense,
Thanks again,
G

J
Jane 4/24/2007

Hi,
I'm not sure that I understand you correctly.

Do you want to show calculated values on the generated pages? Or do you want to update values in the table3 in the database when you add or edit records in the table1 and table2?

Please give me more detailed description of what you need to achieve and I'll try to help you.

G
Greeham author 4/24/2007

Hi Jane,
The tab1 value will always stay the same. Tab2 value is a user entered value, that has entries continually entered (i.e records with a value), and everytime a new record for a particular id is entered I want it to then update the value in tab3 basically everytime another tab2 value is entered.
Does that make sense now? Sorry, for the bad explanation!

J
Jane 4/24/2007

I see what you're saying.
To update value in the table3 use Before record added or Before record updated event on the Events tab.

Here is a sample code:

global $conn;

$str = "select * from table1 where id=".$values["id"];

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

$data = db_fetch_array($rs);
$totalsum = $data["total1"] + $values["runtotal"];
$strUpdate = "update table3 set FieldName=".$totalsum." where id=".$values["id"];

db_exec($strUpdate,$conn);



where FieldName is your actual field name in the table3.

G
Greeham author 4/28/2007

Worked great, thanks Jane....