2013-02-11 25 views
0

嗯所以我更新了我的代碼。對於那些不知道的人,我試圖製作一個表格,讓用戶回答下一任總統的下落。如果正確的話,下一任總統;如果錯了,他們必須繼續猜測,但他們的嘗試會增加。出於某種原因,我的表格並沒有意識到下一任總統將成爲誰。我不認爲$ questionpres(最初是「喬治華盛頓」)正在通過處理($總統)。尋找下一任總統 - PHP陣列和驗證

<?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"; 

    } 
    ?> 
+0

這將是有益的,如果你將包括你的實際輸出和總統變量的內容傳遞。 – starshine531 2013-02-11 00:58:40

+0

我看到你用更多的信息更新了這個,但你仍然缺乏你得到的輸出。例如,它是否總是讓「約翰亞當斯」成爲下一任總統?無論答案是什麼,它總會記錄錯誤的答案嗎?這些是正確調試這些所需的詳細信息。 – starshine531 2013-02-11 02:07:29

+0

它總是說「不正確,誰是喬治華盛頓之後的下一任總統?」即使我選擇了「John Adams」。當我再次回答時,櫃檯停止遞增,就好像表單在第一次提交後沒有提交一樣。 – 2013-02-11 04:17:14

回答

0

整機功能有點混亂..爲什麼totalcurrentqpres本地定義?如果您調用該函數兩次,此值永遠不會改變。您必須將此服務器端的信息存儲在會話中。 爲什麼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']); 
    } 
} 
0

我相信你遇到的問題是因爲你硬編碼「喬治華盛頓」。如果尚未設置,您可能只想設置$ qpres =「George Washington」。也許做到這一點:

if (empty($qpres)){ 
    $qpres = "George Washington"; 
}