I have a custom library and need to pass some variables under specific method to my controller, then views.
Library code
Class Data {
function dviews($sitedata) {
$site1 = "www.site1.com";
$site2 = "site2.com";
}
}
My controller file
class Webs extends CI_Controller {
public function index()
{
$this->load->library('Data');
$data = $this->Data->dviews();
$this->load->view('pages/websites', $data);
}
}
and I pass the $data array using print_r on views.
is that correct or there is another way to get the list of these vars from library file and pass them?
You are setting variable inside method but returning nothing, so
Modify
Class Data {
function dviews($sitedata) {
$site1 = "www.site1.com";
$site2 = "site2.com";
}
}
To
Class Data {
function dviews($sitedata) {
return array( 'site1' => "www.site1.com", 'site2' => "site2.com");
}
}
Controller
class Webs extends CI_Controller {
public function index()
{
$this->load->library('Data');
$data = $this->Data->dviews();
// now print_r($data); will have array which you returned
$this->load->view('pages/websites', $data);
}
}
and in your view, pages/websites.php you can access like below
<?php echo $site1; ?>
<?php echo $site2; ?>
Related
I am new to codeigniter.
I want to set a global variable for my helpers, models, and controllers.
the variable is getting from database..
Here is the example:
don't know where to put the variable
$this->load->model('test');
$data = $this->test->get_code();
$this->code = $data["code"]; //main global variable
my helper
function test() {
if($this->code=="test") {
}
}
my controller
function index() {
echo $this->code;
}
my model
function get_data() {
$query = $this->db->get_where('my_table', array('code' => $this->code));
return $query->row_array();
}
As you can see my scripts above, $this->code is almost used for my helper, my controller, and my model.
Where should i put the variable so that i can access the variable by using $this->code only to all?
You can crate MY_Controller in extends MX_Controller in application/core folder and extends MY_Controller in Application/module create you own controller.EX see below
class MY_Controller extends MX_Controller {
public $code = ""; // Global variable
public function __construct() {
parent::__construct();
$this->load->model('test');
$data = $this->test->get_code();
$this->code = $data["code"]; //main global variable
}
}
class Admin extends MY_Controller {
function index() {
echo $this->code;
}
}
Try put data to session:
$this->load->model('test');
$data = $this->test->get_code();
$this->load->library('session');
$this->session->set_userdata('name' => $data );
and
function test() {
$this->load->library('session');
$data = $this->session->userdata('name');
if($data=="test") {
}
}
function index() {
$this->load->library('session');
$data = $this->session->userdata('name');
}
You may use session without load in every function,
add in config/autoload.php
$autoload['libraries'] = array('session');
In my controller I want to declare a global variable which will always fetch all areas from my db
currently I'm passing $data['dropdowns']
in all methods in my class
{
$data['dropdowns']=loading some other model method
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
the thing is I want to now send $data['area'] to all the views without having to declare it again and again in each method
$data['area']= $this->area_model->get_all_locations();
You want to add global variable , but as per my suggest to use global function to use any where to using to send parameter, so please check below my code.
Note : please load your model in application/config/autoload.php file
This is simple demo :
controller
{
$data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name');
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name,user_name');
$this->load->view('commons/header',$data);
}
Your model
function get_records($table_name,$field_name)
{
$this->db->select("$field_name");
$this->db->from("$table_name");
$query=$this->db->get();
return $query->result_array();
}
create a base_controller and placed in application/core
class base_controller extends CI_Controller
{
public $area = array();
function __construct()
{
// Call the Controller constructor
parent::__construct();
$this->get_area();
}
public function get_area() {
$this->load->model('area_model');
$this->area= $this->area_model->get_all_locations();
}
}
now $this->area is available in all controller which extends base_controller and all common functionality you can put here
class homepage extends base_controller{
function __construct()
{
// Call the Controller constructor
parent::__construct();
}
public function index()
{
$data = $this->area; // call this wherever u need
$this->load->view('commons/header',$data);
}
}
importantly $this->area; one can use directly inside view
Create a helper for your custom functions
Eg: custom_helper.php then load your custom helper in autoload.php
In custom_helper.php, create a function for getting area.
if (!function_exists('get_area')) {
function get_area() {
$CI = & get_instance();
$area= $CI->area_model->get_all_locations();
return $area;
}
}
You can call get_area() in your views without declaring in controllers..
Here is my controller code
public function index()
{
$this->load->model("mod_home");
$data['avoinics'] = $this->mod_home->getAvoinics();
$data['dir']="home";
$data['page']="index";
$this->load->view('main',$data);
}
for another page
public function about()
{
$this->load->model("mod_home");
$data['avoinics'] = $this->mod_home->getAvoinics();
$data['dir']="home";
$data['page']="about";
$this->load->view('main',$data);
}
But i don't want to send $data['avoinics'] again again. Is there any way to access a data from anypage.
How to use same data in a single view more than time.
foreach($avoinics as $avoinics):
$name=$avoinics->sc_name;
echo '<li>'.$name.'</li>';
endforeach;
if i use it again on same view page it's sowing error...
Yes, you can:
Create a global array
private $data = array();
In constructor
$this->load->model("mod_home");
$this->data['avoinics'] = $this->mod_home->getAvoinics();
Now your function will look like this
public function index() {
$this->data['dir']="home";
$this->data['page']="index";
$this->load->view('main',$this->data);
}
For second part, do not change the variable value
foreach($avoinics as $record){
echo '<li>'.$record['name'].'</li>';
}
$avoinics is intact now. You can use it again until you do not modify it.
A good example you might easily understand is when you need to call certain scripts or css files for a specific controller. You won't call it in every single page but yes in the constructor.
class yourController extends CI_Controller
{
private $data;
public function __construct()
{
$this->data['css'] = array('file1.css', 'file2.css');
$this->data['js'] = array('jquery.min.js', 'jquery-ui.min.js');
}
public function index()
{
$this->load->view('yourView', $this->data);
}
public function about()
{
$this->load->view('yourView', $this->data);
}
}
I recommend you to extend core ci_controller with my_controller and declare there your variable in constructor. Then in your new controllers extend your my_controller where you will have variable declaration.
I have my controller
class Page extends CI_Controller {
public function id() {
$this->load->model('content');
$page = $this->uri->segment(3, 0);
if($page == 0)
$page = $this->content->get_default_page($page);
$data['navigation'] = $this->content->getNav();
$data['pagename'] = $this->content->get_pagename($page);
$data['content'] = $this->content->get_content($page);
$this->load->view('main', $data);
}
}
Now I'll try to explain.
Im getting navigation, and navigation text from mysql (id, navName, navText).
then im returning those elements in views/main_view.php in url like: http://abc.com/page/id/1 etc...
Now i need to create other controller like mySuperDuperModule which have some functions not just text.
The problem is that if i create new controller like Gallery(), i need to copy all the stuff from Page() controller to make website show the same.
Is there any way not to do that ?
You can create base controller under /application/core/MY_Controller.php
Here MY_ is the value specified for $config['subclass_prefix'] in /application/config/config.php
class MY_Controller extends CI_Controller
{
protected $data;
public function __construct()
{
parent::__construct();
$this->data = array();
}
public function loadPage($page)
{
$this->load->model('content');
if($page == 0)
$page = $this->content->get_default_page(); // I hope this function will give default page from database table. (A Change from your code)
$this->data['navigation'] = $this->content->getNav();
$this->data['pagename'] = $this->content->get_pagename($page);
$this->data['content'] = $this->content->get_content($page);
}
}
Your modified Page Class in /application/controllers/page.php
class Page extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function id()
{
$page = $this->uri->segment(3, 0);
parent::loadPage($page);
$this->load->view('main', $this->data);
}
}
and your new gallery controller can be in /application/controllers/gallery.php
class Gallery extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function id()
{
$page = 10; // Assuming gallery page index is 10 in database table OR you can change any logic here.
parent::loadPage($page);
$this->load->view('main', $this->data);
}
}
you can create as many controllers as you want by extending MY_Controller.
You can pass these two lines to any controller
and they will display the same template.
$data['navigation'] = $this->content->getNav();
$data['pagename'] = $this->content->get_pagename($page);
If you still dont understand try to use this library
https://github.com/philsturgeon/codeigniter-template
I'm loading views in a main view from controller with codeigniter like this
Controller
public function product_modules($domain_id)
{
$this->load->model('/admin/Model_products', '', TRUE);
$data['product_boxes'] = $this->Model_products->getProducBoxes($domain_id);
$this->load->view('admin/dashboard',$data, null, true);
}
Main View
$this->view($_SERVER['REQUEST_URI']);
but if the requested uri is containing query strings, the view is not getting loaded and I get a type Unable to load the requested file: /admin/product_modules/1.php. What would be the best workaround to call views dynamically?
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Example extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function _remap($method, $params = array())
{
// dynamically assign the method with parameters support
$this->view($method, $params);
}
public function product_modules($domain_id)
{
$this->load->model('/admin/Model_products', '', TRUE);
$data['product_boxes'] = $this->Model_products->getProducBoxes($domain_id);
$this->load->view('admin/dashboard', $data, null, true);
}
public function view($method, $param1 = '', $param2 = '')
{
// params you can sent to models
$data['users'] = $this->model_name->get_user($param1);
// or views
$data['myvar'] = $param2;
// and load the view dynamically
$this->load->view($method, $data);
}
}
So
if the URL is http://example/controller/method then the above will search for method.php view file,
if URL is http://example/controller/product_modules/1 then it search for product_modules.php view file.
Hope this helps. Thanks!!