I want to declare a variable in a php class which can be used in all its functions. I am using the codeigniter framework & to have to pass data like base_url etc in each of the functions. Also I need all of such data in a single variable. For example $data['base_url']=base_url(), $data['title']="My_Site_title" etc & then send only a single variable $data (this is a common thing in codeigniter).
As per this question, I modified my code to:
class Search extends CI_Controller {
function Search() {
parent::__construct();
$this->load->model('student_model');
$this->load->helper('form');
$this->load->helper('url');
$this->output->enable_profiler(TRUE);
$this->data['base_url']=base_url(); //gives an error here
}
function index() {
$this->load->view('search_view', $this->data);
}
}
But still it gives an error at the marked place saying Undefined variable: data and Fatal error: Cannot access empty property in ..\search.php on line 16.
Here you go:
class Search extends CI_Controller {
protected $data = array();
function Search() {
parent::__construct();
$this->load->model('student_model');
$this->load->helper('form');
$this->load->helper('url');
$this->output->enable_profiler(TRUE);
$this->data['base_url']=base_url();
}
function index() {
$this->load->view('search_view', $this->data);
}
}
More about class properties in php: http://php.net/manual/en/language.oop5.properties.php
Related
I was passing my arguments correctly by URL and then I don't know what's wrong with the variables
Message: Missing argument 1 for Welcome::index()
Filename: controllers/Welcome.php
Line Number: 10
Backtrace:
File: /var/www/html/proyecto/application/controllers/Welcome.php Line:
10 Function: _error_handler
File: /var/www/html/proyecto/index.php Line: 292 Function:
require_once
Controller:
class Welcome extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('login_model');
}
public function index($password){
$this->load->view('header');
$this->load->view('index');
}
}
you need to pass the pariable to view page? Then use this code
class Welcome extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('login_model');
}
//value of password fetch to the variable $password
//eg:$password='123';
public function index($password){
$this->load->view('header');
$this->load->view('index',$password);
}
}
Your controller need to accept one argument
public function index(**$password**)
so you need to define what $password coming from.
or you just edit your controller
public function index($password = "")
to avoid the warning.
class Welcome extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('login_model');
}
public function index($password = "") {
$this->load->view('header');
$this->load->view('index',$password);
}
}
Thanks angel and weirdo
You should check your requested url. You may see the reference here in the "Passing URI Segments to your Functions" section.
class Welcome extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('login_model');
}
public function index($password){
$this->load->view('header');
$this->load->view('index');
}
}
From your code above, you create a required $password variable for each request. So you must put a value in the end of your url for the password.
i.e. example.com/index.php/Welcome/index/password
I hope this help. thank you.
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 am having problems with my code. When I try to debug my web application I get the following error message....Call to a member function find() on a non-object.....here is my code
class TeamsController extends AppController {
var $name = 'Teams';
function index() {
$this->set('teams', $this->team->find('all'));
}
function Welcome() {
}
}
I am trying to display records from my MySQL database. Now with that said, I did this tutorial and I followed the instructions down to the tee.....but somehow my code has bugs. The only difference between my code and the code of the tutorial I did is the variable names...and the controller names....and the I dont have the hello world function... Here is a sample of the code from the tutorial I did....
class PostsController extends AppController {
var $name = 'Posts';
function index() {
$this->set('posts', $this->Post->find('all'));
}
function hello_world() {
}
}
With that said, am I suppose to declare an instance of an object to get this to work?
It's likely a case sensitivity issue:
function index() {
$this->set('teams', $this->Team->find('all'));
}
If not, ensure your controller has access to the Teams model (e.g. $uses).
Using CodeIgniter 2.0.3 on XAMPP 1.7.7 I have code where I get the error as: Unable to load the requested file: home.php
The home.php code as follows stored in ./ci2/application/controllers:
class Home extends CI_Controller {
function Home()
{
parent::__construct();
}
function index()
{
$this->load->view('home');
}
While using it just notice:
controller's name is home.php
File named home.php must be present in ci/views
Incase file extention in view folder is not php eg like its html.
then load it using : $this->load->view('home.html');
the function u need to call from outside must be public So make it:
public function index() {... }
as u are calling the constructor for the class Home. Call it like:
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home');
}
Now it will work !
Your call to $this->load->view('home'); will be looking for home.php in /ci2/application/views/. That's the file it can't find.
I'm assuming you're calling http://myapp/index.php/home/ - that means it'll automatically call the index() method.
class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('home');
}
}
Please check the name of the file you have created in application/views/, because otherwise the code is correct.
I'm trying to get familiar with CI and I have run into a problem while trying to implement my Model. I get the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$OrderModel
Filename: controllers/home.php
Line Number: 12
My assumption was that I was breaking some convention with the naming of the Model. If
I change the line in question and call the model using all lower case:
$data['query'] = $this->ordermodel->get_all_workorder_names();
Nothing is returned to the view.. blank page; no source, no error.
Here is my model:
<?php
class OrderModel extends Model{
function OrderModel()
{
parent::Model();
$db = $this->load->database();
}
function get_all_workorder_names()
{
$this->db->select('name');
$query = $this->db->get('WorkOrder');
return $query->result();
}
}
?>
This is the calling controller:
<?php
class Home extends Controller{
function Home()
{
parent::Controller();
$this->load->model('ordermodel');
$this->load->helper('url');
}
function index()
{
$data['query'] = $this->OrderModel->get_all_workorder_names();
$this->load->view('Header');
//$this->load->view('Home',$data);
$this->load->view('Footer');
}
}
?>
What am I doing wrong? A side question: Is there a debugger available for PHP in Eclipse?
Use $this->ordermodel. The variable name will match whatever you use with load->model()
For further reading, you can check out CodeIgniter's User Guide entry on loading models here.