CodeIgniter Forums
How can i initialize a userdefined variable. - 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: How can i initialize a userdefined variable. (/showthread.php?tid=23804)

Pages: 1 2


How can i initialize a userdefined variable. - El Forum - 10-22-2009

[eluser]jinojoseph[/eluser]
Hi all,

I want to initialize a variable like $this->id = mysql_insert_id(); in the model page after a new record is created and i need to get this variable in the controller page.

How can i get this "id" variable in the controller without returning that id using the return statement.

Thanks in advance,

Jino


How can i initialize a userdefined variable. - El Forum - 10-22-2009

[eluser]pistolPete[/eluser]
If you use PHP5, you need to define this variable as public:

Code:
class Model_name extends Model {
    
    public $id;

    public function __construct()
    {
        parent::Model();
    }
}

In PHP4 all class variables are public.

Code:
class Model_name extends Model {
    
    var $id;

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

Now you can access this variable in your controller using:
Code:
$this->model_name->id



How can i initialize a userdefined variable. - El Forum - 10-22-2009

[eluser]jinojoseph[/eluser]
Thanks for ur quick reply!!!. It will be very thankful if u also mention how to initialize a value to this public variable "id " in the model page.


How can i initialize a userdefined variable. - El Forum - 10-22-2009

[eluser]pistolPete[/eluser]
Just like you wrote in your first post:
Code:
$this->id = 0;



How can i initialize a userdefined variable. - El Forum - 10-22-2009

[eluser]jinojoseph[/eluser]
I initialized a public variabe named action as public $action in the model page,

after that,

$data = array('master_name' => $this->input->post('master_name'));


$query = $this->db->insert('master', $data);

$this->action = "insert"; // THis variabel is used in the controller as a flag.

This is causing database error.

Error Number: 1054

Unknown column 'action' in 'field list'

INSERT INTO `master` (`action`, `master_name`) VALUES (NULL, 'sdf')


How can i initialize a userdefined variable. - El Forum - 10-22-2009

[eluser]stuffradio[/eluser]
That error is a MySQL error, it's saying you don't have a column named action in your table "master".


How can i initialize a userdefined variable. - El Forum - 10-23-2009

[eluser]jinojoseph[/eluser]
I know that it is a mysql error, but the reason for this error is because of the "action" variable initialization.

Is there any way to initialize the variable "action" without causing this mysql error.


How can i initialize a userdefined variable. - El Forum - 10-23-2009

[eluser]Michael Wales[/eluser]
Your code isn't making any sense...

Code:
// The data to insert into the table.
$data = array(‘master_name’ => $this->input->post(‘master_name’));
$query = $this->db->insert(‘master’, $data);

Code:
// The error
INSERT INTO `master` (`action`, `master_name`) VALUES (NULL, ‘sdf’)

This means somewhere you are adding 'action' to the $data array. Not what your code in this post is showing us. Are you sure you are not trying to pass $this to the insert() method?


How can i initialize a userdefined variable. - El Forum - 10-23-2009

[eluser]jinojoseph[/eluser]
please read the posts completely from the begining and try to reply..

regards,


How can i initialize a userdefined variable. - El Forum - 10-23-2009

[eluser]Michael Wales[/eluser]
I did read the post from the beginning and I am trying to help. Let's see if we can break this down a little futher:

Here you are inserting 2 columns: action and master_name.
Code:
INSERT INTO `master` (`action`, `master_name`) VALUES (NULL, ‘sdf’)

Here you are inserting 1 column: master_name.
Code:
$data = array(‘master_name’ => $this->input->post(‘master_name’));
$query = $this->db->insert(‘master’, $data);

In the code you have show us, thus far, the only place in which action is referenced is:
Code:
$this->action = 'insert';

So, even though the code you are showing us is this:
Code:
$data = array(‘master_name’ => $this->input->post(‘master_name’));
$query = $this->db->insert(‘master’, $data);

In reality, it looks like this:
Code:
$data = array(‘master_name’ => $this->input->post(‘master_name’));
// ... Do some other stuff here to add an action key to the $data array ...
$query = $this->db->insert(‘master’, $data);

If you would like help in debugging this problem, please share the relevant code in it's entirety.

To do what you want - your code should look something like this:
Code:
class Model_name extends Model {

  var $id;

  function insert($data) {
    $this->db->insert('master', $data);
    if ($this->db->affected_rows() > 0) {
      $this->id = $this->db->last_insert_id();
      return TRUE;
    }

    return FALSE;
  }

}


Code:
class Controller_name extends Controller {

  function index() {
    $this->load->model('model_name');
    if ($this->model_name->insert(array('master_name' => $this->input->post('master_name'))) {
      echo 'Inserted ID: ' . $this->model_name->id;
    } else {
      echo 'Did not insert data.';
    }
  }

}