I have several $data which are called in almost all functions in controller. Is there a way to create this $data in __construct function and combine them with $data in called function? Example:
function __construct() {
parent::__construct();
$this->load->model('ad_model', 'mgl');
$this->load->model('global_info_model', 'gi');
$this->load->model('user_model', 'um');
$this->load->library('global_functions');
$this->css = "<link rel=\"stylesheet\" href=\" " . CSS . "mali_oglasi.css\">";
$this->gi_cat = $this->gi->gi_get_category();
$this->gi_loc = $this->gi->gi_get_location();
$this->gi_type = $this->gi->gi_get_type();
}
function index() {
$count = $this->db->count_all('ad');
$data['pagination_links'] = $this->global_functions->global_pagination('mali_oglasi', $count, 2);
$data['title'] = "Mali Oglasi | 010";
$data['oglasi'] = $this->mgl->mgl_get_all_home(10);
$data['loc'] = $this->gi_loc;
$data['cat'] = $this->gi_cat;
$data['stylesheet'] = $this->css;
$data['main_content'] = 'mali_oglasi';
$this->load->view('template',$data);
}
If I want to put $data['loc'], $data['cat'] and $data['stylesheet'] in __construct I will have to call $this->data in $this->load->view('template',$data);
Is there a way to combine this two?
Add a private member to your controller and set it in the constructor as you need it:
private $data;
function __construct() {
...
$this->data = array(...);
...
}
Then you can access this private member in all of your controllers actions inside the same controller class.
You can merge two arrays using the array union operator (+)Docs:
$data = $this->data + $data;
See as well: PropertiesDocs
Sure, you could do it like this,
class ControllerName extends CI_Controller {
private $_data = array();
function __construct()
{
$this->_data['loc'] = this->gi_loc;
$this->_data['cat'] = this->gi_cat;
$this->_data['stylesheet'] = this->css;
}
function index()
{
// Your data
// Merge them before the $this->load->view();
$data = array_merge($this->_data, $data);
}
}
Related
I need function (literally i need the $data) from this model in whole project.
when i call in some controller $data = $this->multi_language_model->multi_lang(); it work fine. But is it possible to call in one place, so I can use in every controller and view.
I autoload the model
$autoload['model'] = array('multi_language_model');
class Multi_language_model extends MY_Model
{
public function __construct() {
parent::__construct();
}
public function multi_lang() {
$data['menu_delivery'] = $this->lang->line('menu_delivery');
$data['menu_quotations'] = $this->lang->line('menu_quotations');
$data['menu_customer_service'] = $this->lang->line('menu_customer_service');
return $data;
}
}
Put this helper function in your any loaded helper:
function get_multi_lang(){
$CI = & get_instance();
$data = array();
$CI->load->helper('language');
$CI->lang->load('menu','english');
$data['menu_delivery'] = $CI->lang->line('menu_delivery');
$data['menu_quotations'] = $CI->lang->line('menu_quotations');
$data['menu_customer_service'] = $CI->lang->line('menu_customer_service');
return $data;
}
Controller:
class Yourclassname extends CI_Controller {
var $menu_delivery = "";
var $menu_quotations = "";
var $menu_customer_service = "";
function __construct() {
parent::__construct();
$data = get_multi_lang();
$this->menu_delivery = $data['menu_delivery'];
$this->menu_quotations = $data['menu_quotations'];
$this->menu_customer_service = $data['menu_customer_service'];
}
public function index(){
echo $this->menu_delivery.'<pre>';
echo $this->menu_quotations.'<pre>';
echo $this->menu_customer_service.'<pre>';die;
}
}
If you dont want to use helper function then copy helper function lines in controller __construct() [replace $data to $this->]so direct use global variables do same in model and for view pass this variable via controller
I don't know how codeigniter works but maybe you could do this method static and use it like:
class Multi_language_model extends MY_Model
{
private static $data = [];
public function get_multi_lang()
{
return [
'menu_delivery' => $this->lang->line('menu_delivery'),
'menu_quotations' => $this->lang->line('menu_quotations'),
'menu_customer_service' => $this->lang->line('menu_customer_service'),
];
}
public static function multi_lang()
{
if (empty(self::$data)) {
self::$data = (new self)->get_multi_lang();
}
return self::$data;
}
}
Then, whenever you need it, you can use $data = Multi_language_model::multi_lang();
However, I don't see anything wrong in injecting it from the container, wherever you need it. Doing so would be much easier to build tests.
BTW you don't need to overwrite the class constructor if there are no custom parameters set to the extending class. You can safely remove:
public function __construct() {
parent::__construct();
}
I have a class with methods, some of these methods use the same variable across board - "$company_id". Now, I don't want to explicitly define what is contained in $company_id for every method. I want to define it once in a constructor and then reference it in my methods. Please how do I do this? This is how it looks currently.
public function __construct(){
//what should I do here?
}
public static function getItemLimit(){
$company_id = Auth::user()->company_id;
$item_limit = Company::where('id', $company_id)->count();
return $item_limit;
}
public static function currentItemCount(){
$company_id = Auth::user()->company_id;
$item_count = Item::where('company_id', $company_id)->count();
return $item_count;
}
Try this Use Company_Id instead of $abcVar
class Abc{
public static $abcVar = '';
public function __construct()
{
self::$abcVar = 11;
}
public static function getItemLimit()
{
echo self::$abcVar;
exit;
}
}
$obj = new Abc();
Abc::getItemLimit();
I have two functions in my model as
class Jobseeker_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function result_getall($id)
{
$this->db->select('*');
$this->db->from('tbl_jobseeker');
$this->db->where('tbl_jobseeker.User_id',$id);
$this->db->join('tbl_work_exp', 'tbl_jobseeker.User_id = tbl_work_exp.User_id','left');
$query = $this->db->get();
return $query->row();
}
public function select($id)
{
$this->db->select('*');
$this->db->from('tbl_qualification');
$this->db->where('tbl_qualification.User_id',$id);
$query = $this->db->get();
return $query->result();
}
}
And in my controller I have a function as
public function display()
{
$id = $this->session->userdata('user_id');
$data['row'] = $this->jobseeker_model->result_getall($id);
$res['a'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data,$res);
}
It is not possible to display the view page.. I could pass two variables into my view page.right?
You can pass your any number of variables/arrays using a single array.
In Controller:
public function display() {
$id = $this->session->userdata('user_id');
$data['var1'] = $this->jobseeker_model->result_getall($id);
$data['var2'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data);
}
In View:
`$var1` and `$var2` will be available.
You can pass your two variable using single srray
public function display()
{
$id = $this->session->userdata('user_id');
$data['row'] = $this->jobseeker_model->result_getall($id);
$data['a'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data);
}
Views
foreach($a as $data){
// your code
}
echo $row->column_name;
Try this
public function display()
{
$id = $this->session->userdata('user_id');
$data['row'] = $this->jobseeker_model->result_getall($id);
$data['a'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data);
}
I'm using Codeigniter with dynamic subdomains, but in each method of my controllers I need to get the account of the dynamic subdomain. I'm looking for a way to get the domain and add to the $data without to it every method like:
<?php
class Dashboard extends CI_Controller {
function index()
{
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
$subdomain_name = $subdomain_arr[0];
$this->db->from('accounts')->where('subdomain', $subdomain_name);
$query = $this->db->get();
$account = $query->row();
$data['account_id'] = $account->id;
$data['account_name'] = $account->name;
$this->load->view('index', $data);
}
function clients()
{
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
$subdomain_name = $subdomain_arr[0];
$this->db->from('accounts')->where('subdomain', $subdomain_name);
$query = $this->db->get();
$account = $query->row();
$data['account_id'] = $account->id;
$data['account_name'] = $account->name;
$this->load->view('clients', $data);
}
}
Do it once within a Class Constructor and then you can access the same variables from all of the other methods.
As per docs:
"Constructors are useful if you need to set some default values, or run a default process when your class is instantiated. Constructors can't return a value, but they can do some default work."
<?php
class Dashboard extends CI_Controller {
public $data = array();
public function __construct()
{
parent::__construct();
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
$subdomain_name = $subdomain_arr[0];
$this->db->from('accounts')->where('subdomain', $subdomain_name);
$query = $this->db->get();
$account = $query->row();
$this->data['account_id'] = $account->id;
$this->data['account_name'] = $account->name;
}
public function index()
{
$data = $this->data; // contains your values from the constructor above
$data['title'] = "My Index"; // also use your $data array as normal
$this->load->view('index', $data);
}
public function clients()
{
$data = $this->data; // contains your values from the constructor above
$this->load->view('clients', $data);
}
}
NOTE: Even though CodeIgniter functions default to "public", it's best practice to declare them as such. See: Public functions vs Functions in CodeIgniter
Can something like this be done? I want to pass a variable from a public function to my view.
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->variable;
$this->load->view('home_view', $home_data);
}
public function a_function() {
public $variable = "cool";
}
//EDIT//
This is what I m actually trying to accomplish and I m stuck.
get_two gets two items from a table. I want to add the two items to two variables and pass them to the view.
public function get_two() {
$get_results = $this->home_model->get_two_brands();
if($get_results != false){
$html = '';
foreach($get_results as $result){
$html .= '<li>'.$result->brand.'</li>';
}
$result = array('status' => 'ok', 'content' => $html);
header('Content-type: application/json');
echo json_encode($result);
exit();
}
}//public function get_two() {
Should I create two functions like this? But I don't know how to pass the $get_results array from get_two to the below functions. I tried public $get_results = $this->model ... etc but that didn't work.
public function result_one() {
return $resultOne = $get_results[0];
}
public function result_two() {
return $resultTwo = $get_results[1];
}
I'm not sure I've got the question correctly but what you're trying to achieve is something like this?
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->a_function();
$this->load->view('home_view', $home_data);
}
public function a_function() {
return $variable = "cool";
}
/** AFTER EDIT **/
Things get complicated (possibly because of my english comprehension).
you said
get_two gets two items from a table. I want to add the two items to two variables and pass them to the view.
So from the function get_two() you need to get and use the result in this way?
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->get_two(); // <- here?
$this->load->view('home_view', $home_data);
}
So you can try with:
public function get_two() {
$get_results = $this->home_model->get_two_brands();
if($get_results != false){
return $get_results;
}
}
and then
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->get_two();
$this->load->view('home_view', $home_data);
}
and inside you home_view :
<?php
foreach($home_data['cool'] as $result){
echo '<li>'.$result->brand.'</li>';
}
?>
/** AFTER NEW QUESTION **/
I need the ids of the two choices as two distinct variables
So change the index function this way:
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->get_two(); // <- maybe you don't need this anymore
list($result1, $result2) = $this->get_two();
$home_data['resultId1'] = $result1->id;
$home_data['resultId2'] = $result2->id;
$this->load->view('home_view', $home_data);
}
Now you're able to use $home_data['resultId1'] and $home_data['resultId1'] inside your view.
You can also define the variable in the constructor, this is one way .
CODE:
public function __construct(){
$this->variable = "cool";
}
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->variable;
$this->load->view('home_view', $home_data);
}
I don't know the codeigniter framework so this is why I asked for a part of your view but it looks pretty simple as I check in the doc. And the doc's are not bad there.
Check for Adding Dynamic Data to the View
public_function() should return something, for ex:
function public_function() {
return 'groovy';
}
Then call it in the controller:
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->public_function();
$this->load->view('home_view', $home_data);
}
Then add to the view somewhere
<?php echo $home_data['cool'];?>
I assume it's wrapped in some class. So if you cannot return the value you need (for ex. function already returns something else) then do something like this:
class Someclass {
public $some_class_variable;
function public_function() {
$this->some_class_variable = 'groovy';
}
function index() {
$home_data['cool'] = $this->some_class_variable;
}
}