simonp Posted August 8, 2007 Share Posted August 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/ Share on other sites More sharing options...
pranav_kavi Posted August 8, 2007 Share Posted August 8, 2007 $name = $data[5]; $branch = $data[6]; $ok = $data[23]; Instead of numeric index,you ll ve 2 use it as $name = $data[$keyd]; $branch = $data[$keyd]; $ok = $data[$keyd]; Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318352 Share on other sites More sharing options...
simonp Posted August 8, 2007 Author Share Posted August 8, 2007 Thanks pranav_kavi - unfortunately, that gives me the exact same error Any ideas? Simon Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318356 Share on other sites More sharing options...
HuggieBear Posted August 8, 2007 Share Posted August 8, 2007 Is $Data actually an array? Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318359 Share on other sites More sharing options...
simonp Posted August 8, 2007 Author Share Posted August 8, 2007 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 */ Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318369 Share on other sites More sharing options...
pranav_kavi Posted August 8, 2007 Share Posted August 8, 2007 how r u populating $Data???Rather than using arrays,classes can b used in ur case. Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318371 Share on other sites More sharing options...
jitesh Posted August 8, 2007 Share Posted August 8, 2007 if(is_array($Data)){ echo "For each will work"; }else{ echo "For each will not work"; } Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318374 Share on other sites More sharing options...
simonp Posted August 8, 2007 Author Share Posted August 8, 2007 Ah! Thanks jitesh - that tells me that $Data is not an array I guess (it said "For each will not work") Can you suggest what I should use instead of foreach? Ta Simon Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318376 Share on other sites More sharing options...
jitesh Posted August 8, 2007 Share Posted August 8, 2007 You define a variable $Data as array before using it. $Data = array(); Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318379 Share on other sites More sharing options...
simonp Posted August 8, 2007 Author Share Posted August 8, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318402 Share on other sites More sharing options...
jitesh Posted August 8, 2007 Share Posted August 8, 2007 Do not found where the datas in $Data (array) fill up. Is $Data contains the details collected from csv file ? If Yes Change name from $Data to other. Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318407 Share on other sites More sharing options...
simonp Posted August 8, 2007 Author Share Posted August 8, 2007 $Data comes from the ValidateBank function of which we have no control on what's returned Simon Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318410 Share on other sites More sharing options...
jitesh Posted August 8, 2007 Share Posted August 8, 2007 $Data does not exists in your script. the data from cvs files are collected to $data. Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318416 Share on other sites More sharing options...
simonp Posted August 8, 2007 Author Share Posted August 8, 2007 but the $Data comes from the function . . ? Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-318507 Share on other sites More sharing options...
HuggieBear Posted August 9, 2007 Share Posted August 9, 2007 I might have it, back shortly. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-319186 Share on other sites More sharing options...
simonp Posted August 9, 2007 Author Share Posted August 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-319189 Share on other sites More sharing options...
HuggieBear Posted August 9, 2007 Share Posted August 9, 2007 OK, I'll get back to you. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-319192 Share on other sites More sharing options...
HuggieBear Posted August 9, 2007 Share Posted August 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-319214 Share on other sites More sharing options...
simonp Posted August 9, 2007 Author Share Posted August 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-319217 Share on other sites More sharing options...
HuggieBear Posted August 9, 2007 Share Posted August 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-319219 Share on other sites More sharing options...
simonp Posted August 9, 2007 Author Share Posted August 9, 2007 Tada! Brilliant - thanks Huggie. It's returning the first two fields now but the last one always says: Account number not valid whether it's valid or not . . any ideas? I really appreciate your help here btw Simon Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-319220 Share on other sites More sharing options...
HuggieBear Posted August 9, 2007 Share Posted August 9, 2007 OK, if you open the CSV file, does it say True or TRUE? Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-319229 Share on other sites More sharing options...
simonp Posted August 9, 2007 Author Share Posted August 9, 2007 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)! Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-319232 Share on other sites More sharing options...
HuggieBear Posted August 9, 2007 Share Posted August 9, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-319234 Share on other sites More sharing options...
HuggieBear Posted August 9, 2007 Share Posted August 9, 2007 Attached is the finished file with the altered GetSortcode() function too. Regards Huggie [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/63868-solved-php-invalid-argument-error-can-you-help/#findComment-319237 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.