Pneumonic Posted March 6, 2006 Share Posted March 6, 2006 I’m making a survey system and I’ve got a form with a list of answer values. (This part of the program takes and adds the answer types that are available, which, when creating the survey creation form, will be pulled from the db.) Now I want to take those answer values and stick them in one table and then take the other parts and stick them in another table. Problem is, I’m not sure how to get the values out of the array so that I’m not entering blank information into the db. I went to see if my server had mysql 5, but it does not, because if it had I would just convert the database to myisam and then run a trigger to see if there was any data posted into the db after it was posted. A shite way to do it, but it would have sufficed. So basically, how can I recursively go through multiple arrays to determine if the data is there, and if it is put it into the database, piece by piece? Here’s the code I’ve got thus far – with the stuff that’s not needed taken out:[code]<?phpif((isset($_POST['AddAnswer'])) && ($_POST['AddAnswer'] == 'Add Answer')){$SurveyID = $_POST['SurveyID'];$AnswerName = $_POST['AnswerName'];$AnswerType = $_POST['AnsTypes'];$Label = $_POST['Label'];//EXPLODES THE HIDDEN ANSWER FIELD INTO AN ARRAY SO WE CAN CHECK TO SEE IF THE VARIABLES EXIST//$answer_value = explode(",", $_POST['AnswerChecker']);$answer_value = $_POST['AnswerChecker'];//EXPLODES THE HIDDEN NUMERIC FIELD INTO AN ARRAY SO WE CAN CHECK TO SEE IF THE VARIABLES EXIST$numeric_value = explode(",", $_POST['NumericChecker']);//EXPLODES THE HIDDEN IMAGE FIELD INTO AN ARRAY SO WE CAN CHECK TO SEE IF THE VARIABLES EXIST$image_value = explode(",", $_POST['ImageChecker']);$count = 0; //COUNTER TO DETERMINE HOW MANY ROWS HAD VALUESforeach($answer_value as $ans_field){ foreach($numeric_value as $num_field){ foreach($image_value as $img_field){ if($_POST[$ans_field] != ""){ $AnsVal = $_POST[$ans_field]; $NumVal = $_POST[$num_field]; $ImgVal = $_POST[$img_field]; $add_value_sql = "INSERT INTO answer_values (answer_id, answer_value, numeric_value, image) VALUES ('$AnswerID', '$AnsVal', '$NumVal', '$ImgVal')"; $addvalue_result = mysql_query($add_value_sql, $connection) or die(mysql_error()); $count++; } } }}//CREATES THE SQL QUERY$add_answer_sql = "INSERT INTO answer_types (survey_id, answer_name, answer_type, label) VALUES ('$SurveyID', '$AnswerName', '$AnswerType', '$Label')";$add_answer_result = mysql_query($add_answer_sql, $connection) or die(mysql_error());}?><fieldset><legend>Add Answers</legend><form method="post" name="AddAnswer" action="<?php echo $_SERVER['PHP_SELF']; ?>"><dl><dt>*Survey Name:</dt><dd><select name="SurveyID"><?phpdo {?><option value="<?php echo $row_Available_Surveys['survey_id']?>" ><?php echo $row_Available_Surveys['survey_name']?></option><?php} while ($row_Available_Surveys = mysql_fetch_assoc($Available_Surveys));?></select></dd><dt>Answer Name:</dt><dd><input type="text" name="AnswerName" size="32" /></dd><dt>**Label:</dt><dd><input type="text" name="Label" size="32" /></dd><dt>Answer Type:</dt><dd><select name="AnsTypes"><?phpdo {?><option value="<?php echo $row_Available_Answer_Types['answer_type_id']?>" ><?php echo $row_Available_Answer_Types['answer_type']?></option><?php} while ($row_Available_Answer_Types = mysql_fetch_assoc($Available_Answer_Types));?></select></dd></dl><table><tr> <td>Active</td> <td>Num</td> <td>Answer Value(Displayed)</td> <td>Numeric Value</td> <td>Image</td></tr><?phpfor ($j = 1; $j <= 6; $j++){ echo "<tr>\n"; echo "<td><input type='checkbox' name='active$j' checked='checked' /></td>"; echo "<td>$j.</td>\n"; echo "<td><input type='text' name='ans_val$j' size='32' /></td>\n"; echo "<td><input type='text' name='num_val$j' value='0' size='32' /></td>\n"; echo "<td><input type='text' name='img_val$j' value='default.jpg' size='32' /></td>\n"; echo "</tr>\n";}?></table><input type="hidden" name="ActiveAnsChecker" value="active1,active2,active3,active4,active5,active6" /><input type="hidden" name="AnswerChecker" value="ans_val1,ans_val2,ans_val3,ans_val4,ans_val5,ans_val6" /><input type="hidden" name="NumericChecker" value="num_val1,num_val2,num_val3,num_val4,num_val5,num_val6" /><input type="hidden" name="ImageChecker" value="img_val1,img_val2,img_val3,img_val4,img_val5,img_val6" /><input type="hidden" name="AnswerID" value="<?php echo $AnswerID ?>" /><input type="submit" name="submit" value="Add Answer" /><input type="hidden" name="AddAnswer" value="Add Answer" /></form></fieldset>[/code]Now I do realize that the nested foreach does not work, but it's what I had thought to do. When I tried doing it like that it added around 100 entries into the db. Not very efficient or workable. The hidden fields are a tad monotonous and unneeded. Especially the ActiveAnsChecker and the checkboxes, but I was trying to give myself more hooks to figure out what the hell to do. You'll also notice that the checkboxes or active_ans are not referenced in this code, but that's mostly because I'm not sure what I was trying to do with them. Thanks for any help. I appreciate it much! Quote Link to comment Share on other sites More sharing options...
Steveo31 Posted March 7, 2006 Share Posted March 7, 2006 Can you post an example of an array and what values you want to put in the db? This sounds simple, but won't something like this work:[code]foreach($arr as $val){ if(!empty($val)){ //... etc }}[/code]I'm sure I'm missing something though. Quote Link to comment Share on other sites More sharing options...
Pneumonic Posted March 9, 2006 Author Share Posted March 9, 2006 No you're example will not work, although I appreciate you taking the time to look at my question. The array, that I'm retrieving is as such.[code]<?phpfor ($j = 1; $j <= 6; $j++){ echo "<tr>\n"; echo "<td>$j.</td>\n"; echo "<td><input type='text' name='ans_val$j' size='32' /></td>\n"; echo "<td><input type='text' name='num_val$j' value='0' size='32' /></td>\n"; echo "<td><input type='text' name='img_val$j' value='default.jpg' size='32' /></td>\n"; echo "</tr>\n";}?></table><input type="hidden" name="AnswerChecker" value="ans_val1,ans_val2,ans_val3,ans_val4,ans_val5,ans_val6" /><input type="hidden" name="NumericChecker" value="num_val1,num_val2,num_val3,num_val4,num_val5,num_val6" /><input type="hidden" name="ImageChecker" value="img_val1,img_val2,img_val3,img_val4,img_val5,img_val6" /><input type="hidden" name="AnswerID" value="<?php echo $AnswerID ?>" /><input type="submit" name="submit" value="Add Answer" /><input type="hidden" name="AddAnswer" value="Add Answer" />[/code]Now I'm trying to take the array of AnswerChecker, which have the values of ans_val$j (where $j is the placeholder for the current loop number), and check to see if it has a value. You see, if only 3 of the 6 ans_val fields are filled out, I only want to add the 3 fields to the database, not all 6, but I still need to retrieve the rest of the data from that row in the table (which the values of are num_val$j and img_val$j) so it can be inserted into the database as well. So I'm trying to find a way to script it so that it can go through the array, determine where the end of the actual data is, and then put the data into the database. I go attempted doing that as such:[code]<?phpif((isset($_POST['AddAnswer'])) && ($_POST['AddAnswer'] == 'Add Answer')){$SurveyID = $_POST['SurveyID'];$AnswerName = $_POST['AnswerName'];$AnswerType = $_POST['AnsTypes'];$Label = $_POST['Label'];//EXPLODES THE HIDDEN ANSWER FIELD INTO AN ARRAY SO WE CAN CHECK TO SEE IF THE VARIABLES EXIST//$answer_value = explode(",", $_POST['AnswerChecker']);$answer_value = $_POST['AnswerChecker'];//EXPLODES THE HIDDEN NUMERIC FIELD INTO AN ARRAY SO WE CAN CHECK TO SEE IF THE VARIABLES EXIST$numeric_value = explode(",", $_POST['NumericChecker']);//EXPLODES THE HIDDEN IMAGE FIELD INTO AN ARRAY SO WE CAN CHECK TO SEE IF THE VARIABLES EXIST$image_value = explode(",", $_POST['ImageChecker']);$count = 0; //COUNTER TO DETERMINE HOW MANY ROWS HAD VALUESforeach($answer_value as $ans_field){ foreach($numeric_value as $num_field){ foreach($image_value as $img_field){ if($_POST[$ans_field] != ""){ $AnsVal = $_POST[$ans_field]; $NumVal = $_POST[$num_field]; $ImgVal = $_POST[$img_field]; $add_value_sql = "INSERT INTO answer_values (answer_id, answer_value, numeric_value, image) VALUES ('$AnswerID', '$AnsVal', '$NumVal', '$ImgVal')"; $addvalue_result = mysql_query($add_value_sql, $connection) or die(mysql_error()); $count++; } } }}//CREATES THE SQL QUERY$add_answer_sql = "INSERT INTO answer_types (survey_id, answer_name, answer_type, label) VALUES ('$SurveyID', '$AnswerName', '$AnswerType', '$Label')";$add_answer_result = mysql_query($add_answer_sql, $connection) or die(mysql_error());}?>[/code]Unfortunately, the nested foreach ends up adding 100 answers or so. Not what I'm looking for. BTW, I am inserting data into 2 tables. The array is for the table answer_values, whereas the other data goes into answer_types. I hope I've been more clear in specifying what I am actually trying to do. Thanks for any help folks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.