I am a newbie in Codeigniter. I am doing a project in which in every pages, I would like to include a drop down for showing multi-languages. For this, I am including a view file in one of the view file in another controller as:
<?php $this->load->view('language/alllang');?>
In alllang.php, I would like to include the code for displaying drop down. For this I have created a controller LanguageController with the following code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Language extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
function alllang()
{
$data['val']="hhh"; // for testing
$this->load->view('alllang',$data);
exit;
}
}
Code for alllang.php is:
<?php
echo $val;
?>
But I am getting an error like this:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: val
Filename: language/alllang.php
Line Number: 2
This code is only for testing purpose (not the code for multilanguage). How can I set the value to be included in view file from controller.
Edited for database access functionality:
You could just create a library file and save it in application/libraries, call it "language_lib.php":
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Language_lib{
function __construct(){
$this->CI =& get_instance();
}
function lang_dropdown(){
//db queries:
$q = $this->CI->db->select('*')->from('table')->get()->result_array();
$html = … //your dropdown code here using $q
return $html;
}
}
Next, in application/config/autoload.php:
$autoload['libraries'] = array('language_lib');
Now in each controller method that needs the drop down, e.g. the index method:
function index(){
$data['dropdown'] = $this->language_lib->lang_dropdown();
$this->load->view('some_view', $data);
}
You can access this in the view with <?php echo $dropdown; ?>
Your explanation is a bit confusing, but by doing this
$this->load->view('language/alllang');
You're not calling a controller, you're calling a view only, without passing any data. If I get it right, you have a parent view, then inside it you'd like to call a language drop down view? Well, in this case a drop down view should be called with data array, and data should be coming from controller calling a parent view. Does it make sense?
Related
I want to make my logo global so that I can access it anywhere in my website. For that I am declaring $logo in a custom parent controller MY_Authorization and every class is extended by this custom controller.
but when i call the $logo variable in my view a php error occurs that undefined variable: $logo
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Authorization extends CI_Controller {
var $logoCont='logo';
function __construct()
{
parent::__construct();
$CI = & get_instance();
$CI->load->library('session');
$CI->load->helper('url');
if ( !$this->session->userdata('loginuser'))
{
redirect('login');
}
}
}
In my view i am calling logo like this
<img src="<?php echo base_url().sprintf("uploads/%s", $logoCont)?>"alt="logo">
Create custom helper file and placed in helper folder
Navigate to application/helper
List item
Create a new custom file
example : my_custom_helper.php
function logo(){
$ci = & get_instance();
$result = $ci->db->query("you custom query here");
//fetch the logo from database and store in the variable and return this variable
return $result;
}
autoload this helper in autoload.php file at (application/config/autoload.php)
find the variable $autoload['helper'] = array();
add your custom helper there like $autoload['helper'] = array('my_custom_helper.php);
Now the logo() function is global and it will available everywhere in controller/model/view ,and whenever change made in helper function it will be affect the application where logo() function is used.
Your data is not being passed on to the view.
if ( !$this->session->userdata('loginuser'))
{
---added this---
$this->session->set_flashdata('item', 'value');
// OR $this->session->set_flashdata($data); $data is of type array
----------------
redirect('login');
}
Now your data is set in the session (and will be removed after the next request), you can access this data in the view by using $this->session->flashdata('item').
https://codeigniter.com/user_guide/libraries/sessions.html#flashdata
Hope this helps.
I make simple template use code codeigniter.
my code like this.......
This is template.php class
class Template {
protected $_ci;
function _construct() {
$this->_ci=&get_instance();
}
function display($template, $data = null) {
$data['_content'] = $this->_ci->load->view($template, $data, TRUE);
$data['_header'] = $this->_ci->load->view('template/header', $data, TRUE);
$data['_top_menu'] = $this->_ci->load->view('template/menu', $data, TRUE);
$data['_right_menu'] = $this->_ci->load->view('template/sidebar', $data, TRUE);
$this->_ci->load->view('/template.php', $data);
}
}
and this is my controller welcome.php class
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Welcome extends CI_Controller {
function __construct() {
parent::__construct(); //you always have to call parent's constructor before ANYTHING
$this->load->library('template');
$this->load->helper('url');
}
public function index() {
$this->template->display('welcome_message');
}
function contoh_parameter() {
$this->template->display('view_parameter', array('judul' => 'Judul View'));
}
}
and this is my view class, welcome_message.php class
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
But I get error like this :
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/template.php
Line Number: 25
Sorry I am still a newbie in codeigniter and php. Please help me.
First of all your construct method isn't right it should be __construct instead of _construct
Second you use $this->_ci->load->view('/template.php', $data); when you load a view you shouldn't use any slash at the beginning path up to view directory is available to CI. So if your view file is template.php in CI view folder then use it in this way $this->_ci->load->view('template', $data);. Also you use .php extension CI self adds that extension.
I'm new to codeigniter and I have two controllers: utility.phpand welcome.php.
In utility.php, I have functions:
function getdata() {
//code here
}
function logdata() {
//code here
}
Inside welcome.php, I have this function:
function showpage() {
//some code here
//call functions here
}
What I want to do is inside my welcome.php, I want to call the functions from utility.php. How do I do this? Thanks.
Refrence from here
To extend controller please either follow this tutorial or see some code below.
differences between private/public/protected
make a file in folder /application/core/ named MY_Controller.php
Within that file have some code like
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $data = Array(); //protected variables goes here its declaration
function __construct() {
parent::__construct();
$this->output->enable_profiler(FALSE); // I keep this here so I dont have to manualy edit each controller to see profiler or not
$this->load->model('some_model'); //this can be also done in autoload...
//load helpers and everything here like form_helper etc
}
protected function protectedOne() {
}
public function publicOne() {
}
private function _privateOne() {
}
protected function render($view_file) {
$this->load->view('header_view');
if ($this->_is_admin()) $this->load->view('admin_menu_view');
$this->load->view($view_file . '_view', $this->data); //note all my view files are named <name>_view.php
$this->load->view('footer_view');
}
private function _isAdmin() {
return TRUE;
}
}
and now in any of yours existing controllers just edit 1st or 2nd line where
class <controller_name> extends MY_Controller {
and you are done
also note that all your variables that are meant to be used in view are in this variable (array) $this->data
example of some controller that is extended by MY_Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class About extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['today'] = date('Y-m-d'); //in view it will be $today;
$this->render('page/about_us'); //calling common function declared in MY_Controller
}
}
You don't is the short answer.
The point of MVC is to have your code well organized.
What you can do though is create a library in the libraries folder where you put methods you need in more than one controller.
For example you can make a mylog library, where you can put all your log related stuff. In any controller you will then call:
$this->load->library('mylog');
$this->mylog->logdata();
Besides functions that deal with data models should reside in models. You can call any model from any controller in CI
That is out of concept, if you want to call code in different controllers do following:
create helper and call function whenever (even in view) you want.
extend CI_Controller so you can use one or more functions in any controller that is extended.
Lets start with helper:
create file in folder application/helpers/summation_helper.php
use following sample of code
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function sum($a, $b) {
//$CI =& get_instance();
// if you want to use $this variable you need to get instance of it so now instead of using $this->load... you may use $CI->load
$return = $a + $b;
return number_format($return, 3);
}
If you are going to use your helper in many controllers/views please load it in autoload, otherwise just load it manualy $this->load->helper('summation');
Extending Controller_CI: this is way better approach if you are using database. Please follow this tutorial, which explains it all.
*I have crafted an answer just before site went down posting this from cellphone.
Hi all I am having an error and I just can't see why codeigniter is throwing the error:
controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Management extends CI_Controller {
public function index(){
//calls login page view
$this->managementView();
}
public function managementView(){
//loads course page view
$users['users'] = $this->management_model->getInfo();
$this->load->view("header");
$this->load->view("users", $users);
$this->load->view("footer");
}
}
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Management_model extends CI_Model{
function getInfo(){
$query = $this->db->get("users");
$result = $query->result_array();
return $result;
}
}
I am receiving the following error, I just can't seem to see what the hell I'm doing wrong - I'm new to web stuff so don't know what these errors indicate:
Fatal error: Call to a member function getInfo() on a non-object
also see this:
Severity: Notice
Message: Undefined property: Management::$management_model
Filename: controllers/management.php
Line Number: 12
I can't see the issue? - If anyone could point it out would be much appreciated.
you need to load the model before calling any function from the model.
for eg:
$this->load->model('management_model');
$users['users'] = $this->management_model->getInfo();
or you can load it via the constructor.
function __construct() {
parent::__construct();
$this->load->model('management_model');
}
}
the notice you are getting is clearly telling about the undefined property management model
you can see more about this here
You dont appear to ever set the management_model property in your controller.
I would expect to see something like this somewhere in your controller:
$this->management_model = new Management_model();
Initially you can load the model like this
$this->load->model('management_model');
then only u can use the object to call function
you need some more clarity Click
use
$this->load->model('management_model');
before
$users['users'] = $this->management_model->getInfo();
I have a controller, which calls another controller, and after that a DB call happens, now when i run the DB call it doesn't work, the error i get is
<p>Message: Undefined property: Advertisement::$admodelobj</p>
<p>Filename: v1/Advertisement.php</p>
This is how my controller looks
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Advertisement extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function getads()
{
//another controller call
require_once 'application/controllers/v1/ip2locale.php';
$GetLocale = new ip2locale();
$range = $GetLocale->index($clientip);
//now the db call
$this->load->model('ads_model','admodelobj');
$campaigns = $this->admodelobj->getCampaigns('desktop',1.00,'IN');
}
}
Now if i just put the db call above the "Another controller call" it works good, but right after the "another controller call" it gives me the error, what could be the issue?
Fixed it, one has to load the controller back again to avoid the confusion, and it starts working :)
$ci =& get_instance();
$ci->load->model('ads_model','admodelobj');
$campaigns = $ci->admodelobj->getCampaigns($deviceTypeValue,$payout,$locale);
Great Brij Raj Singh... Just tell, I have one way :
public function getads(){
$this->load->library('../controllers/ip2locale');
$range = $this->ip2locale->index($clientip);
.....
}
And it would works :)