here is some details for my question.
controller:
function get_info()
{
$user_id = $this->session->userdata['logged_in']['userid']
$this->load->model('message_model');
$msg_data['msg']=$this->message_model->get_user_msg($user_id);
$this->load->view('header',$msg_data);
$this->load->view('content');
$this->load->view('footer');
}
function others_goes_here()
{
$user_id = $this->session->userdata['logged_in']['userid']
$this->load->model('message_model');
$msg_data['msg']=$this->message_model->get_user_msg($user_id);
LOAD OTHER VIEWS.....
}
model:
function get_user_msg($user_id)
{
QUERY GOES HERE......
}
Now what i want to achieve is how can i make the get_user_msg to be called only once without calling it to all my other functions. Hope you guys can help me with this,
thank in advance.
You create a class variable and run get_user_msg from the constructor:
protected $msg_data;
function __construct() {
parent::__construct();
$this->load->model('message_model');
$user_id = $this->session->userdata['logged_in']['userid']
$this->msg_data = $this->message_model->get_user_msg($user_id);
}
function function get_info() {
$this->load->view('my_view', $this->msg_data);
}
Well use construct function for that
public $user_msg;
function __construct()
{
parent::__construct();
$this->load->model('message_model');
$user_id = $this->session->userdata['logged_in']['userid']
$this->user_msg=$this->message_model->get_user_msg($user_id);
}
function AllOtherFunctions(){
$this->user_msg; //you will have it ready every where
}
Related
<?php
class Dashboard extends CI_Controller {
private $menu;
public function __construct() {
parent::__construct();
$this->load->model('menu_model');
$this->$menu = $this->menu_model->generateTree($items = array());
}
public function index() {
$data['menu'] = $this->$menu;
$this->load->view('dashboard/dashboard', $data);
}
}
Hi guys, I get menu details at the constructor like this.I can get the result also. but it comes with the error
"A PHP Error was encountered
Severity: Notice
Message: Undefined variable: menu
"
You need to remove the dollar sign before menu, because you're referencing local variables
$this->menu = $this->menu_model->generateTree($items=array());
class Dashboard extends CI_Controller {
private $menu;
public function __construct() {
parent::__construct();
$this->load->model('menu_model');
$this->menu = $this->menu_model->generateTree($items = array());
}
public function index() {
$data['menu'] = $this->menu;
$this->load->view('dashboard/dashboard', $data);
}
}
Use $menu as below in your file, hope this solution help you.
$this->menu
Remove $ from $this->$menu
public function __construct()
{
parent::__construct();
$this->load->model('menu_model');
$this->menu = $this->menu_model->generateTree($items=array());
}
public function index()
{
$data['menu'] = $this->menu;
$this->load->view('dashboard/dashboard', $data);
}
while accessing the class variable you need not to use $ for second time after $this
so it would be like below code
for example
$this->variable_name
your code would be as follows
<?php
class Dashboard extends CI_Controller {
private $menu;
public function __construct() {
parent::__construct();
$this->load->model('menu_model');
$this->menu = $this->menu_model->generateTree($items = array());
}
public function index() {
$data['menu'] = $this->menu;
$this->load->view('dashboard/dashboard', $data);
}
}
Okay the issue is something like this
I have a function in AController
public function index()
{
$store = Store::(query)(to)(rows)->first();
return view('store.index', compact('store'));
}
Now in the same controller I have another function
public function abc()
{
return view('store.abc');
}
Now to this function I also want to send the compact('store') to the view abc I can just add the query again in the abc() function but that would be lazy and make performance issues. Is there a way that I can access $store object in other functions too?
If I understand you correctly you want to access the same query from two places. So extract getting stores to another method like
private function store()
{
$minutes = 10; // set here
return Cache::remember('users', $minutes, function () {
return Store::(query)(to)(rows)->first();
});
}
Additionally I have cached the query. So it get executed once at a defiened time.
Then access it from other two methods like,
public function index()
{
$store = $this->store();
return view('store.index', compact('store'));
}
public function abc()
{
$store = $this->store();
return view('store.abc', compact('store'));
}
class StoreController extends Controller
{
public function index()
{
return view('admin.store',['data' => $this->getSetting()]);
}
public function getStoreData()
{
//get your data here, for example
$data = Store::where('status',1)->first();
//get all data
//$data = Store::all();
return ($data);
}
}
Try the following. Not testing but it should work for you.
class AController
{
public function getStore()
{
$store = Store::(query)(to)(rows)->first();
return compact('store');
}
public function index()
{
return view('store.index', $this->getStore());
}
public function abc()
{
return view('store.abc', $this->getStore());
}
}
The point is to execute a function in the constructor, like this for example.
$clas = new andrestest();
class andrestest{
function __construct(){
FunctionName();
}
public function FunctionName()
{
echo 10;
}
}
You can do two things
function __construct(){
$this->FunctionName();
}
or
function __construct(){
self::FunctionName();
}
I'd like to understand better how calls to functions work in OOP. I have the following sample:
class SomeClass {
function __construct(){
//run function do()
//run function include()
//run function run()
}
public function do($foo){
//do some stuff
}
public function include(){
require_once( CONSTANT . 'required.php' );
}
public function run(){
required_func();
}
}
$load_class = new SomeClass();
in required.php:
function required_func(){
$customerInfo = "info";
$customer = $this -> do($customerInfo); //--> This isn't right
return $customer;
}
What I'm trying to do is to have required_func() run the do() with the $customerInfo. So essentially: How to call a Class public function from another function included in the require_once file? Am I even remotely on track here?
Thanks for your help
$this isn't in scope for function required_func()
class SomeClass {
function __construct(){
//run function do()
//run function include()
//run function run()
}
public function do($foo){
//do some stuff
}
public function include(){
require_once( CONSTANT . 'required.php' );
}
public function run(){
required_func($this);
}
}
$load_class = new SomeClass();
and
function required_func($customerObject){
$customerInfo = "info";
$customer = $customerObject->do($customerInfo);
return $customer;
}
I have this controller in Code Igniter application. A value is initialized in the constructor.
class Cat extends CI_Controller {
private $data = array();
public function __construct() {
parent::__construct();
$this->data['sound'] = "meow";
}
public function index() {
$this->load->view('myCatPage', $data);
}
}
The view "views/myCatPage.php" looks like this. It is simple.
<?= $sound ?>
Why does PHP note this error?
Message: Undefined variable: sound
I thought I sent this variable as a key in the array ($data) I sent into the view.
I have tried
$this->load->view('myCatPage', $this->data);
but that strangely fails too.
class Cat extends CI_Controller {
var $data = array();
public function __construct() {
parent::__construct();
$this->data['sound'] = "meow";
}
public function index() {
$this->load->view('myCatPage', $this->data);
}
}