Where is my code giving me "undefined offset" error?

Hi All,

I am importing a csv file and adding the records to a table. In my test I import 5 records, and each record has 3 fields. Date, Amount, Reference, all are comma separated.

When I import them, the fields ARE saved to the data file, but I get the following error

undefined offset: 1
undefined offset: 1
undefined offset: 1
undefined offset: 2

This is my code

if ({bank_code} == 0){
// do nothing if no bank account is present

}else{
// first get rid of SC’s treatment of zero’s and dashes in their temporary file name
$proper_name = str_replace(“SC_SPACE”," “,{csv_file});
$proper_name = str_replace(“SC_MINUS”,”-",$proper_name);

 // zero the count of records imported
 $cnt=0;

 // open the csv file
 $handle = fopen($proper_name,"r");

 if ($handle)
 {
 set_time_limit(0);

 //loop through one row at a time

 while(! feof($handle))	
 {
 // put the csv data into an array
 $data = fgetcsv($handle, 4096, ',');

 // {date_col} and {amount_col} and {ref_col} contain the column numbers in the csv for date, amount, and reference - in my case 1, 2 and 3.

 // get the date field
 $date_check = $data[{date_col}-1];
 // check for single day digits and add a zero	 if so
 if ($date_check[1] == '/'){
    $date_check = '0'.$date_check;
    //echo $date_check;
 }
 // convert to MySQL date format
 $_date = sc_date_conv($date_check,"dd/mm/yyyy","db_format");

 // get the amount field
 $_amount = ABS($data[{amount_col}-1]);

 if ($data[{amount_col}-1] < 0) {
	    $_drcr = 1;
 }else{
    $_drcr = -1;
 }

 if ($data[{amount_col}-1] < 0) {
    $_type = 1;
 }else{
    $_type = 0;
 }

 // get the reference field
 $_payee = $data[{ref_col}-1];

 // set other values for the record to be inserted
 $_bank = {bank_code};	
 $_not_balanced = 1;

 // insert the record is non zero
 if ($_amount != 0){	
     $stm ="INSERT INTO temp_transactions (tp_date,tp_amount,tp_payee,tp_drcr,tp_bank,tp_type,tp_not_balanced)
               VALUES ('{$_date}',{$_amount},'{$_payee}',{$_drcr},{$_bank},{$_type},{$_not_balanced})";

     sc_exec_sql($stm);
 }

 $cnt++;
 }

 fclose($handle);
 // delete temporary csv file
 unlink($proper_name);	

 }

 [count_imported] = $cnt;

}

sc_redir(‘grid_temp_transactions’);

Bearing in mind that the array starts with [0] I place a -1 to get the correct information into the field.

So, cab anyone see where I might be going wrong here?

Thanks

Tony

Is there any reason to use {date_col} instead of $date_col ?
I mean did you check the code that gets generated? These { … } are scriptcase things in this case which are normally translated to someting else…
I would check the generated code.
This kind of line: $_amount = ABS($data[{amount_col}-1]); should be checked. Scriptcase probably makes something from it that you didnt expect.
In which case you should use:
$_amount_col={amount_col};
$_amount = ABS($data[$amount_col-1]);

I havent tested this but I have seen similar odd behaviour during if statements where it also can fail.

Thanks rr,

You were right. I changed the {date_col} to $date_col, and bingo it all worked. No idea why, but some lateral thinking at it’s best.

See ya

Tony