PDA

View Full Version : how can I optimize this...


akstudio348653
04-10-2009, 12:30 PM
I have a form.

I have 3 tables in a db. (assecories, products, lookup)

in my form, I have 14 check boxes. you can select more than 1. This information goes into the lookup. The code is as follows:

<?php
// WA Application Builder Insert
if ($_SERVER["REQUEST_METHOD"] == "POST") // Trigger
{
$WA_connection = $connNews;
$WA_table = "lookup";
$WA_sessionName = "lookup_accessory_ID";
$WA_redirectURL = "";
$WA_keepQueryString = false;
$WA_indexField = "accessory_ID";
$WA_fieldNamesStr = "accessory_ID|product_ID";
$WA_fieldValuesStr = "".$_SESSION['accessories_accessory_ID'] ."" . "|" . "".((isset($_POST["duo"]))?$_POST["duo"]:"") ."";
$WA_columnTypesStr = "',none,''|',none,''";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_fieldValues = explode("|", $WA_fieldValuesStr);
$WA_columns = explode("|", $WA_columnTypesStr);
$WA_connectionDB = $database_connNews;
mysql_select_db($WA_connectionDB, $WA_connection);
if (!session_id()) session_start();
$insertParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
$WA_Sql = "INSERT INTO `" . $WA_table . "` (" . $insertParamsObj->WA_tableValues . ") VALUES (" . $insertParamsObj->WA_dbValues . ")";
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
$_SESSION[$WA_sessionName] = mysql_insert_id();
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
?>


This line is question is:


$WA_fieldValuesStr = "".$_SESSION['accessories_accessory_ID'] ."" . "|" . "".((isset($_POST["duo"]))?$_POST["duo"]:"") ."";


I thought I could just duplicate this 14 times. Doest work

Do I have to do a "DataAssist>>insert record" 13 more times? I would think there is a way to optimize this.

thanks

Ray Borduin
04-10-2009, 02:25 PM
Is this for a lookup table? You should be using "Manage relational Table" instead of insert and it will loop on the checked values for you.

Otherwise I suppose you could apply it thirteen times or write a while loop by hand around it.

akstudio348653
04-10-2009, 02:32 PM
I get this error when trying to insert "manage relational table"

---------------------------
You must have a multi-select or fields within a repeated region to use this interface.
---------------------------

I'm using check boxes.

akstudio348653
04-10-2009, 02:47 PM
This doest make sense to me, I have encased my table (which containes 14 check boxes) within a repeat region. All that does is repeat the table 14 times, giving me a total of 196 check boxes.

following the pdf tutorial, I understand that, if you wanted to add 5 users at a time, then this makes sense. However, I have 1 form where a "product accessory" is added into the db. This form must also associate this new "product accessory" with an already existing product on the market. Therefore, these 14 check boxes must end up in the lookup table, and the rest of the form goes to the accessories table.

The repeat region seems to... well...repeat a region, but I think I need a loop or something.

I do have all this working fine, but it will only write the first checkbox.... unless, of course, I insert 13 more 'insert record' statements.

Ray Borduin
04-10-2009, 03:38 PM
You should only have one checkbox inside the repeat region. The value of the checkbox should be set to the id in the database and you should get the label from the database as well.

This way you would have 14 unique checkboxes, one for each row in the database and if you add more rows to the database, the correct corresponding row would appear in your interface.

That is the concept of the relational table and the one to many relationship... that you have one table of values you want to insert into another table and maintain a relationship to a third. I think this is what you want, but you need to start by creating your form dynamically as you have kind of started to do.