SeeSee
SeeSee

Reputation: 57

Compare array values with input values

I have an PHP file that has an array:

$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,
                )

It is an example of credit card informations, the key is credit card number. So I have to go trough it and compare the values with input values from a form. My HTML form:

  <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>

First I need to check if the card number entered exists in my array, if it does than I need to compare the rest of entered values(name and card type ). How do I do it? I'm a beginner to all this so be kind and help me please!

I had an idea but of course it,s not working:

  $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!";

    }
     }
 }
 }

Upvotes: 0

Views: 81

Answers (3)

buzlee
buzlee

Reputation: 53

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!";
}

Upvotes: 1

rescobar
rescobar

Reputation: 1305

Try this, as @Rajdeep Paul told you, you can use array_key_exists(), I have created a function and I have added the new value to the balance.

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!";
}

Output:

$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!

Upvotes: 1

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

First I need to check if the card number entered exists in my array, if it does than I need to compare the rest of entered values(name and card type ).

Use array_key_exists() function to check if the card number exists in the $cards array or not. And then refactor your entire business logic in the following way,

// 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";
}

Upvotes: 0

Related Questions