2017-01-26 115 views
0

我有一個數組的PHP文件:比較數組值與輸入值

$cards = array 
    (
     1234=>array 
       ( 
        "type" => "Viza", 
        "owner" => "Petar Petrovic", 
        "balance" => 1000, 
       ), 
     5678=>array 
       (

        "type" => "Master Card", 
        "owner" => "Ivan Ivanovic", 
        "balance" => 20000, 
       ), 
    91011=>array 
       (

        "type" => "American Express", 
        "owner" => "Marko Markovic", 
        "balance" => 300000, 
       ), 
    121314=>array 
       (

        "type" => "Diners Club", 
        "owner" => "Veljko Veljkovic", 
        "balance" => 1000000, 
       ) 

這是信用卡信息的例子,關鍵是信用卡號碼。所以我必須通過它並將值與表單中的輸入值進行比較。我的HTML表單:

<form action="cards.php" method="post"> 
    <table align="center"> 
    <tr> 
     <td>Enter your name</td> 
     <td><input type="text" name="name"></td> 
    </tr> 

    <tr> 
    <td>Enter your card number</td> 
    <td><input type="text" name="cardno"></td> 
    </tr> 

    <tr> 
    <td>Enter your sum</td> 
    <td><input type="text" name="sum"></td> 
    </tr> 

    <tr> 
    <td>Select card type</td> 
    <td><select name="select"> 
     <option value="viza">Viza</option> 
     <option value="master">Master Card</option> 
     <option value="american">American Express</option> 
     <option value="diners">Diners Club</option> 
     </select></td> 
    </tr> 

    <tr> 
     <td></td> 
     <td><input type="submit" name="submit" value="SUBMIT"></td> 
    </tr> 

</table> 
</form> 

首先,我需要檢查,如果卡號進入存在於我的數組,如果它確實比我需要比較輸入的值(名稱和卡型)的其餘部分。我該怎麼做?我對所有這些都是初學者,所以請善待我,請幫助我!

我有一個想法,當然它的,不是工作:

$card_number=$_POST['cardno']; 
    $name = $_POST['name']; 
    $sum = $_POST['sum']; 
    $type = $_POST['select']; 

foreach($cards as $key=>$item){ 
    if ($item['type']== $type){ 
    if($item['owner']== $name){ 
     if($item['balance'] > $sum){ 

      $newbalance= $item['balance'] - $sum; 
      echo "Your new balance is:".$newbalance; 
     } 
     else if($item['balance'] < $sum){ 

      echo "You dont have enough sources on your account"; 

     } 
     else if ($item['owner']!== $name){ 

     echo "Ivalid name!"; 

     } 
    else if($item['type']!== $type){ 

     echo "Invalid card type!"; 

     } 
    else if($key !== $card_number){ 

     echo "Invalid card number!"; 

    } 
    } 
} 
} 

回答

0

首先,我需要檢查,如果卡號進入存在於我的數組,如果它確實比我需要比較輸入值的其餘部分(名稱和卡類型)。

使用array_key_exists()功能檢查$cards數組或不存在中的卡號。然後重構你的整個業務邏輯通過以下方式,

// First check if the card number exists or not 
if(array_key_exists($_POST['cardno'], $cards)){ 
    // Check rest of the details 
    if($cards[$_POST['cardno']]['name'] == $_POST['name']){ 
     if($cards[$_POST['cardno']]['type'] == $_POST['select']){ 
      if($cards[$_POST['cardno']]['balance'] >= $_POST['sum']){ 
       $newbalance= $cards[$_POST['cardno']]['balance'] - $_POST['sum']; 
       echo "Your new balance is:".$newbalance; 
      }else{ 
       echo "You dont have enough sources on your account"; 
      } 
     }else{ 
      echo "Invalid card type"; 
     } 
    }else{ 
     echo "Invalid name"; 
    } 
}else{ 
    echo "Invalid card number"; 
} 
+0

只是一個旁註:扣除,並顯示用戶的餘額後,您可能還需要更新'$ cards'陣列的餘額爲好。 –

+0

非常感謝,我會試試這個! – SeeSee

1

試試這個,因爲@Rajdeep保羅告訴你,你可以使用array_key_exists(),我創建了一個功能,我已經添加了新的價值,平衡。

function checkCreditCard($cards, $cardno, $name, $sum, $type) { 
    $bResult = (array_key_exists($cardno, $cards)) ? true : false; 
    if ($bResult) { 
    if ($cards[$cardno]['owner'] == $name && $cards[$cardno]['type'] == $type) { 
     if ($cards[$cardno]['balance'] >= $sum) { 
      $newBalance = ($cards[$cardno]['balance'] - $sum); 
      $cards[$cardno]['balance'] = $newBalance; 
      return "Your new balance is:" . $newBalance; 
     } else { 
      "You dont have enough sources on your account"; 
     } 
    } else { 
     return "Invalid name or card type"; 
    } 
    } 
    return "Invalid card number!"; 
} 

輸出:

$cards = array 
(
1234 => array 
    (
    "type" => "Viza", 
    "owner" => "Petar Petrovic", 
    "balance" => 1000, 
), 

echo checkCreditCard($cards, 1234, 'Petar Petrovic', '100', 'Viza'); 

Your new balance is:900 

echo checkCreditCard($cards, 000000, 'Petar Petrovic', '100', 'Viza'); 

Invalid card number! 
1
if(array_key_exists($card_number, $cards)){ 
    $cDetails = array_values($cards[$card_number]); 
    list($cType, $cOwner, $cBalance) = $cDetails; 

    if ($cOwner == $name) { 
     if ($cBalance > $sum) { 
      $newbalance = $cBalance - $sum; 
      echo "Your new balance is:" . $newbalance; 
     } else { 
      echo "You dont have enough sources on your account"; 
     } 
    } else { 
     echo "Ivalid name!"; 
    } 

    if ($cType != $type) { 
      echo "Invalid card type!"; 
     } 
} else { 
    echo "Invalid card number!"; 
} 
+0

當我嘗試此代碼的一切功能,但我總是有「卡類型無效!」在頁面上回應,即使它是一個正確的。 – SeeSee

+0

是的,看看這個: ​​<選擇name = 「選擇」> <期權價值= 「viza」> Viza <期權價值= 「主」>萬事達 <期權價值= 「美國」>美國運通 這是錯誤的,所有的值必須固定以匹配數組值。 – buzlee

+0

哦,是的,你是對的,這樣一個愚蠢的錯誤,謝謝! 我需要時刻思考這個計算機邏輯,我很容易犯錯誤......再次感謝你。 – SeeSee