kmcook
Posts: 6
|
| Posted: 11/18/2004, 2:44 PM |
|
Still struggling to get to grips with PHP...
The show code for the CheckBoxList control is now a udf .
(Thanks Luis, hope you read this .... and able to help again)
In the processing of the checkboxlist(s) on a form there is a need to have a function
to modify the intersect table according to users checking of the checkbox list.
I've tried to convert the CCS example code to a function but again alas , without success.
On an update, the appropriate records are first deleted prior to the next insert step,
but the insert from the array does not occur.
I have fully functioning forms where all the values hardcoded, but now I have a need for 7 lists
on a form , and similar forms to come.
In this attempt, the main table is PREGNANCIES, with a key of PREGNANCY_ID.
PREGNANCIES has a many/many to the tests within table ANTENATAL_TESTS,
and the intersect table holding the tests-for-a-pregnancy is TESTS_PREGNANCIES,
with fields ANTTEST_ID, PREGNANCY_ID.
My connection is named Peri1, the Checkbox list control is named chkList1.
I'd hope to call the 'modify' code modelled on the CCS example thus:
chkListModify($Actions, $ThisConnection, $ParamName, $ControlName, $MainTableKey, $MainTableName, $IntersectTableName, $IntersectField1, $IntersectField2)
For Update my call would then be...
chkListModify("Update",new clsDBPeri1(),"PREGNANCY_ID","chkList1","PREGNANCY_ID","PREGNANCIES","TESTS_PREGNANCIES", "ANTTEST_ID", "PREGNANCY_ID");
Here's my failing function. What have I wrong this time??
function chkListModify($Actions, $ThisConnection, $ParamName, $ControlName, $MainTableKey, $MainTableName, $ITableName, $IField1, $IField2)
{
// set some memvars
$mainID = 0;
$chkValue = 0;
$ControlList = array();
$GetLastInsKey = 0;
$mainID = CCGetFromGet($ParamName,0);
$ControlList = CCGetFromPost($ControlName,array());
if ($Actions == "Insert") {
$GetLastInsKey = CCDLookup("max(".$MainTableKey.")",$MainTableName,"",$ThisConnection);
// insert new links
reset($ControlList);
while(list($key,$chkValue) = each($ControlList)) {
$ThisConnection->query("INSERT INTO ".$ITableName."(".$IField1.", ".IField2.") VALUES(".$ThisConnection->ToSQL($chkValue,ccsInteger).",".$ThisConnection->ToSQL($GetLastInsKey,ccsInteger).")");
}
// end of $Actions
}
// *** the next delete process performs, all matching records are deleted from intersect table
if ($mainID > 0) {
if ( ($Actions == "Delete") Or ($Actions == "Update") ) {
// delete intersect table data this $mainID
$ThisConnection->query("DELETE FROM ".$ITableName." WHERE ".$IField2." = ".$ThisConnection->ToSQL($mainID,ccsInteger));
}
// *** ..but this insert of client checked changes does not insert into intersect table .
if ($Actions == "Update") {
// insert new/altered intersect table data
reset($ControlList);
while( list($key,$chkValue) = each($ControlList) ) {
$ThisConnection->query("INSERT INTO ".$ITableName."(".$IField1.", ".IField2.") VALUES(".$ThisConnection->ToSQL($chkValue,ccsInteger).",".$ThisConnection->ToSQL($mainID,ccsInteger).")");
}
}
// end of $mainID = 0
}
$ThisConnection->close();
// end function
}
Any help gratefully received
Casey
|
 |
 |
lvalverdeb
Posts: 299
|
| Posted: 11/23/2004, 6:44 AM |
|
Casey,
Have you established the reasons why the INSERT clause in the update action fails? empty array?. Unfortunately, all I can do is give you some pointers or tips on how to "debug" your function.
When I run into these issues I debug the code by setting some "breakpoints" and trace the values of arrays, variables and sql statements. In the absence of an integrated debugger I resort to echoing these results. For example: print_r($myarray), echo $myvar , etc.
From the problems you describe, here's how I would debug your function. It is possible that php may halt the execution of the code arguing that "HTTP headers have already being sent" but by using these breakpoints properly you should be able to determine what the problem is. It is also quite possible that the problem is not in the function itself but in some other event code.
function chkListModify($Actions, $ThisConnection, $ParamName, $ControlName, $MainTableKey, $MainTableName, $ITableName, $IField1, $IField2)
{
// set some memvars
$mainID = 0;
$chkValue = 0;
$ControlList = array();
$GetLastInsKey = 0;
$mainID = CCGetFromGet($ParamName,0);
$ControlList = CCGetFromPost($ControlName,array());
if ($Actions == "Insert") {
$GetLastInsKey = CCDLookup("max(".$MainTableKey.")",$MainTableName,"",$ThisConnection);
// insert new links
reset($ControlList);
while(list($key,$chkValue) = each($ControlList)) {
$ThisConnection->query("INSERT INTO ".$ITableName."(".$IField1.", ".IField2.") VALUES(".$ThisConnection->ToSQL($chkValue,ccsInteger).",".$ThisConnection->ToSQL($GetLastInsKey,ccsInteger).")");
}
// end of $Actions
}
// *** the next delete process performs, all matching records are deleted from intersect table
if ($mainID > 0) {
if ( ($Actions == "Delete") Or ($Actions == "Update") ) {
// delete intersect table data this $mainID
$ThisConnection->query("DELETE FROM ".$ITableName." WHERE ".$IField2." = ".$ThisConnection->ToSQL($mainID,ccsInteger));
}
// *** ..but this insert of client checked changes does not insert into intersect table .
if ($Actions == "Update") {
// insert new/altered intersect table data
print_r($ControlList); // print the contents of the array
reset($ControlList);
while( list($key,$chkValue) = each($ControlList) ) {
$mySQL = "INSERT INTO ".$ITableName."(".$IField1.", ".IField2.") VALUES(".$ThisConnection->ToSQL($chkValue,ccsInteger).",".$ThisConnection->ToSQL($mainID,ccsInteger).")"
echo $mySQL; //
$ThisConnection->query($mySQL);
}
}
// end of $mainID = 0
}
$ThisConnection->close();
// end function
}
All the best.
_________________
lvalverdeb
CR, GMT-6
XAMPP/Ubuntu/CCS3.2/4 |
 |
 |
|