Php for循环声明变量,名称中包含两个灵活数字

时间:2014-04-02 13:26:49

标签: php

我尝试使用for循环或任何其他循环(我不知道如何在php中执行),这将帮助我声明与用户定义的一样多的变量。这些变量来自textareas,名称为:answer11,answer12,answer13(当然是问题1),然后我回答21,answer22,answer23,answer24等等,因为用户定义了很多问题。

for($j=1; $j<$size; $j++) {

  if($_POST['question'][$j] != "")
  {
    //these can be unlimited. This is only for question1 as you can see, but how to add for more question
    $answer11 = $_POST['answer11'];
    $answer12 = $_POST['answer12'];
    $answer13 = $_POST['answer13'];

    $answer[$j] = '<input type="radio" name="ans11">'.$answer11.'<br/>';
    $answer[$j] .= '<input type="radio" name="ans12">'.$answer12.'<br/>';
    $answer[$j] .= '<input type="radio" name="ans13">'.$answer13.'<br/>';

    $print .= $j.') '.$question[$j].'<br/>'.$answer[$j].'<br/><br/>';
  }

}

循环应该如何?我相信这很容易,但我看不到解决方案:(

1 个答案:

答案 0 :(得分:0)

您可能应该使用数组来存储答案。我不确定这些答案是什么,但你可以做同样的事情并适应你的需要:

for($j=1; $j<$size; $j++) 
{
    if($_POST['question'][$j] != "") 
    {
        $post_answer = $_POST['answer'];

        foreach ($post_answer as $k => $ans)
        {
            $answer .= '<input type="radio" name="answer['.$k.']">'.$ans.'<br/>';
        }

        $print .= $j.') '.$question[$j].'<br/>'.$answer.'<br/><br/>';
    }
}