2016-12-06 38 views
1

謝謝你的時間。修改現有的腳本以接受不同的參數

我想修改現有的PhP腳本來接受第23行的用戶輸入字符串。現在它只接受放入數組的數字。

<?php 
function isJewishLeapYear($year) { 
if ($year % 19 == 0 || $year % 19 == 3 || $year % 19 == 6 || 
    $year % 19 == 8 || $year % 19 == 11 || $year % 19 == 14 || 
    $year % 19 == 17) 
return true; 
    else 
return false; 
} 
function getJewishMonthName($jewishMonth, $jewishYear) { 
    $jewishMonthNamesLeap = array("Tishri", "Heshvan", "Kislev", "Tevet", 
          "Shevat", "Adar I", "Adar II", "Nisan", 
          "Iyar", "Sivan", "Tammuz", "Av", "Elul"); 
    $jewishMonthNamesNonLeap = array("Tishri", "Heshvan", "Kislev", "Tevet", 
           "Shevat", "", "Adar", "Nisan", 
           "Iyar", "Sivan", "Tammuz", "Av", "Elul"); 
if (isJewishLeapYear($jewishYear)) 
return $jewishMonthNamesLeap[$jewishMonth-1]; 
else 
return $jewishMonthNamesNonLeap[$jewishMonth-1]; 
} 

$jdNumber = gregoriantojd(12, 12, 2016); 
$jewishDate = jdtojewish($jdNumber); 
list($jewishMonth, $jewishDay, $jewishYear) = explode('/', $jewishDate); 
$jewishMonthName = getJewishMonthName($jewishMonth, $jewishYear); 
echo "<p>The Jewish date of death is $jewishDay $jewishMonthName $jewishYear</p>\n"; 
?> 

我想是該行

$jdNumber = gregoriantojd(12, 12, 2016); 

接受,而不是具體的數字,用戶輸入。我在想,你可以使用$ userinput,但是拋出了預期的3個字符串的錯誤,得到了一個。

再次,這不是我的特長,但我作爲項目的後端被投入混合。我不希望代碼爲我寫,只是朝着正確的方向推動。謝謝。

回答

0

有很多方法可以獲得用戶輸入,但我真的很喜歡jQuery-UI's datepicker。如果它被設置,那麼你可以用the DateTime classformat其作爲輸入輸出解析日期gregoriantojd

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>jQuery UI Datepicker - Default functionality</title> 
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
    <script> 
    $(function() { 
    $("#datepicker").datepicker(); 
    }); 
    </script> 
</head> 
<body> 

<form> 
    <p>Date: <input type="text" name="datepicker" id="datepicker"></p> 
    <p><input type="submit" value="Submit"></p> 
</form> 

</body> 
</html> 

<?php 
function isJewishLeapYear($year) { 
if ($year % 19 == 0 || $year % 19 == 3 || $year % 19 == 6 || 
    $year % 19 == 8 || $year % 19 == 11 || $year % 19 == 14 || 
    $year % 19 == 17) 
return true; 
    else 
return false; 
} 
function getJewishMonthName($jewishMonth, $jewishYear) { 
    $jewishMonthNamesLeap = array("Tishri", "Heshvan", "Kislev", "Tevet", 
          "Shevat", "Adar I", "Adar II", "Nisan", 
          "Iyar", "Sivan", "Tammuz", "Av", "Elul"); 
    $jewishMonthNamesNonLeap = array("Tishri", "Heshvan", "Kislev", "Tevet", 
           "Shevat", "", "Adar", "Nisan", 
           "Iyar", "Sivan", "Tammuz", "Av", "Elul"); 
if (isJewishLeapYear($jewishYear)) 
return $jewishMonthNamesLeap[$jewishMonth-1]; 
else 
return $jewishMonthNamesNonLeap[$jewishMonth-1]; 
} 

if(
    isset($_GET['datepicker']) && 
    $datetime = DateTime::createFromFormat('m/d/Y', $_GET['datepicker']) 
){ 
    $jdNumber = gregoriantojd($datetime->format('m'), $datetime->format('d'), $datetime->format('Y')); 

    $jewishDate = jdtojewish($jdNumber); 
    list($jewishMonth, $jewishDay, $jewishYear) = explode('/', $jewishDate); 
    $jewishMonthName = getJewishMonthName($jewishMonth, $jewishYear); 
    echo "<p>The Jewish date of death is $jewishDay $jewishMonthName $jewishYear</p>\n"; 
} 
0

有兩種方法。他們都要求你將你的$ userinput變量轉換成一個數組。

如果:$userinput = "12 6 2016";

你可以這樣做:

$input = explode($userinput, ' '); 
$jdNumber = gregoriantojd($input[0], $input[1], $input[2]); 

的另一種方式是傳遞數組作爲輸入列表:

$jdNumber = call_user_func_array('gregoriantojd', $input); 

沒有測試,但應該工作。