Jump to content

[SOLVED] PHP Invalid argument error - can you help?!


Recommended Posts

Hi folks,

 

I have a PHP script on a server running PHP Version 4.4.6 that gives me this error:

 

Warning: Invalid argument supplied for foreach() in /home/simple/public_html/test/bank_validation.php on line 126

 

on the first line here:

 

foreach ($Data as $keyd => $data) {
         
         $name = $data[5];
         $branch = $data[6];
         $ok = $data[23];

         echo "<tr>\n";
         echo "<td>Name</td><td>$name</td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Branch</td><td>$branch</td>\n";
         echo "</tr>\n";
         if ($ok=="True") {
           echo "<tr>\n";
           echo "<td colspan=2>Account number validated OK</td>\n";
           echo "</tr>\n";
         }
         else {
           echo "<tr>\n";
           echo "<td colspan=2>Account number not valid</td>\n";
           echo "</tr>\n";
         }
         
         echo "</table>";
      
      }

 

Can anyone offer any suggestions as to what I should do?!

 

Cheers

 

Simon

I think so, yes. The docs say:

 

  /*

  $Data is populated with

    id

    seq

    accountumber

    sortcode

    name

    branch

    address1

    address2

    address3

    address4

    town

    county

    postcode

    phone

    fax

    direct_debits_allowed

    bacs_credits_allowed

    unpaid_cheque_claims_allowed

    life_office_debits_allowed

    building_society_credits_allowed

    dividend_interest_payments_allowed

    direct_debit_instructions_allowed

    ok - determines whether the account number is valid for the sortcode

    validation_error - set if there is a problem with the accountnumber

  */

Hi again,

 

Sorry - but I've played with virtually every option but still either get no result or that same error. I've confirmed the check is actually being made - it just won't show me the returned results!

 

I've attached the script if anyone can spot what I'm missing ;)

 

Cheers

 

Simon

 

[attachment deleted by admin]

Hi Huggie,

 

The whole script was provided by 'as is' by the company that offer the service . . .  but they admit themselves that they are not proficient PHP programers!

 

It seems to work in as much as it probes their database (they can see the request their end) but I can't get it to show the results!

 

Hope you can help.

 

Simon

Simon,

 

Try this:

 

<html>
<head>
  <title>Postcode Anywhere Bank Details Validation</title>
</head>
<body>

<?php

/*
  Note: This script has been developed for .php version 4.2.3.
  .php version 4.3 has more options to parse xml properly with slightly modified functions.

  This script will validate a bank sort number and an account number. Both are needed for the
  sample to work. If only sortcode validation is required then the GetBank method should be
  used instead of Validate
*/

// enter you account code and license key
$ACCOUNTCODE = "xxxx";
$LICENSEKEY = "xxxx";

// values entered are posted back for processing
$SortCode = $_POST['sortcode'];
$AccountNumber = $_POST['accountnumber'];

function ValidateBank($SortCode, $AccountNumber){

   global $ACCOUNTCODE,$LICENSEKEY;

   /* Build up the URL to send the request to. */
   $sURL = "http://services.postcodeanywhere.co.uk/csv.aspx?";
   $sURL .= "account_code=" . urlencode($ACCOUNTCODE);
   $sURL .= "&license_code=" . urlencode($LICENSEKEY);
   $sURL .= "&action=bacs_validate";
   $sURL .= "&sortcode=" . urlencode($SortCode);
   $sURL .= "&accountnumber=" . urlencode($AccountNumber);

   PrepareData($sURL);
}

function GetSortcode ($SortCode){

   global $ACCOUNTCODE,$LICENSEKEY;

   /* Build up the URL to send the request to. */
   $sURL = "http://services.postcodeanywhere.co.uk/csv.aspx?";
   $sURL .= "account_code=" . urlencode($ACCOUNTCODE);
   $sURL .= "&license_code=" . urlencode($LICENSEKEY);
   $sURL .= "&action=bacs_fetch";
   $sURL .= "&sortcode=" . urlencode($SortCode);

   PrepareData($sURL);
}

function PrepareData($filetoopen) {

   $handle = fopen($filetoopen, "r");

   while ($row = fgetcsv($handle, 1000)){
      $data[] = $row;
   }
   fclose($handle);
}

?>

  <h1>Bank Validation</h1>
  <form method="POST" action="bank_validation.php">

<?php

// If the sortcode and account number have been entered they are validated
if (isset($SortCode) && !empty($AccountNumber)) {

   ValidateBank($SortCode, $AccountNumber);

   echo "<table>\n";

   foreach ($data as $k => $v){
      if ($k != 0){

         $name = $data[$k][5];
         $branch = $data[$k][6];
         $ok = $data[$k][23];

         echo "<tr>\n";
         echo "<td>Name</td><td>$name</td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Branch</td><td>$branch</td>\n";
         echo "</tr>\n";
         if ($ok == "True") {
            echo "<tr>\n";
            echo "<td colspan=2>Account number validated OK</td>\n";
            echo "</tr>\n";
         }
         else {
            echo "<tr>\n";
            echo "<td colspan=2>Account number not valid</td>\n";
            echo "</tr>\n";
         }
      }
   }
   echo "</table>";
}


//elseif only the sortcode has been entered, the bank address is returned

elseif (empty($AccountNumber) && !empty($SortCode)) {

   GetSortcode($SortCode);

      echo "<table>\n";

      foreach ($data as $keyd => $data) {

         $name = $data["name"];
         $branch = $data["branch"];

         echo "<tr>\n";
         echo "<td>Name</td><td>$name</td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Branch</td><td>$branch</td>\n";
         echo "</tr>\n";

         echo "</table>";

      }

}


//else neither of the sortcode or account number have been entered, the form is simply displayed again

else {

   echo "<p>Enter a sortcode and account number below and click on the 'Validate' button to check the details.</p> <P>Alternatively, just enter a sortcode and the bank address details will be returned</p>\n";
   echo "Sortcode: <input name=\"sortcode\" id=\"sortcode\" value=\"\" type=\"text\" size=\"6\">\n";
        echo "Account number: <input name=\"accountnumber\" id=\"accountnumber\" value=\"\" type=\"text\" size=\"10\">\n";
   echo "<input id=\"btnValidate\" type=\"submit\" value=\"Validate\">\n";

}


?>

   </form>

</body>
</html>

 

I've only altered the ValidateBank() function and the PrepareData() function.  So if they work OK then I'll alter the GetSortcode() function too.

 

Regards

Huggie

Hi Huggie,

 

Thanks for looking at that for me. Unfortunately, I still get an error (but in a different place)

 

Warning: Invalid argument supplied for foreach() in /home/simple/public_html/test/bank_validation.php on line 79

 

It really doesn't like that foreach() does it ;)

 

Simon

Retry with this:

 

<html>
<head>
  <title>Postcode Anywhere Bank Details Validation</title>
</head>
<body>

<?php

/*
  Note: This script has been developed for .php version 4.2.3.
  .php version 4.3 has more options to parse xml properly with slightly modified functions.

  This script will validate a bank sort number and an account number. Both are needed for the
  sample to work. If only sortcode validation is required then the GetBank method should be
  used instead of Validate
*/

// enter you account code and license key
$ACCOUNTCODE = "xxxx";
$LICENSEKEY = "xxxx";

// values entered are posted back for processing
$SortCode = $_POST['sortcode'];
$AccountNumber = $_POST['accountnumber'];

function ValidateBank($SortCode, $AccountNumber){

   global $ACCOUNTCODE,$LICENSEKEY;

   /* Build up the URL to send the request to. */
   $sURL = "http://services.postcodeanywhere.co.uk/csv.aspx?";
   $sURL .= "account_code=" . urlencode($ACCOUNTCODE);
   $sURL .= "&license_code=" . urlencode($LICENSEKEY);
   $sURL .= "&action=bacs_validate";
   $sURL .= "&sortcode=" . urlencode($SortCode);
   $sURL .= "&accountnumber=" . urlencode($AccountNumber);

   $data = PrepareData($sURL);
   return $data;
}

function GetSortcode ($SortCode){

   global $ACCOUNTCODE,$LICENSEKEY;

   /* Build up the URL to send the request to. */
   $sURL = "http://services.postcodeanywhere.co.uk/csv.aspx?";
   $sURL .= "account_code=" . urlencode($ACCOUNTCODE);
   $sURL .= "&license_code=" . urlencode($LICENSEKEY);
   $sURL .= "&action=bacs_fetch";
   $sURL .= "&sortcode=" . urlencode($SortCode);

   $data = PrepareData($sURL);
   return $data;
}

function PrepareData($filetoopen) {

   $handle = fopen($filetoopen, "r");

   while ($row = fgetcsv($handle, 1000)){
      $data[] = $row;
   }
   fclose($handle);
   return $data;
}

?>

  <h1>Bank Validation</h1>
  <form method="POST" action="bank_validation.php">

<?php

// If the sortcode and account number have been entered they are validated
if (isset($SortCode) && !empty($AccountNumber)) {

   $data = ValidateBank($SortCode, $AccountNumber);

   echo "<table>\n";

   foreach ($data as $k => $v){
      if ($k != 0){

         $name = $data[$k][5];
         $branch = $data[$k][6];
         $ok = $data[$k][23];

         echo "<tr>\n";
         echo "<td>Name</td><td>$name</td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Branch</td><td>$branch</td>\n";
         echo "</tr>\n";
         if ($ok == "True") {
            echo "<tr>\n";
            echo "<td colspan=2>Account number validated OK</td>\n";
            echo "</tr>\n";
         }
         else {
            echo "<tr>\n";
            echo "<td colspan=2>Account number not valid</td>\n";
            echo "</tr>\n";
         }
      }
   }
   echo "</table>";
}


//elseif only the sortcode has been entered, the bank address is returned

elseif (empty($AccountNumber) && !empty($SortCode)) {

   $data = GetSortcode($SortCode);

      echo "<table>\n";

      foreach ($data as $keyd => $data) {

         $name = $data["name"];
         $branch = $data["branch"];

         echo "<tr>\n";
         echo "<td>Name</td><td>$name</td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Branch</td><td>$branch</td>\n";
         echo "</tr>\n";

         echo "</table>";

      }

}


//else neither of the sortcode or account number have been entered, the form is simply displayed again

else {

   echo "<p>Enter a sortcode and account number below and click on the 'Validate' button to check the details.</p> <P>Alternatively, just enter a sortcode and the bank address details will be returned</p>\n";
   echo "Sortcode: <input name=\"sortcode\" id=\"sortcode\" value=\"\" type=\"text\" size=\"6\">\n";
        echo "Account number: <input name=\"accountnumber\" id=\"accountnumber\" value=\"\" type=\"text\" size=\"10\">\n";
   echo "<input id=\"btnValidate\" type=\"submit\" value=\"Validate\">\n";

}


?>

   </form>

</body>
</html>

 

Regards

Huggie

Hi Huggie,

 

I can't see the actual file as I guess it's held somewhere remotely.

 

I've tried, True, TRUE and true and none appear to work.

 

The docs just say:

 

ok - Boolean  - Set if the account number and sort code combination are valid.

 

I tried changing the double == to just = but then everything passes (including the fails)!

 

 

OK, in that case try this:

 

<html>
<head>
  <title>Postcode Anywhere Bank Details Validation</title>
</head>
<body>

<?php

/*
  Note: This script has been developed for .php version 4.2.3.
  .php version 4.3 has more options to parse xml properly with slightly modified functions.

  This script will validate a bank sort number and an account number. Both are needed for the
  sample to work. If only sortcode validation is required then the GetBank method should be
  used instead of Validate
*/

// enter you account code and license key
$ACCOUNTCODE = "xxxx";
$LICENSEKEY = "xxxx";

// values entered are posted back for processing
$SortCode = $_POST['sortcode'];
$AccountNumber = $_POST['accountnumber'];

function ValidateBank($SortCode, $AccountNumber){

   global $ACCOUNTCODE,$LICENSEKEY;

   /* Build up the URL to send the request to. */
   $sURL = "http://services.postcodeanywhere.co.uk/csv.aspx?";
   $sURL .= "account_code=" . urlencode($ACCOUNTCODE);
   $sURL .= "&license_code=" . urlencode($LICENSEKEY);
   $sURL .= "&action=bacs_validate";
   $sURL .= "&sortcode=" . urlencode($SortCode);
   $sURL .= "&accountnumber=" . urlencode($AccountNumber);

   $data = PrepareData($sURL);
   return $data;
}

function GetSortcode ($SortCode){

   global $ACCOUNTCODE,$LICENSEKEY;

   /* Build up the URL to send the request to. */
   $sURL = "http://services.postcodeanywhere.co.uk/csv.aspx?";
   $sURL .= "account_code=" . urlencode($ACCOUNTCODE);
   $sURL .= "&license_code=" . urlencode($LICENSEKEY);
   $sURL .= "&action=bacs_fetch";
   $sURL .= "&sortcode=" . urlencode($SortCode);

   $data = PrepareData($sURL);
   return $data;
}

function PrepareData($filetoopen) {

   $handle = fopen($filetoopen, "r");

   while ($row = fgetcsv($handle, 1000)){
      $data[] = $row;
   }
   fclose($handle);
   return $data;
}

?>

  <h1>Bank Validation</h1>
  <form method="POST" action="bank_validation.php">

<?php

// If the sortcode and account number have been entered they are validated
if (isset($SortCode) && !empty($AccountNumber)) {

   $data = ValidateBank($SortCode, $AccountNumber);

   echo "<table>\n";

   foreach ($data as $k => $v){
      if ($k != 0){

         $name = $data[$k][5];
         $branch = $data[$k][6];
         $ok = $data[$k][23];

         echo "<tr>\n";
         echo "<td>Name</td><td>$name</td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Branch</td><td>$branch</td>\n";
         echo "</tr>\n";
         if ($ok == TRUE) {
            echo "<tr>\n";
            echo "<td colspan=2>Account number validated OK</td>\n";
            echo "</tr>\n";
         }
         else {
            echo "<tr>\n";
            echo "<td colspan=2>Account number not valid</td>\n";
            echo "</tr>\n";
         }
      }
   }
   echo "</table>";
}


//elseif only the sortcode has been entered, the bank address is returned

elseif (empty($AccountNumber) && !empty($SortCode)) {

   $data = GetSortcode($SortCode);

      echo "<table>\n";

      foreach ($data as $keyd => $data) {

         $name = $data["name"];
         $branch = $data["branch"];

         echo "<tr>\n";
         echo "<td>Name</td><td>$name</td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Branch</td><td>$branch</td>\n";
         echo "</tr>\n";

         echo "</table>";

      }

}


//else neither of the sortcode or account number have been entered, the form is simply displayed again

else {

   echo "<p>Enter a sortcode and account number below and click on the 'Validate' button to check the details.</p> <P>Alternatively, just enter a sortcode and the bank address details will be returned</p>\n";
   echo "Sortcode: <input name=\"sortcode\" id=\"sortcode\" value=\"\" type=\"text\" size=\"6\">\n";
        echo "Account number: <input name=\"accountnumber\" id=\"accountnumber\" value=\"\" type=\"text\" size=\"10\">\n";
   echo "<input id=\"btnValidate\" type=\"submit\" value=\"Validate\">\n";

}


?>

   </form>

</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.