How to get the value from model to controller - php

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']
}

Related

How to send data from controller to model with CodeIgniter through functions

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.

Getting a drop down list from a database in CodeIgniter

I am new to CodeIgniter and I have been trying to populate the drop down list on the view page with data from the database with no success. I tried using the recomendations from this question but the drop down is still empty (display data from database to dropdown CodeIgniter)
Here is my view:
<label>City</label>
<select class="form-control>
<option value="">All</option>
<?php
foreach($groups as $city)
{
echo '<option value="'.$city['cityidd'].'">'.$city['city'].'</option>';
}
?>
</select> <br/>
Here is my controller:
<?php
class Main_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index()
{
$this->load->helper('form');
$this->load->view('supplier_add');
}
}
Here is my model:
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT city FROM citys');
return $query->result();
}
}
The table name is "citys" then the corresponding column heads are "cityidd" and "city"
There are several issue found there. Make changes as below
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load_model('Site_model');
$this->load->database();
}
public function index(){
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
Finally model
function getAllGroups(){
$query = $this->db->query('SELECT cityidd,city FROM citys');
return $query->result_array();
}
and now test
First change your SELECT query as
SELECT cityidd, city FROM citys
In the index() of controller change the code as
public function index()
{
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
you have to call your model method and pass it to view in the controller, code:
public function index()
{
$this->load->helper('form');
$this->load->model('site_model');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
if you are working on linux dont forget upper and lowercase names!
class Main_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
$this->load->model('Site_model');
}
public function index()
{
$this->load->helper('form');
$data['groups']=$this->Site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
}
Here is my model:
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT * FROM citys');
return $query->result();
}
}
Try like this
Make these changes
IN Model convert data to array as you are using it as array in view so change to this
function getAllGroups()
{
$query = $this->db->query('SELECT * FROM citys');
return $query->result_array();
}
In Controller
public function index()
{
$this->load->helper('form');
$this->load->model('site_model');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
You are not loading model load the Model
for example:-
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Site_model')
$this->load->database();
}
public function index()
{
$this->load->helper('form');
$data['record']=$this->Site_model->getAllGroups()
$this->load->view('supplier_add', $data);
}
}
Model:-
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT city FROM citys');
return $query->result();
}
}

How to call Base Controllers Method in Codeigniter?

I Have Defined MY_ Controller in my core folder.
Then i have two controllers:
Admin_Controller
Customer_Controller
Now i want to put a query into my Customer_Controller whose result i can access all the controller which extends to Customer_Controllers.
I have put this code in Customer_Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
Now when i have a child class something like this
<?php
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//Get Results from Customer_Controller
}
}
how do this?
you can follow this:
class Customer_Controller extends My_Controller
{
public function customer()
{
return 'costomers';
}
}
then
class User extends Customer_Controller
{
//call the function from Customer_Controller
$this->customer();
}
Let me give you a suggestion. Instead of creating a controller and extending the same, why don't you create a library class.
And then load your library inside the controller you want.
for example here is your library class file.
<?php
class users{
private $session;
public function __construct(){
$CI =& get_instance();
$this->session=$CI->session;
}
public function get_users()
{
// do the code and return the
}
}
AND in your controller
<?php
class User extends CI_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$this->load->library('users');
$users = new users();
$userinfo = users->get_users();
//Results from library
}
}
You Try get_instance() like this in you User Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
return $result;
}
class User extends CI_controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$CI=&get_instance();
$result=$CI->get_users();
foreach($result as $row)
{
echo $row->id;//here add you table field name for id
}
}
}
Codeigniter is MVC framework.
So You will put get_users in model part.
example: you must making custom_model.php in models folder.
<?php
class custom_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function get_users($id)
{
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
?>
next step: you are remake custom_controller.
public function get_users()
{
$id = $this->session->userdata('id');
$this->load->model('custom_model');
$result=$this->cutom_model->get_users($id);
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
and next step:
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//
}
public function get_users(){
parent::get_users();
}

How to show retrieve data from database into text field in codeigniter

it's my model code:
<?php
class Books_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_restaurants()
{
$sql = "SELECT id, names FROM restaurants ";
$query = $this->db->query( $sql );
return $query->result();
}
}
controller code:
<?php
class Booking_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$this->user_data['result']=$this->Books_model->get_restaurants();
$this->load->helper(array('form','url'));
$this->load->view('restaurants/booking',$this->user_data);
}
}
What code I written in view file that the data show in text field?
Try the following :
In the Controller
class Booking_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$data['results'] = $this->Books_model->get_restaurants();
$this->load->helper(array('form','url'));
$this->load->view('restaurants/booking',$data);
}
}
In the View:
<?php
foreach ($results as $result)
{?>
<label>Restaurant Name : </label>
<input type="text" value="<?php echo $result->names;?>" />
<?php } ?>
in the controller
<?php
class Booking_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$data["result"]=$this->Books_model->get_restaurants();
//$this->load->helper(array('form','url')); not needed
$this->load->view('restaurants/booking',$data["result"]);
}
}
now in your view
<?php
// notice that CI strip the key "result" from the array $data to become a variable $result in the view
foreach ($result as $row)
{
echo $row->id."<br>";
echo $row->name."<br>";
echo "----";
}
?>
Note:
there is no member like this "$this->user_data['result']" in
codeigniter but there is "$this->session->user_data("data_name")" if
you want to store some data in the session, but then, no need to pass
it to the view as an argument you can call the session data from the
view directly

How to get and display database table value in codeigniter error was coming

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

Categories