[eluser]Sein Kraft[/eluser]
How can I send data from controller to a model?
Take attention with $file_name. The value of $file_name is necessary in model.
Controller
Code: function do_upload()
{
//UPLOAD
$config['upload_path'] = './';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2000';
$config['max_width'] = '1280';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = array('upload_data' => $this->upload->data());
$file_data = $this->upload->data();
$file_raw = $file_data['raw_name'];
$file_name = $file_data['orig_name'];
$file_ext = $file_data['file_ext'];
$file_size = $file_data['file_size'];
$file_width = $file_data['image_width'];
$file_height = $file_data['image_height'];
// THUMB
$config[] = 'GD2';
$config['source_image'] = './'.$file_name;
$config['new_image'] = './thumb_'.$file_name;
$config['maintain_ratio'] = TRUE;
$config['height'] = 150;
$config['width'] = 150;
$config['quality'] = 100;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//SAVE IN DATABASE
$this->load->model('model_imageboard','', TRUE);
$this->model_imageboard->insert();
$this->load->view('view_imageboard', $data);
}
Model
Code: function insert()
{
$user_id = '1';
$user_ip = '127.0.0.1';
$file_hash = 'myhash';
$file_ext = '.jpg';
$file_size = 'file_size';
$file_height = '600';
$file_width = '800';
$file_source = 'http://www.seinkraft.com/';
$this->db->query("INSERT INTO imageboard_image (id, user_id, user_ip, file_name, file_hash, file_ext, file_size, file_height, file_width, file_source) VALUES (NULL, '$user_id', '$user_ip', '$file_name', '$file_hash', '$file_ext', '$file_size', '$file_height', '$file_width', '$file_source')");
}
[eluser]fatnic[/eluser]
Code: $this->model_imageboard->insert($file_name);
Code: function insert($file_name)
{
$user_id = '1';
$user_ip = '127.0.0.1';
//etc
[eluser]Sein Kraft[/eluser]
Yes, I have tried that but doesn't work.
That shows me the next error:
Code: A PHP Error was encountered
Severity: Notice
Message: Undefined variable: file_name
Filename: models/model_imageboard.php
Line Number: 21
---Edited---
Sorry xD, it's working.
I forgot "insert($file_name)" in model.
[eluser]Sumon[/eluser]
[quote author="Sein Kraft" date="1221277534"]How can I send data from controller to a model?
Take attention with $file_name. The value of $file_name is necessary in model.
Controller
Code: function do_upload()
{
//UPLOAD
$config['upload_path'] = './';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2000';
$config['max_width'] = '1280';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = array('upload_data' => $this->upload->data());
$file_data = $this->upload->data();
$file_raw = $file_data['raw_name'];
$file_name = $file_data['orig_name'];
$file_ext = $file_data['file_ext'];
$file_size = $file_data['file_size'];
$file_width = $file_data['image_width'];
$file_height = $file_data['image_height'];
// THUMB
$config[] = 'GD2';
$config['source_image'] = './'.$file_name;
$config['new_image'] = './thumb_'.$file_name;
$config['maintain_ratio'] = TRUE;
$config['height'] = 150;
$config['width'] = 150;
$config['quality'] = 100;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//SAVE IN DATABASE
$this->load->model('model_imageboard','', TRUE);
$this->model_imageboard->insert();
$this->load->view('view_imageboard', $data);
}
Model
Code: function insert()
{
$user_id = '1';
$user_ip = '127.0.0.1';
$file_hash = 'myhash';
$file_ext = '.jpg';
$file_size = 'file_size';
$file_height = '600';
$file_width = '800';
$file_source = 'http://www.seinkraft.com/';
$this->db->query("INSERT INTO imageboard_image (id, user_id, user_ip, file_name, file_hash, file_ext, file_size, file_height, file_width, file_source) VALUES (NULL, '$user_id', '$user_ip', '$file_name', '$file_hash', '$file_ext', '$file_size', '$file_height', '$file_width', '$file_source')");
}
[/quote]
change save in database portion as
Code: //SAVE IN DATABASE
$this->load->model('model_imageboard','', TRUE);
$this->model_imageboard->insert();
$data['file_name'] = $file_name;
$this->load->view('view_imageboard', $data);
and model as
Model
Code: function insert($data)
{
$file_name = $data['file_name'];
$user_id = '1';
$user_ip = '127.0.0.1';
$file_hash = 'myhash';
$file_ext = '.jpg';
$file_size = 'file_size';
$file_height = '600';
$file_width = '800';
$file_source = 'http://www.seinkraft.com/';
$this->db->query("INSERT INTO imageboard_image (id, user_id, user_ip, file_name, file_hash, file_ext, file_size, file_height, file_width, file_source) VALUES (NULL, '$user_id', '$user_ip', '$file_name', '$file_hash', '$file_ext', '$file_size', '$file_height', '$file_width', '$file_source')");
}
let me know is there are any error happening.
[eluser]Sein Kraft[/eluser]
Yeah! Is working.
Thanks Sumon & fatnic.
|