I want to pass data from Controller to model.But I am unable to fetch it at the model side in CI.Kindly help me out.
Here is my controller function:
function show_chronicles($chronicle_num) {
$this->load->database();
//load the model
$this->load->model('Chronicles_model');
//load the method of model
$data['h'] = $this->Chronicles_model->show_seminar();
//return the data in view
$this->load->view('chronicles', $chronicle_num);
}
And here is my model:
public function show_seminar($chronicle_num) {
echo $chronicle_num;
//$this->db->select('*');
//$this->db->where('chronicles_no',$chronicle_num);
//$query1 = $this->db->get('chronicles');
//return $query1;
}
Its because your not passing any value to your model.
CONTROLLER
function show_chronicles($chronicle_num)
{
$this->load->database();
$this->load->model('Chronicles_model');
$data['h']=$this->Chronicles_model->show_seminar($chronicle_num);
$this->load->view('chronicles', $chronicle_num);
}
and you need to return the result() of your query
MODEL
public function show_seminar($chronicle_num = NULL)
{
return $this->db
->get_where('chronicles', array('chronicles_no' => $chronicle_num))
->result();
}
Controller:
function show_chronicles($chronicle_num) {
$this->load->database();
//load the model
$this->load->model('Chronicles_model');
//load the method of model
// pass it to model
$data['chronicle_num'] = $this->Chronicles_model->show_seminar($chronicle_num);
//return the data in view
$this->load->view('chronicles', $data);
}
And here is my model:
public function show_seminar($chronicle_num) {
return $chronicle_num;
//$this->db->select('*');
//$this->db->where('chronicles_no',$chronicle_num);
//$query1 = $this->db->get('chronicles');
//return $query1;
}
You just forgot to pass $chronicle_num as a parameter when you called $this->Chronicles_model->show_seminar();.
So the right way is $this->Chronicles_model->show_seminar($chronicle_num);
You can add as many parameters as you wish to the functions. Example:
public function show_seminar($chronicle_num, $param2, $param3) {
//Login here
}
Just remember to pass the parameters every time you call the function.
Related
I'm new in codeigniter, I'm having a challenge with num_rows on view page
Here's the sample of the code - Updated
Model
public function __construct()
{
parent::__construct();
}
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Referrals_form_model', 'referral'); /* LOADING MODEL * Referral_form_model as referral */
}
public function index()
{
$this->load->helper('url');
$this->load->view('referrals_form_view');
}
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
}
View
<span><?php
echo $query->num_rows();
?></span>
when i run the code, it tell me undefined variable "query"
Please help.
Thank you
Please change your code as below.
MODEL:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
View :
<span>
<?php
echo $total_referred_in;
?>
</span>
As you can see in Controller line :
$this->load->view("referral_form_view", $data);
$data here is the data array you are passing to your view. In your view you can access data in $data variable by using $data variable's KEY as php variable(In our case its "total_referred_in").
You aren't returning anything from your function, you're just echoing. Try this:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller:
public function referral_in()
{
$data['num_rows'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
And then in your view:
<span><?=$num_rows?></span>
model
This is my model.using last query I get value in the model,but I don't get the value in the controller
class Login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function email()
{
$this->db->select('email');
$this->db->from('change_password');
$result=$this->db->get();
return $result;
}
}
controller
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
}
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$dbemail=$this->login_model->email();
echo $dbemail;
}
}
CI is a MVC Framework. So you need to Command from Controller and get data from the models and finely you need to pass them to view. This is a best Practice
Controller
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();// assign your value to CI variable
$this->load->view('home', $data); //passing your value to view
}
Model
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
$result = $query->result_array();
return $result;
}
View
Extra Knowledge
View will be create under View folder/ Ex im using view name
as home.php
you can use your own stylings(its also created as normal html page.)
foreach ( $dbemail as $new_dbemail )
{
echo $new_dbemail['database_field'];//in here you can get your table header.
//Ex if your table has name field and you need to sho it you can use $new_dbemail['name']
}
I am a new codeigniter developer and have encountered an error. I have stored some value in database and want to display it in view page. Help me please.
Controller
function get_user_value() {
$data['query'] = $this->user_m->user_data_set($query);
$this->load->view('user_settings', $data); }
Model
public function user_details()
{
// $query = $this->db->select('*')->from('user')->get();
$query = $this->db->get('user');
return $query->$result();
}
Try this:
Note call first the model to your controller
add first this function _construct()
function __construct() {
parent::__construct();
$this->load->model("model_file_name", "model_name_alias");
//Example $this->load->model("user_model","user");
//the "user_model" is the file name in your model folder
}
function get_user_value() {
$data['query'] = $this->model_name_alias->user_details();
$this->load->view('user_settings', $data);
}
I start with CakePHP framework. I want use paginator on my site but I wanted to do all the operations in the model.
When I use all in controller is OK:
public function index() {
$this->Paginator->settings = $this->paginate;
$data = $this->Paginator->paginate('MyModel');
$this->set('data', $data);
}
but when i want use method from model have problem with column: MyModel:
public function index() {
$this->Paginator->settings = $this->paginate;
$data = $this->Paginator->paginate($this->MyModel->getData());
$this->set('data', $data);
}
//model method
public function getData()
{
$data = $this->find('all');
return $data;
}
ofc when I display this method without paginate is ok:
public function index() {
$data = $this->MyModel->getData();
$this->set('data', $data);
}
Friend you are trying to apply the pagination on the "array" not on the model. Cakephp provide the functionality to pagination data for the model instance. As the model data transaction method have the database connection object and the pagination component manage that object.
I have variables that are created and used on my model that I need to be able to use on my controller how is that accomplished?
Edit:
Controller: http://pastebin.com/jhAwAVa6
Model: http://pastebin.com/9xXRyYAa
It's unclear from your question, what exactly you want to do.
If it is about accessing model properties, the right way is using accessor methods:
class Model extends CI_Model{
private $name;
public function getName() {return $this->name; /*any other logic here*/}
public function setName($value) {$this->name= $value; /*any other logic here*/}
}
You can not pass the variable from a model to controller.
You can access public variables of a model through a controller.
echo $this->model_name->variable_name;
Model (my_model)
function useful_info()
{
$data = new stdClass();
$q = $this->db->get('users');
$data->users = $this->db->result();
$data->date = date('Y-m-d');
$data->info = array('whatever','more','anything');
return $data;
}
Controller
function index()
{
$info = $this->my_model->useful_info();
foreach($info->users as $user)
{
echo $user->id;
}
echo $info->date;
if($info->info[0] == 'whatever')
{
// do something
}
}
You don't have to create an object (it can be a string, T/F, array, etc), but you usually need to return something from your model and library functions. And you can access what you return by returning it to a variable $info = $this->my_model->useful_info();