This topic is locked

Edit Category Label Auto-Update All Records Assigned

11/6/2007 1:53:59 AM
PHPRunner General questions
D
daveDiamond author

I have seriously looked through this entire forum looking for a way to do the following: Any help is appreciated (Jane, Alexey et al)
I have two tables.. people and people_settings. people contains all records about the employees including the GroupID field which is a Lookup Field that calls people_settings.GroupID for its params. What I want to do is code this thing so that if someone changes the label for people_settings.GroupID, all associated records are updated with the new label. So, here's the play..
Ooh hey wait, someone has updated my name here in the people_settings table. Quick, find all those records with my old new and give them my new name. Get it?
I've tried this code and echoed the query but it's not right and I can't figure how to call for the old name and update it. Enough rambling.

// Before record updated

function BeforeEdit(&$values, $where, &$oldvalues, &$keys,&$message,$inline)

{

// Parameters:

// $values - Array object.

// Each field on the Edit form is represented as a 'Field name'-'Field value' pair

// $where - string with WHERE clause pointing to record to be edited

// $oldvalues - Array object with existing data record content

// $keys - Array object with added record key column values
//********** Custom code ************

// put your custom code here

global $conn;

$strUpdate = "update people set GroupID = '".$values["GroupID"]."' where GroupID = '".$values["GroupID"]."'";

echo $strUpdate;

$db_exec($strUpdate,$conn);

return true;
// return true if you like to proceed with editing this record

// return false otherwise
} // function BeforeEdit


Running that code gives me the dreaded Error type 8 Undefined variable: db_exec. Can anyone knock me on the head here?
Dave

Alexey admin 11/6/2007

Dave,
remove a dollar sign ($) from the beginning of db_exec line

D
daveDiamond author 11/6/2007

Dave,

remove a dollar sign ($) from the beginning of db_exec line


Alexey,
Thanks, I needed that. I can't believe I didn't even notice that. So now may code looks like this..

global $conn;

$strUpdate = "update people set GroupID = '".$values["GroupID"]."' where GroupID = '".$values["GroupID"]."'";

echo $strUpdate;

db_exec($strUpdate,$conn);

return false;


This code works but when the form is saved it's looking for the NEW GroupID value in the 'people' table to update not the OLD one. How do I phrase the WHERE clause to look for the old when when updating? Understand? The other interesting thing is that when I try this code, it updates the GroupID value on the 'people_settings 'edit page but when I return to the 'people_settings' list, it's not updated. I'm guessing this is because 'return' is set to false. However, when I set 'return' to true I get a 'headers already sent' error. Ideas?
Thanks again for your time.
Dave

D
daveDiamond author 11/7/2007

I'm stuck on this. Can anyone else take a stab at this one? Jane, Sergey?

J
Jane 11/7/2007

Dave,
try to use this one:

//** Custom code ****

// put your custom code here

global $conn;

$strUpdate = "update people set GroupID = '".$values["GroupID"]."' where GroupID = '".$oldvalues["GroupID"]."'";

echo $strUpdate;

db_exec($strUpdate,$conn);

return true;
// return true if you like to proceed with editing this record

// return false otherwise

D
daveDiamond author 11/7/2007

Dave,

try to use this one:


That's what I needed Jane. $oldvalues. Is that documented anywhere?
Thanks again Jane!
UPDATE I'm an idiot. That var is right in the page. Duh!

H
horsey_kim 11/7/2007

Does this work even with inline edit?
Kim

J
Jane 11/8/2007

Kim,
sure. Before record updated event work for simple and inline edit pages.

If you want to use Before record updated event for simple edit page only use this code:

if (!$inline)

{

//your code

}