0

嗯所以我更新了我的代码。对于那些不知道的人,我正在尝试制作一个表格,让用户从下拉列表中回答下一任总统是谁。如果正确,就交给下一任总统;如果错了,他们必须继续猜测,但他们的尝试会增加。出于某种原因,我的表格无法识别下一任总统是谁。我不认为$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";

    }
    ?>
4

2 回答 2

0

整个函数有点混乱..为什么是total,currentqpres本地定义的?如果您调用该函数两次,此值永远不会改变。您必须在会话中存储此类服务器端的信息。president函数的参数是否有特殊原因?对我来说没有多大意义。用字符串(不是索引)初始化 qpres 也有点奇怪。

$president = array(
    "George Washington",
    //...
);

function handleForm($president) {
    $qpres = $president[$_SESSION["qpres"]];
    echo "$qpres is the next president";
    if($_GET['answerpres'] == $_SESSION["qpres"]) {
        $_SESSION['total']++;
        $_SESSION["correct"]++;
        $qpres = $president[$qpres];
        $legend = "Correct! Who comes after $qpres?";
        displayForm($president, $legend, $_SESSION["qpres"], $_SESSION["correct"], $_SESSION['total']);
    } else {
        $_SESSION['total']++;
        $legend = "Incorrect. Who comes after $qpres?";
        displayForm($president, $legend, $_SESSION["qpres"], $_SESSION["correct"], $_SESSION['total']);
    }
}
于 2013-02-11T00:53:59.960 回答
0

我相信您遇到的问题是因为您对“George Washington”进行了硬编码。如果尚未设置,您可能只想设置 $qpres = "George Washington"。也许做到:

if (empty($qpres)){
    $qpres = "George Washington";
}
于 2013-02-11T00:54:49.070 回答