うーん、コードを更新しました。知らない人のために、ユーザーが次の大統領が誰になるかをドロップダウンで答えるフォームを作ろうとしています。正しければ、次の大統領に。間違っている場合は、推測を続ける必要がありますが、試行は増加します。どういうわけか、私のフォームは次の大統領が誰になるかを認識していません. $questionpres (当初は "George Washington") が handleform($president) に渡っているとは思えません。
<?php
$president = array(
"George Washington"=>"John Adams",
"John Adams"=>"Thomas Jefferson",
"Thomas Jefferson"=>"James Madison",
"James Madison"=>"James Monroe",
"James Monroe"=>"John Quincy Adams",
"John Quincy Adams"=>"Andrew Jackson",
"Andrew Jackson"=>"Martin Van Buren",
"Martin Van Buren"=>"William Henry Harrison",
"William Henry Harrison"=>"John Tyler",
"John Tyler"=>"James K. Polk",
"James K. Polk"=>"Zachary Taylor",
"Zachary Taylor"=>"Millard Fillmore",
"Millard Fillmore"=>"Franklin Pierce",
"Franklin Pierce"=>"James Buchanan",
"James Buchanan"=>"Abraham Lincoln",
"Abraham Lincoln"=>"Andrew Johnson",
"Andrew Johnson"=>"Ulysses S. Grant",
"Ulysses S. Grant"=>"Rutherford B. Hayes",
"Rutherford B. Hayes"=>"James Garfield",
"James Garfield"=>"Chester A. Arthur",
"Chester A. Arthur"=>"Grover Cleveland",
"Grover Cleveland"=>"Benjamin Harrison",
"Benjamin Harrison"=>"Grover Cleveland",
"Grover Cleveland"=>"William McKinley",
"William McKinley"=>"Theodore Roosevelt",
"Theodore Roosevelt"=>"William Howard Taft",
"William Howard Taft"=>"Woodrow Wilson",
"Woodrow Wilson"=>"Warren G. Harding",
"Warren G. Harding"=>"Calvin Coolidge",
"Calvin Coolidge"=>"Herbert Hoover",
"Herbert Hoover"=>"Franklin D. Roosevelt",
"Franklin D. Roosevelt"=>"Harry S. Truman",
"Harry S. Truman"=>"Dwight D. Eisenhower",
"Dwight D. Eisenhower"=>"John F. Kennedy",
"John F. Kennedy"=>"Lyndon B. Johnson",
"Lyndon B. Johnson"=>"Richard M. Nixon",
"Richard M. Nixon"=>"Gerald R. Ford",
"Gerald R. Ford"=>"James Carter",
"James Carter"=>"Ronald Reagan",
"Ronald Reagan"=>"George H. W. Bush",
"George H. W. Bush"=>"William J. Clinton",
"William J. Clinton"=>"George W. Bush",
"George W. Bush"=>"Barack Obama",
"Barack Obama"=>"George Washington");
?>
<!DOCTYPE html>
<head>
<title>President Flashcards</title>
<style type="text/css">
body{ font-family: Verdana, sans-serif; }
fieldset {
height: 200px;
width: 290px;
background-image: url(whitehouse.jpg);
background-repeat: no-repeat;
}
legend {
background-color: gold;
width: 300px;
font-size: 18px;
}
select {
background-color: gold;
font-size: 18px;
height: 40px;
}
input.mybutton {
height:40px;
float:right;
font-size: 26px;
background-color:gold;
font-weight:bold;
font-family:arial;
position:relative;
bottom:-50px;
}
.status {
background-color: green;
color: gold;
position:relative;
bottom:-100px;
}
</style>
</head>
<body>
<pre>
<?php //print_r($_GET); ?>
</pre>
<?
if (isset($_GET['answerpres'])) {
handleForm($president);
} else {
$questionpres = "George Washington";
$legend="Who comes after $questionpres?";
displayForm($president, $legend, $questionpres, 0, 0);
}
?>
</body>
</html>
<?php
/* handleForm - this function handles a submitted form. It determines if the user
* selected the correct president.
* Argument - the array of president/next presidents
* Called by - main
* Calls - displayForm
* If the correct president is selected,
* increment total and correct,
* create the legend with the "correct" message
* call displayForm
* If the incorrect president is selected
* increment total only
* create the legend with the "incorrect" message
* call displayForm
*/
function handleForm($president){
echo "Next is $president[$questionpres]";
if($_GET['answerpres'] == $president[$questionpres]) {
$total++;
$correct++;
$questionpres = $president[$questionpres];
$legend = "Correct! Who comes after $questionpres?";
displayForm($president, $legend, $qpres, $correct, $total);
} else {
$total++;
$legend = "Incorrect. Who comes after $questionpres?";
echo "You selected".$_GET['answerpres'];
displayForm($president, $legend, $questionpres, $correct, $total);
}
}?>
<?php
/*
* displayForm - displays the input form
* Called by - handleForm
* Calls - makepresidentmenu
* Arguments - the array of president/next presidents
* - the legend for the form's fieldset
* - the president the user will be asked about
* - the number of correct answers the user has given
* - the total number of answers the user has given.
*/
function displayForm($president, $legend, $questionpres, $correct, $total){
?>
<fieldset style="background-img=url(whitehouse.jpg); width:640px; height:343px;">
<legend><? echo $legend ?></legend>
<form>
<? makepresidentmenu($president, $menuname, $currentpres); ?>
<input type = "submit" name = "formsubmitted" value = "Submit" /></form>
<div style="color:yellow; background:green; margin-top:100px; text-align:center; border:3px solid white;">
<? echo "You have $correct of $total correct."; ?>
</div>
</fieldset>
<? } ?>
<?php
/*
* makepresidentmenu - make a selection menu with all the presidents.
* Called by - displayForm
* Calls - ksort
* Arguments - the array of president/next presidents
* - the menu name
* - the current selection, defaults to NONE
* Note that the displayed name and the value are the keys of the array.
*/
function makepresidentmenu($president, $menuname, $currentpres = "")
{
ksort($president);
echo "<select name='answerpres'>";
//$key is current president. $value is next president
foreach ($president as $key => $value){
if ($currentpres == $key)
echo "<option value=\"$key\" selected> $key </option>\n";
else
echo "<option value=\"$key\"> $key </option>\n";
}
echo "</select>\n";
}
?>