【发布时间】:2016-03-11 09:34:36
【问题描述】:
而且我不确定为什么我的代码不起作用。它现在所做的是将用户输入检查到下一个随机生成的城市到国家,而不是检查当前城市。到目前为止,这是我的 PHP 文件: LINK TO WEBPAGE
<!DOCTYPE html>
<html>
<?php
$cityToCountry = array();
$file = fopen('world_capitals.txt', 'r');
while ( !feof($file) )
{
$line = fgets($file, 2048);
$delimiter = "\t";
$data = str_getcsv($line, $delimiter);
$cityToCountry[$data[0]] = $data[1];
}
fclose($file);
/*
foreach($cityToCountry as $key => $value) {
echo "$key is at $value<br>";
}
*/
$randomCity = array_rand($cityToCountry);
echo $randomCity . " is the capital city of what country?<br><br>";
?>
<form method="post" action="">
<input type="text" name="data"/>
<input type="submit" name="check" value="check"/>
</form>
<?php
if (isset($_POST['check'])) {
$input = $_POST["data"];
echo $input . "<br>";
echo $randomCity . "<br>";
if ($input == $cityToCountry[$randomCity]) {
echo "Correct";
} else {
echo "False";
}
}
?>
</html>
它给出“假”的结果。我在这里做错了什么?已经坚持了几个小时。提前谢谢!
【问题讨论】:
-
您能粘贴
$cityToCountry和$randomCity的值吗?需要确定$randomCityisset in$cityToCountry