Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中设置接受稍有错误答案的测验_Php_Html - Fatal编程技术网

在php中设置接受稍有错误答案的测验

在php中设置接受稍有错误答案的测验,php,html,Php,Html,所以最近我研究了PHP,并利用它设置了一个小测验。我的问题是如何让测验接受拼写稍有错误的答案?例如,如果我的测验问阿拉巴马州的首府是什么,有人把蒙哥马利拼错了一点,我怎么能让它仍然算作对的呢?我想对我的测验提出的所有问题都这样做,如果不让我知道的话,我希望我的问题措辞足够好。这是我目前正在使用的代码,是的,它被设置为三个文件,因为我发现将它放在一个文件中很复杂,我希望得到关于我拥有的多个文件的答案,如果可能的话,我不希望在一个页面上显示所有php代码的示例。代码: 列出州和首都的代码,不是完整列

所以最近我研究了PHP,并利用它设置了一个小测验。我的问题是如何让测验接受拼写稍有错误的答案?例如,如果我的测验问阿拉巴马州的首府是什么,有人把蒙哥马利拼错了一点,我怎么能让它仍然算作对的呢?我想对我的测验提出的所有问题都这样做,如果不让我知道的话,我希望我的问题措辞足够好。这是我目前正在使用的代码,是的,它被设置为三个文件,因为我发现将它放在一个文件中很复杂,我希望得到关于我拥有的多个文件的答案,如果可能的话,我不希望在一个页面上显示所有php代码的示例。代码:

列出州和首都的代码,不是完整列表,而是示例:

<?php
$states = Array();
$capitals = Array();
$states[]="Alabama";
$capitals[]="Montgomery";
$states[]="Alaska";
$capitals[]="Juneau";
$states[]="Arizona";
$capitals[]="Phoenix";
$states[]="Arkansas";
$capitals[]="Little Rock";
$states[]="California";
$capitals[]="Sacramento";
$states[]="Colorado";
$capitals[]="Denver";
$states[]="Connecticut";
$capitals[]="Hartford";
$states[]="Delaware";
....etc
   ?>

测验基础的实际主代码:

<!DOCTYPE html>
<html>
<head>
<title>State capital quiz: ask</title>
</head>

<body>
<h1>State Capital Quiz </h1><p>
<?php
$saywhich=@$_GET['saywhich'];
if ($saywhich){
  include("statecapitals.php");
  $which=$_GET['which'];
  $choice=rand(0, sizeOf($states)-1);
   if ($which=='state') {
     $state = $states[$choice];
     print("What is the capital of $state?<br>");
     print("<form action='statecapquizcheck.php' method='get'>\n");
     print("<input type='text' name='capital'><br>\n");
     print("<input type='hidden' name='which' value=$which>\n");
     print("<input type='hidden' name='choice' value=$choice>\n");
     print("<input type='submit' value='Submit Answer'>");
     print("</form>\n");
   }   
else   {
     $capital = $capitals[$choice];
     print("$capital is the capital of which state?<br>");
     print("<form action='statecapquizcheck.php' method='get'>\n");
     print("<input type='text' name='state'><br>\n");
     print("<input type='hidden' name='which' value=$which>\n");
     print("<input type='hidden' name='choice' value=$choice>\n");
     print("<input type='submit' value='Submit Answer'>");
     print("</form>\n");
   }
}
else {
print("Choose form of question: do you want to be given the state or     the capital?<br>");
print("<form action='statecapquizask.php' method='get'>\n");
print("Ask <input type='radio' name='which' value='state'>State");
print(" <input type='radio' name='which' value='capital'>Capital\n");
print("<input type='hidden' name='saywhich' value='true'>\n");
print("<input type='submit' value='Submit choice'>");
print("</form>");
}
?>


</body>
</html>

州首府问答:提问
州首府问答比赛

我希望你有足够的时间独自玩这个,因为这是你学习的方式

如果你自己花了时间来破解这个问题,那么我将为你从中收集到的任何东西提交这个脚本。我采用了你的方案,并编写了一个程序脚本来进行试验;我对
soundex()
很好奇。是的,这是一个传呼机。。。我通常不会这样做,因为我会使用类,所以它是单元可测试的

这在顶部使用了PHP的约定;HTML在底部。这有助于实现逻辑和表示的分离

html可能工作,也可能不工作;这是未经测试的

FWIW,我发现
soundex
非常、非常、非常宽容的!因此,我使用了
变音

<?php

// initialize all variables

$get = $_GET; // this was done for testing and I'm too lazy to replace it ;)

$success = false;
$choose_which = true;
$supplied_term = '';
$which = '';
$checked_answer = false;
$answer_array = array();

// state => capital
$states = array(
  'Alabama'     => 'Montgomery',
  'Alaska'      => 'Juneau',
  'Arizona'     => 'Phoenix',
  'Arkansas'    => 'Little Rock',
  'California'  => 'Sacremento',
  // ... etc
  );

// capital => state
$capitals = array_flip($states);


function get_random($stateOrCapital) {

  $indexed_list = array_keys($stateOrCapital);
  $random_index = (rand(0, sizeOf($indexed_list)-1));
  return $indexed_list[$random_index];
}

function check_answer($submitted,$correct) {
  //return (soundex($submitted) == soundex($correct)); // too forgiving
  return (metaphone($submitted) == metaphone($correct));
}


// are we checking state or capital?  Decouple GET input, choose which list to use for answers
if( array_key_exists('which', $get)) {

  // don't show radio buttons
  $choose_which = false;

  switch($get['which']) {
    case 'capital':
      $which = 'capital';
      $answer_array = $capitals;
      break;

    default:
      $which = 'state';
      $answer_array = $states;

  }
} 


// show question?
if($choose_which == false) {

  $supplied_term = get_random($answer_array);

}

// check answer?
if( array_key_exists('check_answer', $get)) {

  $supplied_term = htmlentities($get['supplied_term']);
  $correct_answer = $answer_array[$supplied_term];
  $success = check_answer($get['answer'],$correct_answer);
  $checked_answer = true;


}

// default is to ask which type of question
// everything below could be separated into a view.

?><html>
<head>
<title>State capital quiz</title>
</head>

<body>
  <h1>State Capital Quiz </h1>

  <?php if($checked_answer): ?>
    <?php if($success): ?>
      <h2>CORRECT</h2>
    <?php else: ?>
      <h2>Sorry, try again...</h2>
    <?php endif; ?>
  <?php endif; ?>

  <?php if($choose_which || $success): ?>
    <h2>Which do you wish to answer?</h2>
    <form action='' method="get">
      <div><label>Capital <input type='radio' name='which' value='capital' /></label></div>
      <div><label>State <input type='radio' name='which' value='state' /></label></div>
      <div><input type='submit' /></div>
    </form>

  <?php else: ?>
    <p>What is the matching state or capital for <?= $supplied_term ?></p>

    <form action='' method='get'>
      <input type='hidden' name='check_answer' value='true'>
      <input type='hidden' name='which' value='<?= $which ?>'>
      <input type='hidden' name='supplied_term' value='<?= $supplied_term ?>'>

      <input type='text' name='answer'><br>
      <input type='submit' value='Submit Answer'>
    </form>
  <?php endif; ?>

州首府问答比赛
州首府问答比赛
对的
对不起,再试一次。。。
你想回答哪一个问题?
首都
陈述
匹配的州或首都是什么


我会考虑函数:SimulasyType,LevsTein,SouthExcel,你必须使用像SunDeX这样的模糊的东西。我还建议对州/首府使用关联数组,即,
$capital['Alabama']='Montgomery'。这将确保您的列表不会神秘地偏离一个。@TimMorton是的,我真的应该感谢您的建议,并请您或smith快速解释我将如何实现这些东西。我了解这些东西的用途,但我不知道如何实现类似的东西,这样我的程序就可以检查并继续我从来没有使用过soundex,所以这个链接是我最好的建议。我不确定你的答案是否正确。我向你们致敬,因为你们找到了扩音器,我正在玩soundex,并且已经安装好了,但正如你们所说的那个样,它实际上很糟糕。变音有着更现实的宽恕,我所需要做的就是用甲烷来代替soundex这个词,其他的一切都是以同样的方式设置的。我不确定是否将其标记为正确,因为我不知道您的代码是否有效,尽管您关于变音的建议是yetGlad认为它有帮助的最佳选择
<?php

// initialize all variables

$get = $_GET; // this was done for testing and I'm too lazy to replace it ;)

$success = false;
$choose_which = true;
$supplied_term = '';
$which = '';
$checked_answer = false;
$answer_array = array();

// state => capital
$states = array(
  'Alabama'     => 'Montgomery',
  'Alaska'      => 'Juneau',
  'Arizona'     => 'Phoenix',
  'Arkansas'    => 'Little Rock',
  'California'  => 'Sacremento',
  // ... etc
  );

// capital => state
$capitals = array_flip($states);


function get_random($stateOrCapital) {

  $indexed_list = array_keys($stateOrCapital);
  $random_index = (rand(0, sizeOf($indexed_list)-1));
  return $indexed_list[$random_index];
}

function check_answer($submitted,$correct) {
  //return (soundex($submitted) == soundex($correct)); // too forgiving
  return (metaphone($submitted) == metaphone($correct));
}


// are we checking state or capital?  Decouple GET input, choose which list to use for answers
if( array_key_exists('which', $get)) {

  // don't show radio buttons
  $choose_which = false;

  switch($get['which']) {
    case 'capital':
      $which = 'capital';
      $answer_array = $capitals;
      break;

    default:
      $which = 'state';
      $answer_array = $states;

  }
} 


// show question?
if($choose_which == false) {

  $supplied_term = get_random($answer_array);

}

// check answer?
if( array_key_exists('check_answer', $get)) {

  $supplied_term = htmlentities($get['supplied_term']);
  $correct_answer = $answer_array[$supplied_term];
  $success = check_answer($get['answer'],$correct_answer);
  $checked_answer = true;


}

// default is to ask which type of question
// everything below could be separated into a view.

?><html>
<head>
<title>State capital quiz</title>
</head>

<body>
  <h1>State Capital Quiz </h1>

  <?php if($checked_answer): ?>
    <?php if($success): ?>
      <h2>CORRECT</h2>
    <?php else: ?>
      <h2>Sorry, try again...</h2>
    <?php endif; ?>
  <?php endif; ?>

  <?php if($choose_which || $success): ?>
    <h2>Which do you wish to answer?</h2>
    <form action='' method="get">
      <div><label>Capital <input type='radio' name='which' value='capital' /></label></div>
      <div><label>State <input type='radio' name='which' value='state' /></label></div>
      <div><input type='submit' /></div>
    </form>

  <?php else: ?>
    <p>What is the matching state or capital for <?= $supplied_term ?></p>

    <form action='' method='get'>
      <input type='hidden' name='check_answer' value='true'>
      <input type='hidden' name='which' value='<?= $which ?>'>
      <input type='hidden' name='supplied_term' value='<?= $supplied_term ?>'>

      <input type='text' name='answer'><br>
      <input type='submit' value='Submit Answer'>
    </form>
  <?php endif; ?>