• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to send the values of variable from controllers to models ?

#1
[eluser]dimasVS[/eluser]
Hi all,,
I need your help..
how to send the value of variable that created from controller to be sent to models ???

this is my script in Controllers :

function do_upload(){
$n = rand(0,100000);
$nama_file = $_FILES['userfile']['name'];
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['encrypt_name'] = "$n";
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());

$this->load->view('admin/employeeAdd', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$image_data = $this->upload->data();
$file_name = $image_data['file_name'];
echo $file_name;
echo "Register Successfull<br />";
echo anchor('admin/employee', 'Back');
}
}

how to send the value of $file_name to Models ?
and how to catch that value in models ?

Thanks...

#2
[eluser]Dirk Einecke[/eluser]
Hi,

in your controller:

Code:
echo $file_name;
$this->load->model('foobar_model');
$this->foobar_model->afunction($file_name);

in your model:

Code:
function afunction($filename) {
  // do something with $filename
}

Dirk

#3
[eluser]dimasVS[/eluser]
thanks Dirk,,

but how if it's more than one variable ?
for examples I wanna throw $file_name, $file_type.

#4
[eluser]Dirk Einecke[/eluser]
Hi,

in your controller:

Code:
$this->load->model('foobar_model');
$this->foobar_model->afunction($var1, $var2, var3);

in your model:

Code:
function afunction($var1, $var2, var3) {
  // do something with $var1, $var2, var3
}

Dirk

#5
[eluser]dimasVS[/eluser]
hmmm...
can we use array ???
I can't imagine if I have to throw more than 20 variables with that way...

I actually use your way,,
but is there any simpler code ???

#6
[eluser]Dirk Einecke[/eluser]
Hi,

in your controller:

Code:
$vars = array('var1' => $var1, 'var2' => $var2);

$this->load->model('foobar_model');
$this->foobar_model->afunction($vars);

in your model:

Code:
function afunction($vars) {
  // do something with $vars['var1'], $vars['var2']
}

Dirk

#7
[eluser]dimasVS[/eluser]
Oh, thanks Dirk...
It works,, Now I could get all of values from there...
:-)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.