Form Submission array handling

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MTupid
Forum Newbie
Posts: 1
Joined: Mon Mar 16, 2009 11:00 am

Form Submission array handling

Post by MTupid »

I have a form that is a survey which has a variable number of questions in it. There are two types of question in it, rating questions (under which a variable number of people are rated) and text questions.

So for each rating question I need to insert into a table called FeedbackRating

FeedbackID
QuestionID passed through as RQuest (and then an incrementing number on the end)
LecturerID passed through as LecsID (" same as above, eg first will be LecsID1, LecsID2...)
RatingValue passed through as Rat (" same as above Rat1, Rat2, Rat3)

Code: Select all

<?php
 
$FeedbackID = mysql_insert_id();&#160; # fetch the newly generated id
 
 
 
 $RatingQuests = array();
 
for($i=1;$i<=40;$i++)&#160; # create array with 1-40 elements
if(isset($_POST['Rat'.$i]) and ($_POST['Rat'.$i] > ""))
$RatingQuests[] = '('.$FeedbackID.',"'.($_POST['Rat'.$i]).'")';
$values = implode(',',$RatingQuests);
 
$sql2 = "INSERT INTO FeedbackRating (FeedbackID,RatingValue) VALUES $values";
 
?>
What this does is cycle through all the Rat posts and inserts them all fine. However I can't work how how to expand this to do the RQuest and LecsID.

I thought it might be something like this however it doesn't work

Code: Select all

 
<?php
$FeedbackID = mysql_insert_id();&#160; # fetch the newly generated id
 
$RatingQuests = array();
 
for($i=1;$i<=40;$i++)&#160; # create array with 1-40 elements
if(isset($_POST['RQuest'.$i]) and ($_POST['RQuest'.$i] > "")) && isset($_POST['LecsID'.$i]) and ($_POST['LecsID'.$i] > "")) && isset($_POST['Rat'.$i]) and ($_POST['Rat'.$i] > "")))
$RatingQuests[] = '('.$FeedbackID.',"'.($_POST['RQuest'.$i]).'","'.($_POST['LecsID'.$i]).'","'.($_POST['Rat'.$i]).'")';
$values = implode(',',$RatingQuests);
 
$sql2 = "INSERT INTO FeedbackRating (FeedbackID,QuestionID,LecturerID,RatingValue) VALUES $values";
 
?>
 
Hope I have explained this well enough anyway. Any thoughts would be great.

As a side note QuestionID and LecturerID are numerical and RatingValue can either be text or numerical

Thank you
Post Reply