Coding issue (PHP)

Users who are viewing this thread

Elfy

Grandmaster Knight
Hi!

I'm working on adding a feature to survey website, that allows the maker of the survey see the results of the survey according to
other results - so that one chart would show what women have answered, one what men, one what children, one what adults etc.

This works like this: There are 0-4 questions, which are special "background info" questions. Each of these has maximum of 5 answers. These values are passed to PHP file in following form: $_POST['tausta(number of the background question, 0-4)']. The values of these variables are numbers 0-4, which are the answer possiblities.

Then there are the regular questions, which are passed like this: $_POST['kys'(number of the question)] The values of these variables are numbers 0-4, which are the answer possiblities, too.

There are 1-40 questions, but the numbers are between 0-39. I have the regular Answers-column in the database, which has all the answers in this form: 0***0***0***0*0***|||0***0***0***0***0 where *** separates the number of answers in one answer choice, and ||| separates the questions.

I also have 20 columns made for the answers according to background, which are named pv0-pv19. The numbers mean the background answers: the answers of background question one, answer three are in pv2, question three answer one in pv10 etc.

So my problem is, how do I get the answers in the correct column correctly, when there are multiple background questions. I have spent like 10 hours working on this without finding a viable solution. As detailed descriptions (or code) as possible, please.
 
Have read this through a few times now and still am slightly confused 'bout what you're after.

Could you write an example of something that would be in the code and the database?

For example, the "$_POST['tausta(number of the background question, 0-4)']". Are there actual parentheses in the variable name? As in, $_POST['tausta(1,3)']? Or is that a function call? If the latter number is the choice the user makes on the form, how in the **** do you even get it there? Are the question options entered as numbers or are they radio buttons?

And the database, "0***0***0***0*0***|||0***0***0***0***0". Are the 0's the answers? And what's the significance of the "*" instead of the "***" everywhere else?

I think in this case it might be better to describe what you want to happen in general instead of asking how to make something specific you think might be a good way to do it happen.

Would help too if you could post whatever code you have done already.

Or in a nutshell: Please provide more, less vague information!
 
The * should be *** too  :oops:

The parentheses are simply my remark, the variables are like $_POST['tausta0'], that's what I
tried to tell in the parentheses. The values are passed with radio buttons, yes.

I'll post my current code and more detailed description when I get to my PC today.

Edit: here is the current code which handles the "normal" questions, which are not relative to any background information:

Code:
<?php
$vanhat_tulokset = HaeTietokannasta('vastaukset','kyselyt','id',$id); // Gets the old results

$vantuks = explode('|||',$vanhat_tulokset); // creates an array of the question answers


while($numero < $maara){ // while number < amount of questions ($maara is defined earlier)
${'ehdot' . $numero} = explode('***',$vantuks[$numero]); // creates variables of question answers
$kysymys = htmlspecialchars($_POST['kys' . $numero],ENT_QUOTES); // what the user has answered
if($kysymys !== "0"){ // this is because PHP considers string '0' empty
if(empty($kysymys)){ // make sure that the user has answered all questions

echo 'Unohdit vastata johonkin/joihinkin kysymyksiin. Ole hyvä ja paina selaimesi Takaisin-näppäintä varmistaaksesi, että vastaat kaikkiin kysymyksiin.'; 
exit; // if not, print this (Finnish)

}
}
if(is_numeric($kysymys)){ // has to be a number, but is not integer


if($kysymys === '0'){ // if the user has answered 0 (1), then we add one to the results 

${'ehdot' . $numero}[0] = ${'ehdot' . $numero}[0] + 1; // to the right place, naturally

}
if($kysymys === '1'){ // and these next lines work the same, but with different answers

${'ehdot' . $numero}[1] = ${'ehdot' . $numero}[1] + 1;

}


if($kysymys === '2'){

${'ehdot' . $numero}[2] = ${'ehdot' . $numero}[2] + 1;

}


if($kysymys === '3'){

${'ehdot' . $numero}[3] = ${'ehdot' . $numero}[3] + 1;

}


if($kysymys === '4'){

${'ehdot' . $numero}[4] = ${'ehdot' . $numero}[4] + 1;

}


$numero = $numero + 1;
}
}


$nummer = 0;

while($nummer < $maara){  // again while number is less than amount of questions
$muuttuja = ${'ehdot' . $nummer}; //variables we defined and added 1 to right place earlier

${'ehdot' . $nummer} = implode('***',$muuttuja); // Implode them back to string

$nummer = $nummer + 1;

}
$numer = 0;
$jono = "";
while($numer < $maara){ // do I need to say this again?

$jono = $jono . ${'ehdot' . $numer} . '|||'; // And make the values back to full string
$numer = $numer + 1; // 
}

$palaute = HaeTietokannasta('palaute','kyselyt','id',$id); // this is irrelevant, it just
if (!empty($_POST['palaute'])){ // puts possible feedback about the survey to the database
$palaute = htmlspecialchars($_POST['palaute'],ENT_QUOTES) . '|' . $palaute;
}

mysql_query("UPDATE kyselyt SET vastaukset='$jono' WHERE id='$id'"); // put the values in
mysql_query("UPDATE kyselyt SET palaute='$palaute' WHERE id='$id'");
echo 'Olet vastannut kyselyyn onnistuneesti.'; // "You have completed the survey     successfully"


?>
 
Back
Top Bottom