2017-01-26 112 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