PHP中for循环内的用户输入

时间:2013-10-17 10:57:09

标签: javascript php html arrays excel

好。我几乎得到了它。我有一个循环问题。我无法让PHP端静坐并等待用户响应。我尝试isset,它仍然继续。我在这里粘贴我的代码。希望没关系。 excel电子表格从基本上是row = 4作为你的多个选择,然后你选择,它找到你选择的列并连续下去询问下一行问题......我很抱歉我知道这是noob小镇,但我真的很感激反馈。

<HTML>
<HEAD>
<title>
</title>
</head>

    <body>

    <?php

    // include class file
    include 'Excel/reader.php';

    // initialize reader object
    $excel = new Spreadsheet_Excel_Reader();

    // read spreadsheet data
    $excel->read('Question-Flow.xls');    

    $x = 4; //Row
    $y = 0; //Column @ 0 for 1st iteration

    $questions = array();  //Stores questions and ends in 'STOP'

    //Iterates down a row until $cell='SUBMIT'
    do {

            //Populates $questions() and iterates until $cell='STOP'
            do {

                $y++;

                $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';

                if ($cell){

                    //Populates $questions array with active cells
                    array_push($questions, $cell);

                }

            } while ($cell != 'STOP');

          for ($i=0; $i < count($questions)-1; $i++){

                    //echo "<input type=radio name=$i value=\"".$questions[$i]."\">".$questions[$i]."<br>";
                    ?>
                <HTML><BODY>
                    <form action="index.php" method="post">
                    <input type=submit name=choice value=<?php echo $questions[$i]; ?> style="width: 100px; height: 100px;"><br>
                    </form>                
                    </BODY></html>
                <?php

                //Move $cell back one from 'STOP'
                $y--;
                $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';

                $choice = $_POST['choice'];

                if(isset($_POST['choice'])){
                    echo $choice;
                    echo $cell;
                    echo $x;
                    echo $y;
                }
          }

            while($choice != $cell){

                //Iterate back to find $choice
                $y--;

                $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';

            }

            //Moves to next row of Questions
            $x++;

            //Populates $cell with 'SUBMIT' or prompts next question
            $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';

    } while ($cell != 'SUBMIT');

    ?>


    </body>
</html>

我迷路了。我是PHP,JavaScript,HTML的新手,请原谅这个愚蠢的问题。我已经使用excel文件中的字符串填充了这个数组。印刷品看起来像

Array ( [0] => Question1 [1] => Question2 [2] => Question3 [3] => STOP )

我需要将每个值的用户呈现为一个问题,让他们在他们之间做出选择。然后我需要循环回来并将excel中的新值(这些都已完成)输入到该数组中,并回来向他们询问新问题。我需要将他们的答案存储到他们选择的“单元格”的excel循环中。它就像一个分支的问题树。

如果我可以将某种类型的输入表单放入下面的for循环中,那么它是否正常工作?不会采取。

  for ($i=0; $i < count($questions); $i++){
      echo $i;  //echo'd just to see the count and it's correct
  }

2 个答案:

答案 0 :(得分:0)

Ashish,html表单和php文件之间的关系是使用脚本参数构建的。在现场创建表单是一步,但表单必须将答案发送回可以处理发送的变量的PHP脚本:在您的情况下,答案。之后,如果全部或部分答案存储在您发送到php脚本的参数中,则可以使用for循环将它们全部复制。

您需要使用POST方法生成的表单(由php或static html生成),例如:http://w3schools.com/php/showphp.asp?filename=demo_form_post

这里有一个像欢迎演示的PHP脚本:http://w3schools.com/php/php_forms.asp

如果您使用相同的脚本进行表单生成和发布(使字段值等于您收到的POST变量),它可能看起来更具交互性。对于高级行为,你至少应该通过关于php,html的w3schools.com简介课程,然后再学习一些javascript。

答案 1 :(得分:0)

使用 foreach 循环,如下所示

foreach($questions as $qst){
    echo "<input type=radio name=q1 value=\"".$qst."\">".$qst."<br>";
}

用户可以根据单选按钮选择问题。