CodeIgniter Forums
readfile and insert to db help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: readfile and insert to db help (/showthread.php?tid=26632)

Pages: 1 2


readfile and insert to db help - El Forum - 01-19-2010

[eluser]sasori[/eluser]
I created a folder named "file" outside the system folder.

now here's my controller function

Code:
function add()
    {
        $data['words'] = $this->stock_model->putData("files/testtext.txt");
        $this->load->view('success',$data);
    }

and this is my model function to read the file

Code:
function putData($txt)
    {
      $handle = file_get_contents($txt);
      $var_array_parent = explode("\n",$handle);
      foreach($var_array_parent as $value)
      {
        $var_array = explode(";",$value);
        $a = array_chunk($var_array,4);
        for($i=0;$i<sizeof($a);$i++)
        {
          $a[$i][1] = preg_replace("/\s\%/","%",$a[$i][1]);

          $data = array(
                        'compcode' => $a[$i][0],
                        'pricepercent' => $a[$i][1],
                        'compname' => $a[$i][2],
                        'volume' => $a[$i][3]);

          $this->db->insert('stocks',$data);
        }
      }
      return $this->db->affected_rows();
    }

now am getting this error
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$stock_model

Filename: controllers/welcome.php

Line Number: 23
Quote:Fatal error: Call to a member function putData() on a non-object in C:\xampp\htdocs\myapp\system\application\controllers\welcome.php on line 23

please tell me what to do ?


readfile and insert to db help - El Forum - 01-19-2010

[eluser]flaky[/eluser]
put this in the contructor
Code:
$this->load->model('stock_model');



readfile and insert to db help - El Forum - 01-19-2010

[eluser]hendrawan[/eluser]
this probably becouse you haven't call your model

Code:
$this->load->model('Stock_model');
//and call your function with
$this->Stock_model->putData(bla bla bla);



readfile and insert to db help - El Forum - 01-19-2010

[eluser]sasori[/eluser]
is that mandatory even if it database is already autoloaded ?


readfile and insert to db help - El Forum - 01-19-2010

[eluser]flaky[/eluser]
yes it is


readfile and insert to db help - El Forum - 01-19-2010

[eluser]sasori[/eluser]
now i am getting these

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: models/stock_model.php

Line Number: 29
[quote]
[quote]
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 2

Filename: models/stock_model.php

Line Number: 34
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 3

Filename: models/stock_model.php

Line Number: 35
Quote:A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\myapp\system\application\models\stock_model.php:2)

Filename: codeigniter/Common.php

Line Number: 360

btw, i attached the sample file so you have idea what i was trying to read and attempting to insert into the db


readfile and insert to db help - El Forum - 01-19-2010

[eluser]flaky[/eluser]
put the code of stock_model.php here


readfile and insert to db help - El Forum - 01-19-2010

[eluser]sasori[/eluser]
ok sir this is my stock_model

Code:
class Stock_model extends Model
{

    function Stock_model()
    {
      parent::Model();
    }

    function getStocks()
    {
      $data = array();
      $data[] = $this->db->get('stocks');
      return $data;
    }

    function putData($txt)
    {
      $handle = file_get_contents($txt);
      $var_array_parent = explode("\n",$handle);
      foreach($var_array_parent as $value)
      {
        $var_array = explode(";",$value);
        $a = array_chunk($var_array,4);
        for($i=0;$i<sizeof($a);$i++)
        {
          $a[$i][1] = preg_replace("/\s\%/","%",$a[$i][1]);

          $data = array(
                        'compcode' => $a[$i][0],
                        'pricepercent' => $a[$i][1],
                        'compname' => $a[$i][2],
                        'volume' => $a[$i][3]);

          $this->db->insert('stocks',$data);
        }
      }
      return $this->db->affected_rows();
    }


}



readfile and insert to db help - El Forum - 01-19-2010

[eluser]flaky[/eluser]
at the
Code:
$a[$i][1] = preg_replace("/\s\%/","%",$a[$i][1]);

//change it to

$a[$i][0] = preg_replace("/\s\%/","%",$a[$i][0]);



readfile and insert to db help - El Forum - 01-19-2010

[eluser]flaky[/eluser]
for debugging do a
Code:
print_r($a);
and put the result here