This is a discussion on form with dynamic questions within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi all, I've a mysql table where i stored a list of questions. My php script executes a query ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi all,
I've a mysql table where i stored a list of questions. My php script executes a query which yields a different number of questions every time; i used the loop function to create an input field for each question in the form; and dynamically referencing each input name with the question number i.e. question 1 is called q1, question 2 named q2, and so on. The data is submitted using POST. I then try to retrieve all the answers by doing a loop and using something like echo '$_POST['q'.$n.'']'; except the syntax doesnt seem to work. Can anyone please tell me what am doing wrong - or a better way of doing this. Many thanks T |
|
|||
|
toffee wrote:
> Hi all, > > I've a mysql table where i stored a list of questions. My php script > executes a query which yields a different number of questions every time; i > used the loop function to create an input field for each question in the > form; and dynamically referencing each input name with the question number > i.e. question 1 is called q1, question 2 named q2, and so on. > The data is submitted using POST. I then try to retrieve all the answers by > doing a loop and using something like echo '$_POST['q'.$n.'']'; except the > syntax doesnt seem to work. > Can anyone please tell me what am doing wrong - or a better way of doing > this. > > Many thanks > > T > > Why not use an array? Then you can use $_POST["q[$n]"]; -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|||
|
["Followup-To:" header set to comp.lang.php.]
toffee wrote: > the syntax doesnt seem to work. > echo '$_POST['q'.$n.'']'; echo $_POST['q' . $n]; or $index = 'q' . $n; echo $_POST[$index]; -- File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot |
|
|||
|
On Sat, 7 Oct 2006 03:51:19 +0100, "toffee" <toffee@toffee.com> wrote:
>Hi all, > >I've a mysql table where i stored a list of questions. My php script >executes a query which yields a different number of questions every time; i >used the loop function to create an input field for each question in the >form; and dynamically referencing each input name with the question number >i.e. question 1 is called q1, question 2 named q2, and so on. >The data is submitted using POST. I then try to retrieve all the answers by >doing a loop and using something like echo '$_POST['q'.$n.'']'; except the >syntax doesnt seem to work. >Can anyone please tell me what am doing wrong - or a better way of doing >this. > >Many thanks > >T > when your query generates a random number of question your are going to loop through the result set to display each questioin. The name field for each question should be set up in array format like this <form> {... this is loop area <p> <?= $question ?> </p> <input type="test" name="answer[]" value="" > the key thing is the [] then under each question would the the question id in a hidden input like this... <input type="hidden" name="questionID[]" value="<?= $questionID ?>" > } // end of loop <input type="submit" name="Submit" value="Submit" /> </form> Now when you submit the form you loop through the hidden array to get the values for each mathing question, fromthere you can figure out what to do with the answers $answer = $_POST['answer']; // answer array $questionID = $_POST['questionID']; // questionID array for($x=0; $x<sizeof($questionID); $x++) { $userAnswer = $answer[$questionID[$x]]; ..... do something with answer and it's associated with the question id $questionID[$q] } This is the general idea, read through the logic a few times and you can probably figure it out |
|
|||
|
Thanks G and all,
that was exactly what i was trying to do and the route i initially went down. With the results to every question, i was trying to create a multidimensional array which i would then append to a mysql table so i could then run a query to work out how many points the respondant got depending on his answers. - i didnt manage to successfully create a multidimension array with each sub-array holding one row to be appended. - i tried to write each row to text file and then load it into mysql table - but i dont have the right privileges - the only working solution i have right now is to count how many questions were answered, loop through all of them and run an insert query for every row even though its working, it feels a bit long winded. is there maybe a more efficient way to do this ? T "Gleep" <Gleep@Gleep.com> wrote in message news:b8fgi2hsbik4vq1on5v54sj9hvhrsrbdo5@4ax.com... > On Sat, 7 Oct 2006 03:51:19 +0100, "toffee" <toffee@toffee.com> wrote: > > >Hi all, > > > >I've a mysql table where i stored a list of questions. My php script > >executes a query which yields a different number of questions every time; i > >used the loop function to create an input field for each question in the > >form; and dynamically referencing each input name with the question number > >i.e. question 1 is called q1, question 2 named q2, and so on. > >The data is submitted using POST. I then try to retrieve all the answers by > >doing a loop and using something like echo '$_POST['q'.$n.'']'; except the > >syntax doesnt seem to work. > >Can anyone please tell me what am doing wrong - or a better way of doing > >this. > > > >Many thanks > > > >T > > > > when your query generates a random number of question your are going to loop through the result set > to display each questioin. The name field for each question should be set up in array format like > this > > <form> > {... this is loop area > <p> <?= $question ?> </p> > <input type="test" name="answer[]" value="" > > > the key thing is the [] > > then under each question would the the question id in a hidden input like this... > > <input type="hidden" name="questionID[]" value="<?= $questionID ?>" > > > } // end of loop > > <input type="submit" name="Submit" value="Submit" /> > > </form> > > > Now when you submit the form you loop through the hidden array to get the values for each mathing > question, fromthere you can figure out what to do with the answers > > > $answer = $_POST['answer']; // answer array > $questionID = $_POST['questionID']; // questionID array > for($x=0; $x<sizeof($questionID); $x++) { > $userAnswer = $answer[$questionID[$x]]; > .... do something with answer and it's associated with the question id $questionID[$q] > } > > > This is the general idea, read through the logic a few times and you can probably figure it out > > > |
![]() |
| Thread Tools | |
| Display Modes | |
|
|