how to get values from model and display in controller - codeigniter

I am making a site in codeigniter, here is my controller, model and view:
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:
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']
}
but i want to get $new_dbemail['name'] in my controller, how i can get the value of $new_dbemail['name'] in controller rather than view ???

To do so... you have to fetch data into a row_array not in result_array in Model.
Controller
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();
$database_field = $data['dbemail']['database_field']; //to get result in controller
$date['database_field'] = $data['dbemail']['database_field']; //to get result in view
$this->load->view('home', $data);
}
Model
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
return $query->row_array();
}
View
<p> Database Field value : <?=$database_field;?></p>

You are Calling the Model In A Variable :-
$data['dbemail']=$this->login_model->email();
So in this you have the results which you are fetching from Database. Than you passing this variable data to your View.
$this->load->view('home', $data);
So you already have the value in controller which u fetched from model which is stored in $data.
You can View the Values of $data , using
echo "<pre>";
print_r($data);
and it will show what data you are receiving

Try like this...
In controller...
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$dbemail=$this->login_model->email();//array containing all emails
//For each loop here for displaying emails
foreach($dbemail as $email){
$mail = $email; //assigning to variable
echo $mail."<br/>";//displaying email
}
$data['dbemail'] = $dbemail;//for passing to view
$this->load->view('home', $data); //passing your value to view
}

First you need to fetch the name from the database in the model. Change the query line:
$query = $this->db->query("SELECT name, email FROM change_password");
Then to use the name data in the controller you could try this:
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();
$name = $data['dbemail']['name']; // example usage to retrieve name in controller
$this->load->view('home', $data);
}
Now the name data is available as $name in controller.

Controller:
public function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemails']=$this->login_model->email();
$this->load->view('home', $data); //passing your value to view
}
Model
public function email()
{
$this->db->select("email"); // the row of the table you want to select.
$this->db->get('change_password'); //table name. you can still use your query, its just much neat when you use a specific query builder.
$result = $this->result(); //giving you a result of type object
return $result;
}
View
foreach ( $dbemails as $dbemail )
{
echo $dbemail->email; // the '->' will be the identifier of which row you want to get or display, i assume its email beacause you specify it in the model.
}
Hope this what your looking for.

Related

How to passing value from link in codeigniter

My link in view page
Detail
My controller
public function cari(){
//what i'm doing here ? please help ?
}
My model
function search_by_id($id){
$query = $this->db->where('id',$id);
$query = $this->db->get();
return $query->result();
}
How pass value from link to model and show the result to view again?
it's my first time using codeigniter, need some help
You have to pass argument into method:
public function cari($hasil_id){
if ((int)$hasil_id > 0)
{
$this->load->model('Name_of_model');
$data['search_by_id'] = $this->Name_of_model->search_by_id($hasil_id);
$this->load->view('hasil_view', $data);//assuming page for item
}
else
{
redirect('not_good_id_method', 'refresh');//in case of not valid id
}
}
In your controller ,receive the value passed through link by uri class.And then pass it into your model.
Controller :
public function cari()
{
$id= $this->uri->segment(3);
$this->model_name->search_by_id($id);
}
Take a look on this.

Passing values from Controller to Controller

I am new to laravel and I'm trying to pass a value from one controller to another controller.. In this case.. I want to pass the order_id generated in Order Controller to /pizza/create in Pizza Controller..
this is my code in OrderController
public function store()
{
$user = User::find(Auth::user()->id);
$order= new Order;
$order->user()->associate($user);
$order->status = 'unconfirmed';
$order->save();
return Redirect::to('/pizza/create')
->with('order', $order->order_id);
}
this is my code in /pizza/create in PizzaController
public function create()
{
$id = Session::get('order');
$order = Order::find($id);
return View::make('pizza.create')
->with('order', $order);
}
this somehow works.. but the value (order_id) disappears when i change views/routes..
if you want to pass a value from controller to controller use session, when you are using redirect and with method it is creating session, when you are using view and with method it is creating variable which you can use in view to display that value.
this create a session
return Redirect::to('/pizza/create')
->with('order', $order->order_id);
this statement create a variable which you can use in view
return View::make('pizza.create')
->with('order', $order);
above variable you can use in blade or view file
{{ $order }}
so in create method you need to create a session keep variable alive and available in other controllers
public function create()
{
$id = Session::get('order');
$order = Order::find($id);
Session::put('order',$id);
return View::make('pizza.create')
->with('order', $order);
}

Codeigniter and Set Session in Model

I want to unset and set session in model function depend on passed id.
I have language field for every record and should change session considering that.
in sample word i have a link like this : http://www.mysite.com/report/2020
now this article by this id maybe saved by english language but current session is french.so I should change the session.
here is my code but it dos not work.
//function for checking language by id
public function lang($id)
{
$data = $this->db
->select('language')
->from('content')
->where('id',$id)
->get();
if ($data->num_rows > 0) {
return $data;
}else{
return false;
}
}
//function for reading data from db depend on id and language
public function report($id)
{
$cat=2;
$code=$id;
$langu= $this->lang($id)->row()->language;
if($langu==3){
$this->session->unset_userdata('lang');
$this->session->set_userdata('lang','dr');
}
if($langu==2){
$this->session->unset_userdata('lang');
$this->session->set_userdata('lang','pa');
}
if($langu==1){
$this->session->unset_userdata('lang');
$this->session->set_userdata('lang','en');
}
$language= $this->session->userdata('lang');
$lang=$language;
$data = $this->db
->select('*')
->from('content')
->where('category',$cat)
->where('language',$lang)
->where('id',$code)
->get();
if ($data->num_rows > 0) {
return $data;
}else{
return false;
}
}
the best way to session is:
you have to check the query in model, and call the model in controller, if its true set session data within controller....
Setting Session within the model is not a good idea for an MVC architecture.
Sometimes there is really need to set or get session data in model.
Use then Code Igniter instance like below:
$codeIgniter =& get_instance();
$codeIgniter->set_userdata('data', $data);
// or to read data:
$data = $codeIgniter->userdata('data');
Note that using $this to call set_userdata() or userdata() in model will not work correctly.

codeigniter: is this correct way to grab parameter?

if my page is Company/add/4 (company is controller, add is the function and 4 is the id of that company)
is this how I would echo the id to another page:
public function add($id) {
$data['data'] = $id;
$this->load->view('templates/header');
$this->load->view('locations/add', $data);
$this->load->view('templates/footer');
then on the view:
echo $data['id'];
This is working correctly but i'm not sure its the best way to do it. because it seems like it would make more sense to have:
public function add($id) {
$this->load->view('templates/header');
$this->load->view('locations/add', $id);
$this->load->view('templates/footer');
but that doesn't appear to work.
Updated the controller
public function add($id) {
$data['id'] = $id;
$this->load->view('templates/header');
$this->load->view('locations/add', $data);
$this->load->view('templates/footer');
On VIew
just do echo $id
codeigniter passes an array to the view so that you can pass not only 1 but group of data to you're views, use the key that you assigned on the controller as the variable's name on the view
whatever you place on the key that will be the name of you're variable in the view
//controller
$data['test'] = 'test';
//view
var_dump($test);//string "test"
//controller
$data['test'] = array('1','2');
//view
var_dump($test);//array()
//controller
$data['test'] = 1;
//view
var_dump($test);//int "1"
so on and so forth

Fetching “id” from a view and pass to another view in Grocery Crud CodeIgniter

I am new in Grocery Crud with Code Igniter and need a help. I have table vaboteni (emploees) and it work well. But I get stuck with code add more action. When I click to add action button I got error 404 Page Not Found. I want to fetch "id" from one row in table and pass to another view in order to display data for only one employee. I have site in local server, address localhost/bis_resursi/index.php/vraboteni/vraboteni_managment
Here is my Controller vraboteni.php
function vraboteni_management()
{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('vraboteni');
$crud->set_subject('вработен');
.....
$crud->add_action('Преглед', '', 'vraboteni/vraboten_managment/pregled','ui-icon-plus');
function pregled($id)
{
$this->load->model("vraboteni_pregled_model");
$data["result"] = $this->getVraboteniPregled($vrabotenID);
$this->load->view("pregled", $data);
}
$output = $crud->render();
$this->_example_output($output);
}
and Models: vraboteni_pregled_model.php
<?php
class Vraboteni_Pregled_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
function getVraboteniPregled($id){
$query = $this->db->query("SELECT * FROM vraboteni WHERE vraboteID = '$id' ");
return $query->result();
}
and in view vraboten_view.php I put
<?=$query['vrabotenID']?>
<br>
Hi, I'am <?=$query['ime']?>
<br>
from<?=$query['adresa']?>
I managed to find solution. Right code is:
Controller vraboteni.php
$crud->add_action('Преглед', '', 'vraboteni/get','ui-icon-plus');
$output = $crud->render();
$this->_example_output($output);
}
function vraboteni()
{
$crud = new grocery_crud();
$crud->set_table('vraboteni');
$output = $crud->render();
print_r($output);
}
function getall()
{
$this->load->model('vraboten_model');
$data['query']=$this->vraboten_model->vraboten_getall();
$this->load->view('vraboten_view',$data);
}
function get($vrabotenID)
{
$this->load->model('vraboten_model');
$data['query']=$this->vraboten_model->vraboten_get($vrabotenID);
$this->load->view('vraboten_view',$data);
}
Models vraboten_model.php
<?php
class Vraboten_model extends CI_Model{
function vraboten_model(){
parent::__Construct();
}
function vraboten_getall(){
$this->load->database();
$query=$this->db->get(' vraboteni');
return $query->result();
}
function vraboten_get($vrabotenID){
$this->load->database();
$query=$this->db->get_where(' vraboteni',array('vrabotenID'=>$vrabotenID));
return $query->row_array();
}
}
and view vraboten_view.php
<?=$query['vrabotenID']?>
<br>
Hi, I'am <?=$query['ime']?>
<br>
from<?=$query['adresa']?>

Resources