Re: PHP assistance needed --

by Mark Leaver <dramoth(at)lwds.net>

 Date:  Mon, 21 Oct 2002 21:11:04 +0100
 To:  "Davies,
Elizabeth H." <EHDavies(at)West.com>,
"HWG-technique Mailing List (E-mail)" <hwg-techniques(at)hwg.org>
 In-Reply-To: 
  todo: View Thread, Original
Hi Elizabeth,

I would assume that you are using 10 pages of 10 answers per page?

Based on this assumption, I would do it like this.

I would make a form array for each page and then for each of the paired 
radio buttons on the page make each pair the same array element with a 
hidden form element holding the name of the question.

i.e.
         <.input type="Radio" name="FormArray[1] value="1"> a
         <.input type="Radio" name="FormArray[1] value="2"> b

That should take care of breaking down the questions into managable chunks. 
With that structure, the PHP for loop to create the strings should be nice 
and easy.

But looking at your table structure, it looks very inefficient having to 
record each answer on a separate tupple in the table. It would be much 
easier in the long run to make your table structure more like this:

create table survey_astd (
survey_id       mediumint(8) not null auto_increment primary key,
person  mediumint(8) not null,
answer1 tinyint(1) not null,
.
.
.
answer70        tinyint(1) not null
);

that way, you dont have to search multiple rows just to get the answer of a 
single question for a single person.

When the person answers the first question, you will do an initial insert 
of the persons user number (if that what the p_key element is) and the 
first 10 answers.

your first insert statement will look something like this

         insert into survey_astd values ('', $user_number, FormArray[0], 
FormArray[1], FormArray[2], FormArray[3], FormArray[4], FormArray[5],
                 FormArray[6], FormArray[7], FormArray[8], FormArray[9]);

Then when it comes time to do the update for each of the subsequent pages, 
you will be constructing a string containing the names of the answer fields 
and the answer for the question, like this

         $k = $page*10;
         for ($i=0;$i<count(FormArray);$i++) {
                 $string = "answer".$k."='".$FormArray[$i]."'";
                 $k++;
         }

then the update sql statement is going to be

         update survey_astd set $string where person = $user_number;

Or you can do it like this and do 10 separate updates with each loop

         $k=$page*10;
         for ($i=0;$i<count(FormArray);$i++) {
                 $result = mysql_query("update survey_astd set 
answer$k=$FormArray[$i]");
                 $k++;
         }

Hopefully this rather complex answer will help you out. If you want any 
more help with this, give me an email off the list with anymore questions.

Cheers,

Mark


At 11:25 21/10/2002 -0500, Davies, Elizabeth H. wrote:
>My graphics-oriented brain is not seeing something... I simply can't 
>handle array's
>
>I have a 70 question survey and am passing an array of answers, 10 at a 
>time....(radio buttons with the name of q_ans1, q_ans2 etc)
>
>Questions...
>1. a or b
>2. a or b
>...
>10. a or b ... form submit
>
>I need to insert the current 10 into a database table, with a table layout of:
>--- p_key (the person submitting the survey)
>--- q_num (the question number)
>--- q_ans (the answer, either a or b)... and then continue to the next 10.
>
>I just can't handle the split... ::: sigh :::
>
>I got this far (X is passed from the form and will start at 1 on the first 
>page):
>
>for (i=X;i<=X+10;i++) {
>
>         SOME METHOD TO SPLIT UP THE ANSWERS from variables q_ans1, 
> q_ans2, q_ans3....q_ans70
>
>         $query = "INSERT INTO is_survey_astd
>                 (p_key, q_num, q_ans)
>         VALUES
>                 ('$p_key','$i','$q_ans')";
>}
>
>First off the FOR statement blows up with
>  Parse error: parse error, unexpected '=', expecting ';' in 
> /wic/intranet/istrain_survey/survey.php on line 12 (this is the for 
> statement line)
>
>Can anyone assist? point me to an online example of this? keep me from 
>having my brain melt? ::: whimper :::
>
>Elizabeth Davies

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA