0

我在这里遇到了 PHP 的一个大问题,我需要你的帮助。我在 PHP 中用替代方法创建问题。每个问题将有 4 个选项。问题将被插入到一个名为 questions 的表中,所有备选方案将被插入到另一个名为 Alternatives 的表中。每个问题都有自己的 id,甚至每个备选方案也都有自己的 id。但是每组备选方案也会有问题 id,以了解哪个备选方案属于哪个问题。然后我将它们选为单选按钮。例如,如果我们有两个问题,例如

  1. 澳大利亚首都叫什么名字?(假设这个问题的 id 编号为 1)
    A)悉尼
    B)新德里
    C)堪培拉
    D)麦米
    以上所有这些替代方案都有自己的 id,在这种情况下,问题 id 为 1。

  2. 什么是 89 + (3*9) (假设这个问题的编号为 3)
    A)116
    B)117
    C)115
    D)112

在这种情况下,上面所有这些替代方案都将有自己的 id 和问题 id 为 3。

3. 一个物体从 100 米高的塔顶掉下。t秒后其离地高度为100-4.9t2 m。掉落后 2 秒失败的速度有多快?
A)98m
B) 78m
C)56m
D)没有一个
等等......

这是我的代码:

if (isset($_REQUEST['submit']) && $_REQUEST['submit'] != "") {
    $questions = $_POST['questions']; 

    $alternatives = $_POST['alternatives']; 


    if(isset($_POST['questions']) && $_POST['questions'] != '')
    {

        foreach($questions as $q) {

            if($q !='') {

                $sql1 = " INSERT INTO questions (question) ".
                        " VALUES ('".$q."'); ";
                if (!db_query($sql1)) 
                    $errors['err']='Unable to create question';
                $question_id[] = mysql_insert_id(); 
            }

        }

        foreach($question_id AS $q_id) { // I think here is the problem but I don't know how to solv'

            foreach($alternatives as $alt){


                if($alt !=''){
                    $sql =  " INSERT INTO alternatives (alternative, question_id) ".
                                " VALUES ('".$alt."', ".$q_id."); ";
                                if (!db_query($sql))
                    $errors['err']='Unable to create alternatives';
                 }

            }

        }

    }

}




<form action="test.php" method="POST" enctype="multipart/form-data"><br /><br /><br />
for($i=1; $i<=10; ++$i){
$i.") "?><input type="text" name="questions[]" value="" size="100"/><br />
<br /><p>Alternatives:</p><br /> 
for($j=1; $j<=4; ++$j){
<input type="text" name="alternatives[]" value="" /><br /> } }
<input type="submit" name="submit" value="Send"></input>
 <input class="button" type="reset" value="Reset" />

</form>

对不起我的代码..在这里写起来很困难。但正在发生的是:

所有 12 个备选方案都插入了 3 次,每次都得到相同的 question_id。我的意思是前 12 个备选方案得到 question_id 1 ,然后再次插入这 12 个备选方案,它们得到 question_id = 2 ,依此类推....我想要的是前四个备选方案得到 question_id 1,第二个问题的备选方案将有 question_id nr 2 等等。我不知道如何解决它。

4

2 回答 2

3

Why don't you add the question number to the name of the input as a key:

<form action="test.php" method="POST" enctype="multipart/form-data"><br /><br /><br />
<?php 
for($i=1; $i<=10; ++$i){?>
<input type="text" name="questions[<?php echo $i;?>]" value="" size="100"/><br />
<br /><p>Alternatives:</p><br /> 
<?php for($j=1; $j<=4; ++$j){?>
<input type="text" name="alternatives[<?php echo $i;?>][]" value="" /><br />
<?php } } ?>
<input type="submit" name="submit" value="Send"></input>
 <input class="button" type="reset" value="Reset" />

</form>

that way you'll end with two very easy to loop arrays such as

  ["questions"]=>
  array(10) {
    [1]=>
    string(4) "What's the name of Australian capital city?"
    [2]=> ....

  ["alternatives"]=>
  array(10) {
    [1]=>
    array(4) {
      [0]=>
      string(6) "Sidney"
      [1]=>
      string(4) " New Delhi"
      [2]=>
      string(4) " Canberra"
      [3]=>
      string(2) "Maimi"
    }
    [2]=>
    array(4) {... and so on
于 2012-07-15T01:21:55.160 回答
0

foreach($question_id AS $q_id)遍历您的问题列表。

在该循环内,foreach($alternatives as $alt)迭代您的答案列表。因此,您将浏览整个答案列表,每个问题一次。

相反,您需要将每个答案与其适用的问题相关联。最简单的方法可能是一次插入一个问题。有多种其他方法可以做到这一点,但是如果没有看到您用来提交这些方法的界面,很难说什么方法会起作用。

于 2012-07-15T00:53:00.913 回答