PHP 中 for 循环中的用户输入


User input within for loop in PHP

好的。 我差点就明白了。 我遇到了循环问题。 我无法让 PHP 端坐以待毙并等待用户响应。 我尝试 isset,它仍然继续。 我在这里粘贴我的代码。 希望没事。 它从基本上 row=4 中提取的 excel 电子表格作为您的多项选择,然后您选择,它会找到您选择的列并向下一行询问下一行问题...... 对不起,伙计们,我知道这是菜鸟小镇,但我真的很感谢反馈。

<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
  }

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

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

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

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

按如下方式使用foreach循环

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

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