将多个PHP数组转换为一个数组,以便转换为Javascript



我正在创建一个PHP和Javascript的多项选择测验游戏。问题可以有2到6个答案。我一直在使用下面代码中的PHP查询来获取测验问题的数组以及每个问题的答案。

我现在想在Javascript中使用这些数据,并且想让它这样,每次用户单击下一个问题,一个问题和相应的答案显示。

我认为(但我不确定),这样做的最好方法是将数组组合成一个数组,其中问题数组包含答案数组,然后转换为javascript数组。

我真的应该这样做吗?如果是,我该怎么做?

$thisquizid = $_POST['quizidvalue'];
    for ($j = 0; $j < $questionrows; ++$j)
        {
            $questionresult = pg_fetch_array($questionquery); 
            $answerquery = pg_query($db_handle, "SELECT * FROM answer WHERE questionid = '$questionresult[0]'");
            $answerrows = pg_num_rows($answerquery);  
        for ($i = 0; $i < $answerrows; ++$i)
            {
                $answerresult = pg_fetch_array($answerquery); 
            }
    }

让我们从另一个角度来看这个问题。JS中的数据应该是什么样子?或者更确切地说,PHP应该返回什么JSON以使JS美观干净。比如:

var json = { // From PHP via AJAX
  "questions_and_answers": [
    {
      "question": "What is the airspeed velocity of an unladen swallow?",
      "answers": [
        "11 meters per second",
        "24 miles an hour",
        "African or European?"
      ]
    },
    {
      "question": "If a tree falls in an empty forest does it make a sound?",
      "answers": [
        "Unequivocally, yes.",
        "Unequivocally, no.",
        "It's all subjective."
      ]
    }
  ]
};
for(var i = 0, j = json.questions_and_answers.length; i < j; i++) {
  var question = json.questions_and_answers[i].question;
  var answers = json.questions_and_answers[i].answers.join("n");
  alert(question + "n" + answers);
}

在PHP中会是什么样子呢?由于不使用问答对象或某种ORM,传递json_encode的最简单方法是嵌套数组:

<?php
$questions_and_answers = array(
    array(
        "question" => "What is the airspeed velocity of an unladen swallow?",
        "answers" => array(
            "11 meters per second",
            "24 miles an hour",
            "African or European?"
        )
    ),
    array(
        "question" => "If a tree falls in an empty forest does it make a sound?",
        "answers" => array(
                "Unequivocally, yes.",
                "Unequivocally, no.",
                "It's all subjective."
            )
    )
);
// Wrap in a parent object, assuming you're delivering to JS with via AJAX.
echo json_encode(array("questions_and_answers" => $questions_and_answers));
?>

所以你的服务器端代码在正确的轨道上,但是你想要一个空数组填充当你循环你的数据库结果:

<?php
$thisquizid = $_POST['quizidvalue'];
    $json_output = array();
    for ($j = 0; $j < $questionrows; ++$j)
        {
            $questionresult = pg_fetch_array($questionquery); 
            $answerquery = pg_query($db_handle, "SELECT * FROM answer WHERE questionid = '$questionresult[0]'");
            $answerrows = pg_num_rows($answerquery);             
            $json_output[$j] = array( "question" => $questionresult["text"], "answers" => array());
        for ($i = 0; $i < $answerrows; ++$i)
            {
                $answerresult = pg_fetch_array($answerquery);
                $json_output[$j]["answers"][] = $answerresult["text"];
            }
    }
    echo json_encode(array("questions_and_answers" => $json_output));
?>

当然,你可以这样做,假设你不只是想要Javascript的一切(IE实际上是一个测验,人们会想要作弊,所以你需要在你的服务器上存储答案…)。

使用AJAX调用您的PHP页面,然后只需返回您需要显示为变量的值,JSON数组,无论什么…基本的AJAX调用看起来像这样:

var xmlhttpMain=new XMLHttpRequest();
function changeContent(){
    xmlhttpMain.open("GET", true);
    xmlhttpMain.send(null);
    xmlhttpMain.onreadystatechange=function(){
    if (xmlhttpMain.readyState==4 && xmlhttpMain.status==200){
            newStuff=xmlhttpMain.responseText;//This is your JSON or Array that you create in the PHP file... 
            //Then run whatever code you want here
    }
}

在你的PHP文件中,只需使用"echo($someString);"将你的信息拍摄回JS土地…

相关内容

  • 没有找到相关文章