用数据库值定义数组。

[英]PHP - defining arrays with database values


I've been tasked to produce a web app that asks the user for an answer(option) to a specific question - performing different actions depending on the option chosen. Each question is stored in a database, along with each question's 7 options. For example, "what was your favourite food when you were younger?" could have the options: Cookies, cakes, pies, etc.

我的任务是生成一个web应用程序,它要求用户给出一个特定问题的答案(选项)——根据所选择的选项执行不同的操作。每个问题都存储在数据库中,以及每个问题的7个选项。例如,“你年轻时最喜欢的食物是什么?”可以有选择:饼干、蛋糕、馅饼等等。

i've actually managed to write the script to define each value using 1D and 2D indexed arrays using mysqli_fetch_array. However, when I try to view the data stored in each question and answer array using for loops, I get a strange problem... All of the questions display, but only the first question's answers display.

实际上,我已经使用mysqli_fetch_array编写了使用1D和2D索引数组来定义每个值的脚本。然而,当我尝试查看存储在每个问题中的数据并使用for循环来回答数组时,我遇到了一个奇怪的问题…所有的问题都显示出来,但只有第一个问题的答案显示出来。

Here is my code:

这是我的代码:

<?php
    session_start();
    $con = mysqli_connect("server","username","password","db");

    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }


    $arr = array();
    $ans = array();
    $i = 0;
    $c = 0;
    $q_sql = "SELECT Question FROM Question";

    $question = mysqli_query($con,$q_sql);

    while($q = mysqli_fetch_array($question)){
        $arr[$i] = $q['Question'];

        $a_sql = "SELECT Answer FROM Answer WHERE QuestionID = ".($i+1);
        $answer = mysqli_query($con,$a_sql);

        while($a = mysqli_fetch_array($answer)){
            $ans[$i][$c] = $a['Answer']; // array(0 => $i, 1 => $a);
            $c++;
        }
        $i++;
    }

    mysqli_close($con);
?>

HTML:

HTML:

<!DOCTYPE html>

<html>
    <head>
        <title></title>
    </head>
    <body>
    <?php

        for ($i=0; $i < sizeof($arr); $i++){
            print "<br/>".$arr[$i];
            for ($c=0; $c < sizeof($ans[$i]); $c++){
                print "<br/>".$ans[2][$c];          
            }
        }

    ?>
    </body>
</html>

If any more info is needed, please ask. I have no idea why only one set of answers is displayed, so any help would be appreciated.

如果需要更多的信息,请提问。我不知道为什么只显示一组答案,所以任何帮助都会被欣赏。

1 个解决方案

#1


0  

Suppose each question has 5 answers. During the first iteration of the Question query, $i = 0 and $c runs from 0 to 4. During the second iteration, $i = 1 and $c runs from 5 to 9, so you filled in $ans[1][5]. But your loop that prints the answers expects the second index always to run from 0 to sizeof($ans[$i])-1.

假设每个问题有5个答案。在问题查询的第一次迭代中,$i = 0和$c从0运行到4。在第二次迭代期间,$i = 1和$c从5到9运行,所以您填充了$ans[1][5]。但是,打印答案的循环希望第二个索引总是从0运行到sizeof($ans[$i])-1。

You need to reset $c to 0 before the inner loop, not before the outer loop.

您需要在内部循环之前将$c重置为0,而不是在外部循环之前。

But there's also no reason to use multiple queries and nested loops, you should join them into a single query.

但是也没有理由使用多个查询和嵌套循环,您应该将它们连接到一个查询中。

$sql = "SELECT q.ID, Question, Answer
        FROM Question AS q
        LEFT JOIN Answer AS a ON a.QuestionID = q.ID
        ORDER BY q.ID";
$result = mysqli_query($con, $sql);
$last_qid = null;
$i = -1;
while ($row = mysqli_fetch_assoc($result)) {
    if ($row['ID'] != $last_qid) {
        $i++;
        $arr[$i] = $row['Question'];
        $ans[$i] = array();
    }
    if ($row['Answer'] !== null) { // skip null value if there were no answers
        $ans[$i][] = $row['Answer'];
    }
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/06/09/af697db9bb1127dcf69f2c5ae79a45ca.html



 
  © 2014-2022 ITdaan.com 联系我们: