codeigniter: is this correct way to grab parameter? - codeigniter

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

Related

Codeignitter route for passing id to controller and from controller to view

I have a view where I have a Table of Users, which I print with a Loop.
In this Table I have a Link:
<i class="fa fa-edit">
In the route-config I have following route
$route['edit_user_id/(:num)'] = 'user/edit_user_id/$1';
In the Controller user I have the function
public function edit_user_id($id){
$data['id'] = $id;
$this->load->view('header');
$this->load->view('user/edit_user_id/edit_user_id', $data);
$this->load->view('footer');
}
Im trying to call the function with an id and pass this to the view where
I want to edit the User with this id
Can someone help me to find my failure?
Solution:
$data['id'] = $id;
is wrong it has to be
$data->id = $this->uri->segment(2);
public function edit_user_id($id){
$this->load->view('user/edit_user_id/edit_user_id', $data);
}
check this line of yours
I found the solution,$data['id'] = $id; is wrong it has to be $data->id = $this->uri->segment(2);

How do I setup a second argument for my controller codeIgniter

How do I set up two arguments for my view function in the controller in codeIgniter?
I managed to setup one that takes the category. I need to setup a second one that takes the article alias in the database but when i go to article i get a 404 error page.
In my view function I passed both of them. The first one $category_alias is working but the second one isn't working. It takes me to 404 page.
This is my controller function
public function view($category_alias = NULL, $article_alias = NULL) {
$data['category'] = $this->categories_model->get_all_categories($category_alias);
$data['games_by_category'] = $this->categories_model->get_all_articles_by_category($category_alias);
$data['article_infos'] = $this->categories_model->get_game_infos($article_alias );
if (empty($data['category'])) {
show_404();
}
if (isset($article_alias)) {
$this->load->view('pages/article_display', $data);
}
$data['title'] = $data['category']['category_name']." Games - WalkthroughRemix";
$this->load->view('templates/header', $data);
$this->load->view('templates/top', $data);
$this->load->view('templates/main_menu', $data);
$this->load->view('templates/breadcrumbs', $data);
$this->load->view('pages/category_display', $data);
$this->load->view('templates/footer', $data);
}

how to get values from model and display in controller

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.

How to use session_data in codeigniter throughout Website without including in particular view

Is there any other best way to use session_data in website.
How i set session in my project:
$sess_array = array('id' => $row->user_id,'name'=>$row->user_name,'email'=>$row->email,'condition'=>'','balance'=>$row->balance,'did_alloted'=>$row->did_alloted,'create_date'=>$row->create_date);
$this->session->set_userdata('logged_in', $sess_array);
when it comes to controller:
$data['id'] = $session_data['id'];
$data['name'] = $session_data['name'];
$data['email'] = $session_data['email'];
$data['balance']=$session_data['balance'];
$data['did_alloted']=$session_data['did_alloted'];
$data['create_date']=$session_data['create_date'];
$this->load->view('san-reception', $data);
and in my view. I use <?php echo $name ?> to get session_data.
So is there any method by which i can directly access session_data in view without including as $data.
$this->session->userdata('logged_in'); will be available directly in views and hence you just need to assigned this to a variable and then use that as array like bellow.
$userdata=$this->session->userdata('logged_in');
now variable $userdata contain all array field that you set in controller and hence you can use it as $userdata['id'], $userdata['name'] etc
public function __construct()
{
parent::__construct();
$data['session_data']=$this->session->user_data('logged_in');
}
public function some_function(){
$data['dummy']="";
$this->load->view('someview3',$data)
}
public function some_function1(){
$data['dummy']="";
$this->load->view('someview1',$data)
}
public function some_function2(){
$data['dummy']="";
$this->load->view('someview2',$data)
}
If you assign that into the constructor then it will call by automatically whenever a function calls in the controller.
you can view it in you view page by,
print_r($session_data);

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