Filling dropdown dynamicly in Helper class in codeigniter - php

I'm using codeigniter and trying to write a Helper class for loading dropdown dynamicly according to model name and the attribute which is the Column name for view.
function dropdown($Class,$Attribute)
{
$Output=NULL;
define("CLASSNAME", $Class."_model");
define("ATTRIBUTE", $Attribute);
$CI = get_Instance();
$CI->load->model(CLASSNAME);
$FullData=$CI->CLASSNAME->get();
foreach ($FullData as $Data)
{
$Output.='<option value="'.$Data->Id.'">'.$Data->ATTRIBUTE.'</option>';
}
return $Output;
}
But when I call it means i am getting this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: News::$CLASSNAME
Filename: helpers/component_helper.php
Line Number: 11
Fatal error: Call to a member function get() on a non-object in C:\xampp\test\application\helpers\component_helper.php on line 11

it seems that you may have missed a syntax
$CI = get_Instance();
use $CI =& get_Instance(); instead and check again if the issue is resolved.

Related

codeigniter get current controller in post controller constructor hook

I'm trying to get the name of the called controller in a "post controller constructor" hook:
<?php
function authenticate() {
$CI =& get_instance();
$controller = $CI->router->class;
}
But I get this error message:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: hooks/post_controller_constructor.php
Line Number: 5
Backtrace:
File: / ... /CodeIgniter-3.0rc3/application/hooks/post_controller_constructor.php
Line: 5
Function: _error_handler
File: / ... /index.php
Line: 292
Function: require_once
Any ideas why?
<?php
function authenticate()
{
$CI = &get_instance();
$controller = $CI->router->fetch_class(); //Controller name
$method = $CI->router->fetch_method(); //Method name
}
i am not sure about this. but you can try like this.
fetch_class is used to fetch the class name and method is used to fetch the method name.
$this->router->fetch_class();
$this->router->fetch_method();

Call to a member function load() on a non-object when autoloading config files at CodeIgniter 3.0

I'm working in a small project with CI 3.0. I have set up /application/config/autoload.php for autoload app config file as follow:
$autoload['config'] = array('myappcfg');
Then at controller constructor I'm doing the following:
protected $facebookSession;
public function __construct()
{
$this->facebookSession = FacebookSession::newAppSession(
$this->config->item( 'facebook_app_id' ),
$this->config->item( 'facebook_app_secret' )
);
}
But I'm getting this notice:
A PHP Error was encountered Severity: Notice Message: Undefined
property: ShareFacebook::$config Filename:
controllers/ShareFacebook.php Line Number: 16
Backtrace:
File: /var/www/html/dts/myapp/application/controllers/ShareFacebook.php
Line: 16
Function: _error_handler
File: /var/www/html/dts/myapp/index.php
Line: 292
Function: require_once
Fatal error: Call to a member function load() on a non-object in /var/www/html/dts/myapp/application/controllers/ShareFacebook.php on line 16
Can any tell me what I'm doing wrong? Why I can't access $this->config at controller if config file is autoloaded?
You haven't stated as such so I am assuming that when you tried...
protected $facebookSession;
public function __construct()
{
echo $this->config->item('facebook_app_id');
echo '<br>';
echo $this->config->item('facebook_app_secret');
// $this->facebookSession = FacebookSession::newAppSession(
// $this->config->item( 'facebook_app_id' ),
// $this->config->item( 'facebook_app_secret' )
// );
}
You were able to see see the values of the two config items in question!
Update:
So you have the file called myappcfg.php saved under /application/config and the file itself contains something like...
<?php
$config['facebook_app_id'] = 'app_id';
$config['facebook_app_secret'] = 'app_secret';
Also check the spelling and case of your filenames. It can be as simple as that sometimes.
Well since I've found the solution I will share for others, the problem is the constructor since I was missing inherits from the CI_Controller. In a few words this was missing:
public function __construct() {
parent::__construct();
}

Trying to get property of non-object in PHP

I'm working with PHP and get error message:
Notice: Trying to get property of non-object in D:\xampp\htdocs\gmbr\fw\model\UserModel.php on line 234
This is my code:
# Static user
public static function is_admin()
{
return self::$auth->admin?true:false; // <<- This line 234
}
public static function username()
{
return self::$auth->username;
}
Probably what's going on is that the $auth object isn't initialized yet when your method is called.
Use a var_dump(self::$auth) to see what's going on.
If this is the case, I would use a Singleton design pattern with $auth, Running a self::getAuth() instead of self::$auth.

Calling a model in a Helper file using codeigniter

I want to write a function for loading dropdown in helper file and for that reason I want to Use my models in Helper file.
When I use this it give me the error:
$this->load->model("news_model");
The Error:
Fatal error: Using $this when not in object context in C:\xampp\test\application\helpers\component_helper.php on line 6
my method:
function dropdown($Class,$Attribute)
{
$Output=NULL;
$ClassName=$Class."_model";
$this->load->model($ClassName);
$FullData=$ClassName->get();
foreach ($FullData as $Data)
{
$Output.='<option value="'.$Data->Id.'">'.$Data->$Attribute.'</option>';
}
return $Output;
}
Thanks
Check this post:
function my_helper()
{
// Get a reference to the controller object
//$CI = get_instance();
// use this below
$CI = &get_instance();
// You may need to load the model if it hasn't been pre-loaded
$CI->load->model('my_model');
// Call a function of the model
$CI->my_model->do_something();
}
https://stackoverflow.com/a/2479485/1570901
Helper functions are functions. This means they are not bound to an object nor class. $this is therefor not avaible!
You have to get the instance of CodeIgniter-Core from somewhere else!
CodeIgniter provides a get_instance(); function for that:
$CI = get_instance();
Now wereever you had $this->load etc.
Replace $this with $CI to call on the CodeIgniter Core!
And by the way you are calling the model wrong. You have to access it via the CI-core:
$CI->$Classname->get();
Here is the correction for you code:
function dropdown($Class,$Attribute)
{
$CI = get_instance();
$Output=NULL;
$ClassName=$Class."_model";
$CI->load->model($ClassName);
$FullData=$ClassName->get();
foreach ($FullData as $Data)
{
$Output.='<option value="'.$Data->Id.'">'.$Data->$Attribute.'</option>';
}
return $Output;
}
Now instead of using $this, always use $CI inside this method.
Hope this helps.

Codeigniter model Undefined property

i have following code in the controller file which check for post varible pdf and execute following code
if(isset($_POST['pdf']) && $_POST['pdf']=="pdf")
{
$this->load->model('voutput_model');
$final_view = $data['frmReport'];
$PDF_Name = "report.pdf";
$this->votput_model->pdf($PDF_Name,$final_view);
}
I have following code in model file
class Voutput_model extends CI_Model{
public function __construct()
{
parent::__construct();
$this->CI = get_instance();
}
public function pdf($file_name,$filecontents){
$this->pdf_path = $this->config->item('pdf_path');
$this->load->library('htmltopdf/Html2fpdf');
$file['Attach_path'] = $this->pdf_path.$file_name;
$this->html2fpdf->generate_pdf($file['Attach_path'],$filecontents,'Y');
}
}
while i am executing the code i am getting error as below:
Severity: Notice
Message: Undefined property: Voutput::$Voutput_model
Filename: controllers/voutput.php
Fatal error: Call to a member function pdf() on a non-object in
C:\xampp\htdocs\asset\application\controllers\voutput.php
You need to load model before use . Try this
$this->load->model('votput_model');
$this->votput_model->pdf($PDF_Name,$final_view);

Categories