2013-10-17 67 views
0

好的。我幾乎明白了。我有一個循環問題。我無法讓PHP方面坐下來等待用戶的迴應。我嘗試isset,它仍然繼續。我在這裏粘貼我的代碼。希望沒關係。 excel電子表格基本上是從第4行開始的,作爲你的多個選擇,然後你選擇它,找到你選擇的列,然後連續詢問下一行問題...我很抱歉,我知道這是新手鎮但我真的很感激反饋。用戶輸入PHP中的for循環

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

回答

0

Ashish,html表單和php文件之間的關係是使用腳本參數構建的。現場創建表單只需一步,但表單必須將答案發回給可以處理髮送的變量的php腳本:在你的情況下,答案。之後,如果全部或部分答案都存儲在發送給php腳本的參數中,則可以使用for循環將其全部複製。

您需要的形式(通過PHP或靜態HTML生成)與POST方法如:http://w3schools.com/php/showphp.asp?filename=demo_form_post

而且PHP腳本像歡迎示範這裏:http://w3schools.com/php/php_forms.asp

如果您使用相同的腳本表單生成和發佈(使字段值等於您收到的POST變量),它可能看起來更具交互性。對於高級行爲,您至少應該通過w3schools.com簡短課程關於php,html以及之後的一些javascript。

+0

請不要提供w3schools的參考資料。那裏的信息經常過時或無效。 – webnoob

0

使用foreach循環如下

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

用戶可以選擇在單選按鈕的方面的問題。