In my project, I have one search section with 3 select box. I passed the value to it using
$data['restaurant_all']=$this->restaurant_model->get_all('','','','','','yes')->result();
$data['state_all']=$this->state_model->get_all();
$data['menu_all']=$this->menu_model->get_all('all','','','','','','yes')->result();
$data['restaurant']=$this->input->post('restaurant');
$data['state']=$this->input->post('area');
$data['food_type']=$this->input->post('menu');
I need this statement in all my pages. In there any way to accomplish this without writing these statements in all the pages
a. extend the default controller by creating a file MY_Contoller.php at a suitable location.
b. create a custom class that will extend the default controller.
c. add a protected or public variable $data to custom class.
e. do something with data using __construct()
d. make every controller extend the custom controller.
e. you can access this variable like any other class variable.
example code:
MY_Controller.php
class APP extends CI_controller {
protected $data;
function __construct() {
parent::__construct();
$this->_init();
}
function _init() {
$this->data['state'] = $this->input->post('area');
}
}
normal controllers:
class Welcome extends APP {
function __construct() {
parent::__construct();
}
function view() {
/* pass this data value like normal data param */
$this->load->view('some_view', $this->data);
}
}
hope it helps.
Use constants, in /config/constants.php
Related
I made an array in MY_Controller class placed on core folder. In its constructor i fetched records from db so as to make navigation menu in my views. Since i have different page layouts so i cannot call the same header view every where. for this reason i made a core class as per my understanding which i am not sure is right or not. below is the code for my controller
class MY_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$data['parent'] = $this->Category_model->getParentCategories();
$data['child'] = $this->Category_model->getChildCategories();
}
}
my default controller is main
class Main extends MY_controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home/header',$data);
$this->load->view('home/footer');
}
Now in my header view i am receiving undefined variable parent and child error. I want this two variables available in all the views so that i do not have to define those two variables in every controller.
Thanks
You may try something like this:
class MY_controller extends CI_Controller
{
$commonData = array();
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$this->commonData['parent'] = $this->Category_model->getParentCategories();
$this->commonData['child'] = $this->Category_model->getChildCategories();
}
}
Then use $this->comonData in your index method instead of $data to pass to the view:
public function index()
{
$this->load->view('home/header', $this->comonData);
$this->load->view('home/footer');
}
Now it'll be available in the header view and since it's at the top of other views then you may use it further, unless you override it with other value in any class.
I have an array public variable loaded identically in every controller class that I have. The array variable contains language file to be passed to the view file.
Sample:public $data; $this->data = array('lbl_first_name'=>$this->lang->line('lbl_first_name'));. As language data goes plenty, so as the content of the array that holds the language file does also. How would I able to put this variable to a library or as a helper then loads it something like $this->load->library('language_data') or $this->load->helper('language_data') in every controller file? not the array variable with lots of language data anymore to be loaded in each controller I have. Thanks a lot. Sample codes are shown below:
Controller 1:
class Courses extends CI_Controller {
public $data;
public function __construct(){
parent::__construct();
$this->data =array(
//language file for menu item
'dropdown'=>$this->lang->line('dropdown'),
'dropdownedit'=>$this->lang->line('dropdownedit'),
'home'=>$this->lang->line('home'),
'menu_desc'=>$this->lang->line('menu_desc'),
'login'=>$this->lang->line('login'),
'login_desc'=>$this->lang->line('login_desc'),
'teacher'=>$this->lang->line('teacher'),
'logout'=>$this->lang->line('logout'),
'course_occasion'=>$this->lang->line('course_occasion'),
'courses'=>$this->lang->line('courses'),
'student'=>$this->lang->line('student'),
'tennant'=>$this->lang->line('tennant'),
'messages'=>$this->lang->line('messages'),
'sent_messages'=>$this->lang->line('sent_messages'),
//language file for forms
'course_edit_form_desc'=>$this->lang->line('course_edit_form_desc'),
'course_reg_form_desc'=>$this->lang->line('course_reg_form_desc'),
'course_view_list'=>$this->lang->line('course_view_list'),
'view_course_available_list'=>$this->lang->line('view_course_available_list'),
'lbl_course_name'=>$this->lang->line('lbl_course_name'),
'lbl_course_desc'=>$this->lang->line('lbl_course_desc'),
'lbl_tennant_name'=>$this->lang->line('lbl_tennant_name'),
'lbl_public'=>$this->lang->line('lbl_public'),
'lbl_not_public'=>$this->lang->line('lbl_not_public')
);
}
}
Controller 2: (Same as controller 1)
class Occasions extends CI_Controller {
public $data;
public function __construct(){
parent::__construct();
$this->data =array(
//language file for menu item
'dropdown'=>$this->lang->line('dropdown'),
'dropdownedit'=>$this->lang->line('dropdownedit'),
'home'=>$this->lang->line('home'),
'menu_desc'=>$this->lang->line('menu_desc'),
'login'=>$this->lang->line('login'),
'login_desc'=>$this->lang->line('login_desc'),
'teacher'=>$this->lang->line('teacher'),
'logout'=>$this->lang->line('logout'),
'course_occasion'=>$this->lang->line('course_occasion'),
'courses'=>$this->lang->line('courses'),
'student'=>$this->lang->line('student'),
'tennant'=>$this->lang->line('tennant'),
'messages'=>$this->lang->line('messages'),
'sent_messages'=>$this->lang->line('sent_messages'),
//language file for forms
'course_edit_form_desc'=>$this->lang->line('course_edit_form_desc'),
'course_reg_form_desc'=>$this->lang->line('course_reg_form_desc'),
'course_view_list'=>$this->lang->line('course_view_list'),
'view_course_available_list'=>$this->lang->line('view_course_available_list'),
'lbl_course_name'=>$this->lang->line('lbl_course_name'),
'lbl_course_desc'=>$this->lang->line('lbl_course_desc'),
'lbl_tennant_name'=>$this->lang->line('lbl_tennant_name'),
'lbl_public'=>$this->lang->line('lbl_public'),
'lbl_not_public'=>$this->lang->line('lbl_not_public')
);
}
}
Desired output:
Controller 1 and Controller 2:
$this->load->library('language_array');
or
$this->load->helper('language_array');
Not too sure I understand you, but I think this is what you want:
Firstly create a library or helper function return the array of lang.
create a controller like:
class MY_Controller extends CI_Controller {
public $data;
public function __construct(){
parent::__construct();
$this->load->library('language_array');
}
}
So now you have a controller which loads you lib or helper item right?
Then class Occasions extends MY_Controller and class Courses extends MY_Controller, so anything that you want all your controllers to have you put in MY_Controller which all you other controllers inherit from.
I am new to both PHP and Codeigniter.
I am loading a view from my controller. This view then has a form_open in it that directs to a function within that controller. Can I use variables previously set in my controller within that function?
For example, the constructor of the controller loads a model. Then, another function in this controller calls $this->model->someFunction($id) and sets the $id variable of my model to $id. Later, after a link is clicked in my view, it goes to a different function in the controller which then calls $this-model->printID(). This fails because in my model, $id is not set.
How can I achieve that a link goes to a function that accesses the same object (or model) that I modified earlier? That is, the final echo of $ID returns a blank string - the $ID is not set as I would have expected it to be.
Thanks in advance for the help.
My controller:
class Studio extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('studio_model','',TRUE);
}
function view($id)
{
$this->studio_model->setID($id);
$this->load->view('studio_view');
}
function signup(){
$ID = $this->input->post('ID');
$this->studio_model->signup($ID);
}
}
My model:
Class studio_model extends CI_Model{
public $ID;
function setID($id) {
$this->ID = $id;
}
function signup($ID){
echo $this->ID;
echo $classID;
}
I think that the answer to my question is related to the following: ellislab.com/forums/viewthread/185409/#876547 A PHP instance is not maintained and so each new call instantiates new classes, controllers, etc. As a result, global variables are no longer set if you have left a controller and then re-enter it later.
I am trying to get scroll news in any page of the website from database without having to pass the variable in every controller of the site. So I decided to use hooks instead of passing scroll variable in every controller.
I did create a class like this
class Scroll
{
function getScroller()
{
$data = array();
$CI =& get_instance();
$CI->db->where('a_status','active');
$CI->db->limit(4);
$CI->db->order_by('id','desc');
$Q = $CI->db->get('news');
if($Q->num_rows() > 0){
foreach($Q->result_array() as $row){
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
}
What I get now is
Severity: Notice
Message: Trying to get property of non-object
Call to a member function get() on a non-object in E:\xampp\htdocs\
Can anyone please help me how to do this ? Thanks I want to get scrollernews in any controller's view automatically without having to pass in each controller. Thanks
If you are defining that on a view level, there is no need for that.
You can define db requests directly in a view.
Other approach would be to have separated controller which with separate view and load it in the page through iframe. It's often used for "web widgets" that can be later on loaded in to another pages.
Extending the core class of CI Controller will should cause you less troubles.
http://ellislab.com/codeigniter/user-guide/general/core_classes.html
application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
//By do this, all controllers who use this class as parent controller
//will have $news in their views
$this->load->vars(array(
'news' => array()
));
}
}
application/controller/welcome.php
class Welcome extends MY_Controller {
public function index()
{
$this->load->view('welcome_message');
}
}
application/views/welcome_message.php
var_dump($news);
use separated library or helper and call that method on controller's constructs like :
class My_Controller extends CI_Controller(){
function __construct(){
parent::__construct();
//load your library
//call your library method
}
}
is there a reasonable way to access the view attribute "passedArgs" (or any similar)
/* view */
$this->passedArgs
from within a Helper?
I'd be happy to customize the _construct() of the helper or to customize the app_helper... but I don't want to have to pass $this->passedArgs into the helper on every view or usage.
Cake 2.x and 3.x
You can look up your variables in the _View object:
$this->_View->viewVars['foo'];
Cake 1.x
If you grab the current view object from within the helper you should be able to get to its passedArgs.
class SomeHelper extends AppHelper {
function __construct($settings = array()){
$this->passedArgs = ClassRegistry::getObject('view')->passedArgs;
}
}
Cake 1.2.x
If you grab the current view object from within the helper you should be able to get to its viewVars.
class SomeHelper extends AppHelper {
function __construct($settings = array()){
$this->viewVars = ClassRegistry::getObject('view')->viewVars;
}
}
Enjoy,
Nick
Have you tried just setting the view's value from the AppController?
class AppController extends Controller {
function beforeFilter() {
// other stuff
$this->set( 'passed_args', $this->params['pass'] );
}
}
Cake 3:
$this->getView()->get('my_var');