有效地从页面中获取许多单选按钮的值

efficiently getting value of many radio buttons from page

我的应用程序是一个数学测验,我必须从此页面获取许多单选按钮的值以用于计算分数和诸如此类的东西。

截至目前,我为每个单选按钮指定了十个单独的变量,但这对我来说听起来像是蛮力编码。有没有人有更有效的方法来做到这一点?下面是我当前的代码。

//these variables hold which radio button the user selected
$answerChoice1 = $_POST['test1']; //pulls value of radio button named test 1
$answerChoice2 = $_POST['test2'];
$answerChoice3 = $_POST['test3'];
$answerChoice4 = $_POST['test4'];
$answerChoice5 = $_POST['test5'];
$answerChoice6 = $_POST['test6'];
$answerChoice7 = $_POST['test7'];
$answerChoice8 = $_POST['test8'];
$answerChoice9 = $_POST['test9'];
$answerChoice10 = $_POST['test10'];

$questionID1 = $_POST['theId1']; //pulls the 'bid' of the question asked
$questionID2 = $_POST['theId2'];
$questionID3 = $_POST['theId3'];
$questionID4 = $_POST['theId4'];
$questionID5 = $_POST['theId5'];
$questionID6 = $_POST['theId6'];
$questionID7 = $_POST['theId7'];
$questionID8 = $_POST['theId8'];
$questionID9 = $_POST['theId9'];
$questionID10 = $_POST['theId10'];

$sqlAnswer1 = "SELECT * FROM `math` WHERE `bid` = \"" . $questionID1 . "\"  "; //sql statement for selecting the questions that were generated
$sqlAnswer2 = "SELECT * FROM `math` WHERE `bid` = \"" . $questionID2 . "\"  "; //on the page
$sqlAnswer3 = "SELECT * FROM `math` WHERE `bid` = \"" . $questionID3 . "\"  ";
$sqlAnswer4 = "SELECT * FROM `math` WHERE `bid` = \"" . $questionID4 . "\"  ";
$sqlAnswer5 = "SELECT * FROM `math` WHERE `bid` = \"" . $questionID5 . "\"  ";
$sqlAnswer6 = "SELECT * FROM `math` WHERE `bid` = \"" . $questionID6 . "\"  ";
$sqlAnswer7 = "SELECT * FROM `math` WHERE `bid` = \"" . $questionID7 . "\"  ";
$sqlAnswer8 = "SELECT * FROM `math` WHERE `bid` = \"" . $questionID8 . "\"  ";
$sqlAnswer9 = "SELECT * FROM `math` WHERE `bid` = \"" . $questionID9 . "\"  ";
$sqlAnswer10 = "SELECT * FROM `math` WHERE `bid` = \"" . $questionID10 . "\"  ";

看到这段代码是多么笨拙和丑陋吗?同样在这些 sql 语句下面,我对每个语句都有十个单独的查询。有什么想法吗?

无需更改您的标记,您只需创建一个循环来读取和处理变量:

for ($i = 1; $i <= 10; $i++)
{
  $answerChoice = $_POST["test$i"];
  $questionID = $_POST["theId$i"]; //pulls the 'bid' of the question asked

  $sqlAnswer = "SELECT * FROM `math` WHERE `bid` = \"" . $questionID . "\"  ";

  // Execute the other stuff within the loop as well, if it is per question.  
}

注意:我将代码段重点放在按原样优化您的代码段上,因为这是您要求的,但它仍然容易受到 SQL 注入的攻击。如果有人伪造了一个在 $_POST["theId1"] 中包含一些奇怪内容的请求,那么这些内容将成为您 SQL 查询的一部分,并且可能会损坏您的数据库!

关于数组

正常

// You can declare an array like this:
$answerChoices = array();
for ($i = 1; $i <= 10; $i++)
{
  // Using the bracked notation, you can append an item to an array. 
  // You don't even have to specify an index. It will just start at 0.
  $answerChoices[] = $_POST["test$i"];

  // You *can* specify an index, though. Using -1 here to be in sync with the 0-based array above.
  $questionIDs[$i - 1] = $_POST["theId$i"]; //pulls the 'bid' of the question asked

  // You don't have to declare the array first. If you just add an item, PHP will 
  // create the array for you.
  $sqlAnswers[] = "SELECT * FROM `math` WHERE `bid` = \"" . $questionID . "\"  ";
}

您可以像这样遍历它们:

foreach ($sqlAnswers as $i => $sqlAnswer) {

  echo $sqlAnswer; // This variable is generated in the foreach loop.

  echo $answerChoices[$i]; // For the other arrays you can use the index.
}

当然,像上面那样混合访问数组的方式可能会造成混淆。幸运的是,您仍然可以使用整数索引来使用普通的 for 循环:

for ($i = 0; $i < count($answerChoices); $i++)
{
  echo $answerChoices[$i];
  echo $sqlAnswers[$i];
}

嵌套

你也可以嵌套数组。您可以使用字符串作为索引。这样,您可以更好地构建数据,从而更容易在其余代码中使用它。

$quizData = array();
for ($i = 1; $i <= 10; $i++)
{
  $questionID => $_POST["theId$i"];

  // Not the best way, but for now a simple way to make your code a little 
  // safer, if question id is indeed an integer.
  $questionID = (int)$questionID;

  // Question, answer and SQL are stored in an array with string keys. 
  // That array is stored in $quizData, so it will be an array of arrays.
  $quizData[] = array(
    'answerChoice' => $_POST["test$i"],
    'questionID' => $questionID,
    'sqlAnswer' => "SELECT * FROM `math` WHERE `bid` = \"" . $questionID . "\"  ";
}

现在您有一个嵌套数组,其中每个项目都是一个数组,其中包含包含数据的命名索引。

检查这个循环:

print_r($quizData); // Show the whole array in all its glory. ;)

foreach ($quizData as $questionData)
{
  print_r($questionData); // All data for a single question

  echo $questionData['questionID']; // Just one property of a question.
}

下一个选项是对象,但我认为我正在扩大问题的范围。 ;)

如果您想更好地控制变量,我建议您将 POST 变量放在一个数组中。

$answerChoice = array($_POST["test1"], $_POST["test2"], etc...) 然后使用 for loop 遍历数组

for ($i = 1; $i <= 10; $i++){
$answerChoice[i]
//more code
}

这让您可以轻松地选择某些答案