Defining variables
2 posts by 1 authors in: Forums > CMS Builder
Last Post: April 16, 2009 (RSS)
By rjbathgate - April 16, 2009 - edited: April 16, 2009
Hi again,
Sorry for the vague subject, couldn't think how to explain it.
On a form, I have a number of Questions, and Answers which are submitted.
It's done like this:
Where $counter is a simple counter going up +1 for each question, so the answer to question 4, would be:
<input name="answer4">
I've done it like this as the number of questions can vary (list table in CMS).
So I have that working fine :)
It's just the results page, where I need to pull/define each variable from the _POST.
i.e
So I need it to do this:
FOREACH COUNTER/ANSWER
$answerXXX = Trim(stripslashes($_POST['answerXXX']));
END FOREACH
I'm thinking we can control this foreach by defining the total number of records ($totalRecords), and running the foreach for every number from 1 up to $totalRecords.
So we get:
I just can't work out how to write a FOREACH statement which says foreach number from 1 up to $totalRecords.
Also I'd need to inject $counter into the XXX string of:
I hope all that makes sense!
Cheers
Just a thought, instead of having to run a foreach between 1 and $totalRecords, could you do a loop, starting from 1 going up 1 each time, and breaking as soon as it finds a blank question number...
i.e. IF !$questionXXX, BREAK.
Just a thought. I don't know!
Sorry for the vague subject, couldn't think how to explain it.
On a form, I have a number of Questions, and Answers which are submitted.
It's done like this:
FOREACH QUESTION
Question
<input name="answer<?php echo $counter ?>">
END FOREACH
Where $counter is a simple counter going up +1 for each question, so the answer to question 4, would be:
<input name="answer4">
I've done it like this as the number of questions can vary (list table in CMS).
So I have that working fine :)
It's just the results page, where I need to pull/define each variable from the _POST.
i.e
$answerXX = Trim(stripslashes($_POST['answerXX']));
So I need it to do this:
FOREACH COUNTER/ANSWER
$answerXXX = Trim(stripslashes($_POST['answerXXX']));
END FOREACH
I'm thinking we can control this foreach by defining the total number of records ($totalRecords), and running the foreach for every number from 1 up to $totalRecords.
So we get:
$answer1 = Trim(stripslashes($_POST['answer1']));
$answer2 = Trim(stripslashes($_POST['answer2']));
$answer3 = Trim(stripslashes($_POST['answer3']));
$answer4 = Trim(stripslashes($_POST['answer4']));
etc... until we hit $totalRecords
I just can't work out how to write a FOREACH statement which says foreach number from 1 up to $totalRecords.
Also I'd need to inject $counter into the XXX string of:
$answerXXX = Trim(stripslashes($_POST['answerXXX']));
I hope all that makes sense!
Cheers
Just a thought, instead of having to run a foreach between 1 and $totalRecords, could you do a loop, starting from 1 going up 1 each time, and breaking as soon as it finds a blank question number...
i.e. IF !$questionXXX, BREAK.
Just a thought. I don't know!
Re: [rjbathgate] Defining variables
By rjbathgate - April 16, 2009 - edited: April 16, 2009
Sorted it!!!
Using LOOP...
code incase anyone's interested:
Using LOOP...
code incase anyone's interested:
<?php
$loop=0;
do
{
$loop++;
$looptwo = $loop;
$answerloop = "answer" . $looptwo;
@$answernum = Trim(stripslashes(strtolower($_POST[$answerloop])));
$correctloop = "correct" . $looptwo;
@$correctnum = Trim(stripslashes(strtolower($_POST[$correctloop])));
if ($answernum == $correctnum)
{
$score = $score + 1; }
}
while ($loop<$quantity);
?>