Jump to content

undefined offset help


sasori

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/189024-undefined-offset-help/
Share on other sites

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.