sasori Posted January 19, 2010 Share Posted January 19, 2010 hi, here's my code $handle = read_file($txt); $var_array_parent = explode("\n", $handle); foreach($var_array_parent as $value) { $var_array = explode(";", $value); $a = array_chunk($var_array, 4); } foreach($a as $k=>$v) { $temp = explode(' ',$v[1]); unset($a[$k][1]); $a[$k][] = $temp[0]; $a[$k][] = $temp[1].$temp[2]; } $b = print_r($a,true); for($i=0;$i<count($a);$i++){ $data = array( "compcode" => $a[$i][0], "price" => $a[$i][4], "percent" => $a[$i][5], "compname" => $a[$i][2], "volume" => $a[$i][3]); $this->db->insert('stocks',$data); when am trying to insert it in the db..it returns A PHP Error was encountered Severity: Notice Message: Undefined offset: 1 Filename: models/stock_model.php Line Number: 53 [ A PHP Error was encountered Severity: Notice Message: Undefined offset: 1 Filename: models/stock_model.php Line Number: 56 A PHP Error was encountered Severity: Notice Message: Undefined offset: 2 Filename: models/stock_model.php Line Number: 56 what should i do? please help with the coding logic Quote Link to comment https://forums.phpfreaks.com/topic/189024-undefined-offset-help/ Share on other sites More sharing options...
Adam Posted January 19, 2010 Share Posted January 19, 2010 Which are lines 53 and 56? That looks like some messy logic.. What is it you're trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/189024-undefined-offset-help/#findComment-998007 Share on other sites More sharing options...
Buddski Posted January 19, 2010 Share Posted January 19, 2010 Im going to guess that line 53 is $temp = explode(' ',$v[1]); and line 56 is $a[$k][] = $temp[1].$temp[2]; Your obviously referencing array keys that do not exist.. You need to make sure that $v and $temp have the associated keys.. Quote Link to comment https://forums.phpfreaks.com/topic/189024-undefined-offset-help/#findComment-998009 Share on other sites More sharing options...
sasori Posted January 19, 2010 Author Share Posted January 19, 2010 sir this was the response from the codeigniter. I was trying to extract some jumbled data from a text file and then input them in mysql... btw this is the original function before i even put it on the my model in CI. it use to work by running it alone $var_array_parent = explode("\n", $handle); foreach ($var_array_parent as $value) { $var_array = explode(";", $value); $a = array_chunk($var_array, 4); } foreach($a as $k=>$v) { $temp = explode(' ', $v[1]); unset($a[$k][1]); $a[$k][] = $temp[0]; $a[$k][] = $temp[1].$temp[2]; } echo '<pre>'.print_r($a, true).'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/189024-undefined-offset-help/#findComment-998010 Share on other sites More sharing options...
Buddski Posted January 19, 2010 Share Posted January 19, 2010 It would actually help if you actually told us the lines that are at fault.. it may also help for us to see the text document you are reading in.. Quote Link to comment https://forums.phpfreaks.com/topic/189024-undefined-offset-help/#findComment-998021 Share on other sites More sharing options...
sasori Posted January 19, 2010 Author Share Posted January 19, 2010 sir(s) attached is the data that i was trying to read. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/189024-undefined-offset-help/#findComment-998030 Share on other sites More sharing options...
Buddski Posted January 19, 2010 Share Posted January 19, 2010 after your first foreach statement can you do a var_dump($a) and show us the results.. Quote Link to comment https://forums.phpfreaks.com/topic/189024-undefined-offset-help/#findComment-998041 Share on other sites More sharing options...
sasori Posted January 19, 2010 Author Share Posted January 19, 2010 function putData($txt) { $handle = read_file($txt); $var_array_parent = explode("\n", $handle); foreach($var_array_parent as $value) { $var_array = explode(";", $value); $a = array_chunk($var_array, 4); } echo "<pre>",print_r($a),"</pre>"; foreach($a as $k=>$v) { $temp = explode(' ',$v[1]); unset($a[$k][1]); $a[$k][] = $temp[0]; $a[$k][] = $temp[1].$temp[2]; } the result was Array ( [0] => Array ( [0] => ABA [1] => 0.97 0.00 % [2] => Abacus A [3] => 50,000 ) [1] => Array ( [0] => ABS [1] => 22.00 1.1494 % [2] => ABS-CBN [3] => 60,700 ) and ofcourse there are these errors at the end of it A PHP Error was encountered Severity: Notice Message: Undefined offset: 1 Filename: models/stock_model.php Line Number: 54 A PHP Error was encountered Severity: Notice Message: Undefined offset: 1 Filename: models/stock_model.php Line Number: 57 A PHP Error was encountered Severity: Notice Message: Undefined offset: 2 Filename: models/stock_model.php Line Number: 57 Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\myapp\system\application\models\stock_model.php on line 63 where line 63 is this "compcode" => $result[$i][0], Quote Link to comment https://forums.phpfreaks.com/topic/189024-undefined-offset-help/#findComment-998061 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.