Welcome Guest, Not a member yet? Register   Sign In
readfile and insert to db help
#1

[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 ?
#2

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

[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);
#4

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

[eluser]flaky[/eluser]
yes it is
#6

[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
#7

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

[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();
    }


}
#9

[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]);
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB