0

My PHP Code is:

    $userAnswers = array(   // you would get these values via $_POST["answers"]
        "s1" => array(
            "q1" => $_POST['S1Q1'],
            "q2" => $_POST['S1Q2']
        ),
        "s2" => array(
               "q1" => $_POST['S2Q1'],
               "q2" => $_POST['S2Q2'],
               "q3" => $_POST['S2Q3'],
               "q4" => $_POST['S2Q4']
        ),
        "s3" => array(
               "q1" => $_POST['S3Q1'],
               "q2" => $_POST['S3Q2'],
               "q3" => $_POST['S3Q3'],
               "q4" => $_POST['S3Q4'],
               "q5" => $_POST['S3Q5']
        ),
    );

    $q001 = $_POST['S1Q1'];
    $q002 = $_POST['S1Q2'];
    $q003 = $_POST['S2Q1'];
    $q004 = $_POST['S2Q2'];
    $q005 = $_POST['S2Q3'];
    $q006 = $_POST['S2Q4'];
    $q007 = $_POST['S3Q1'];
    $q008 = $_POST['S3Q2'];
    $q009 = $_POST['S3Q3'];
    $q010 = $_POST['S3Q4'];
    $q011 = $_POST['S3Q5'];

$savearray = array($q001, $q002, $q003, $q004, $q005, $q006, $q007, $q008, $q009, $q010, $q011);

$_SESSION['annual'] = $savearray;

I think It will be too tedious and time consuming if I have a lot of questions and I am entering the POST twice and was thinking of how to replace the second appearance of $_POST[] with something like the following:

$userAnswers[$s][$q]

Will that go in order starting from $q001 and forward? This way the $_POST[] is called only once.

So something like this:

$q001 = $userAnswers[$s][$q];
$q002 = $userAnswers[$s][$q];
$q003 = $userAnswers[$s][$q];
$q004 = $userAnswers[$s][$q];
$q005 = $userAnswers[$s][$q];
$q006 = $userAnswers[$s][$q];
$q007 = $userAnswers[$s][$q];
$q008 = $userAnswers[$s][$q];
$q009 = $userAnswers[$s][$q];
$q010 = $userAnswers[$s][$q];
$q011 = $userAnswers[$s][$q];

Thinking I might have to use a foreach statement?

foreach () { }
4

2 回答 2

1

自定义它以输入/输出结果。

<form action="" method="post">
<input name="s1[1]" type="text" value="">
<input name="s1[]" type="text" value="">
<input name="s2[1]" type="text" value="">
<input name="s2[]" type="text" value="">
<input name="jello" type="submit" value="send">
</form>



<?php        

foreach ($_POST['s1'] as $k=>$v)
{
echo "Qs1 answer $k is $v";

}

foreach ($_POST['s2'] as $k=>$v)
{
echo "Qs2 answers $k is $v";

 }
?>
于 2013-06-14T15:39:33.370 回答
1

像这样的工作:

$userAnswers["s1"]["q1"]

对于循环:

for ($sNumber = 1; $sNumber <= 2 ; $sNumber+1){
    for ($qNumber = 1; $qNumber <= 2 ; $qNumber+1){
        $sTemp = "s" + $sNumber;
        $qTemp = "q" + $qNumber;
        echo $userAnswers[$sTemp][$qTemp];
    }
}
于 2013-06-14T15:29:25.720 回答