CodeIgniter: get_instance inside My_Lang - php

I found this useful Internationalization code:
http://pastebin.com/SyKmPYTX
everything works well except I am unable to use CI functions inside this class .
I want to set $languages and $special variable from DB .
but when I am using $CI =& get_instance(); in instance function its showing following error :
Fatal error: Class 'CI_Controller' not found in /system/core/CodeIgniter.php on line 231

The language class is loaded before the CodeIgniter instance exists, which is why you get the error.
You can use a post_controller_constructor hook to set your variables.
Here is a thread from the CodeIgniter forums where someone is tried to do something similar: http://codeigniter.com/forums/viewthread/108639/

The easiest way
in My_Lang.php
var $languages = array();
function __construct()
{
parent::__construct();
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->query( 'SELECT * FROM languages');
$result = $query->result();
foreach( $result as $row )
{
$this->languages[$row->short_name] = $row->full_name;
}
}
i did this and is working fine :)) i also added default_uri in foreach.

Related

Php Fatal Error Class not found

I am trying to debug an issue with a php website that was working and for some reason stopped. When I try to access the website via http it does not work so I check the server logs in Linux and this is what I get:
PHP Fatal error: Uncaught Error: Class 'HomePageBanner' not found
The beginning of the file has code
<?php
require_once 'config.php';
include "checkiflogin.php";
$condition = "";
$objDreamVacationGallery = new HomePageBanner();
$data = $objDreamVacationGallery->selectAllRecords($condition, $sort_field, $sort_order, $start, $limit);
?>
The index.php is stored in the root directory and the HomePageBanner is stored in root/classes. The code for HomePageBanner looks something like
<?php
class HomePageBanner extends DataBase
{
public $db_table = 'tblxxx';
public $data = '';
public function __construct($data=''){ /* VALUE ASSIGNMENT */
parent::__construct();
if($data!=''){
$this->data = $data;
}
}
public function __destruct(){
parent::__destruct();
}
Would appreciate any help.
Thanks
You need to require_once homepagebanner class first as below
require_once('classes/HomePageBanner.php');
and as i seen in comments you are using linux make sure classes and HomePageBanner in code are in the same case as in the your directory /var/www/html

Using $this when not in object context into the same class

I'm trying to build a View class for Smarty templates so I could call templates like in Laravel View::make('template');
But, for first time I'm getting this error. I found plenty of responses fixing the issue but I cannot fix mine. I don't know why so I'm starting to be little bit fed up... hehehehe
This is the class:
class View {
public $engine;
protected $tmpl_folder = 'tmpl';
protected $tmpl_compiled = 'tmpl_c';
protected $cache = true;
protected $force = true;
function __construct() {
$this->engine = new Smarty();
$this->engine->compile_check = $this->cache;
$this->engine->force_compile = $this->force;
$this->engine->template_dir = './' . $this->tmpl_folder . '/';
$this->engine->compile_dir = './' . $this->tmpl_compiled;
}
static function make($t, $args = '') {
if(!empty($args))
$this->engine->assing($args);
$this->engine->display($t.'.tpl');
exit();
}}
Error is launching in method make(), line <b>$this->engine->assing($args)</b>.
I tried to change declaration of $engine variable to public, private, protected and static... nothing...
I declared make() like public, public static, like in the example above and the same with no "static".... nothing...
I tried to change $this-> for self::... nothing...
I don't know what else can I do! Some advice please!
to get this working quickly instantiate your View class as an object like this:
$obj_view = new View();
then make your call to the make() method like this:
$obj_view->make('template');
I'd also declare scope for your methods (functions) (both of them should be public) - you did it for the properties (variables), so why not for the methods as well and remove static from the make()
why are you calling exit() in make()? I hope this is for debug ('Dead programs tell no lies')
assing() made me lol, a great typo! :)
TIP: avoid using bad language in code (in variable names, comments or debug) these are viewable by other developers and can, unintentionally, leak to the end user - it's not professional!

Calling PHP Class Function In My Page

I have a class file: we'll call it class.php. The functionality of that is to grab info from an ini file (yeah, I posted the question about security and was given the great suggestion to use either a config file or an ini file to hold the DB information).
Essentially, my class is this:
<?php
class myClass
{
public function getAttached()
{
$file = "../../myFile.ini";
if (!$settings = parse_ini_file($file, TRUE)) throw new exception('Unable to open ' . $file . '.');
$hoost = $settings['mysqli']['default_host'];
$useer = $settings['mysqli']['default_user'];
$pazz = $settings['mysqli']['default_pw'];
$dbs = $settings['mysqli']['default_db'];
$con = mysqli_connect($hoost ,$useer, $pazz, $dbs);
return $con;
}
}
$obj = new myClass();
$obj->getAttached();
$vals = $obj->getAttached();
//echo $vals; //didn't know if I should echo this or not.
?>
I want to call this in my somePage.php file to make my "mysqli" connection and go from there...
I tried this:
require_once('class.php');
getAttached();
Obviously that didn't work (I knew it wouldn't but - I did it anyway just to see if "maybe"), so - how do I call that function from my class file in the regular php page?
Any thoughts would be appreciated.
Thanks in advance.
You need to make an instance of the class before calling the functions as they're not static.
require_once('class.php');
$myClass = new myClass();
$myClass-> getAttached();
or, like I said above you could make the function static.
public static function myFunction() {
//etc...
}
Then to call it you would use:
require_once('class.php');
myClass::getAttached();
You have to instanciate your class first, the same way you did it in you class.php file:
$myclass = new myClass();
$myClass->getAttached();
Note that if your method can be used without any relation with your class, you could make it static:
public static function getAttached() {
// ...
}
And use it without having to instanciate your class:
myClass::getAttached();
Your getAttached() method within the myClass ,create the instance for the class and call
the function
$call = new myClass();
$call->getAttached();
Given answers are correct, but if you keep your class file as you posted, you have object already in $obj so there is no need to make new one. If it is just temporary you can ignore my post.
One more thing:
$obj->getAttached(); // this line is not needed, as you call this function in next line
$vals = $obj->getAttached();

Fatal error: Call to a member function get_row()

I'm working for admin login please anybody fix that..
Email: <?php echo $admin->get_email(); ?>
Fatal error: Call to a member function get_row() on a non-object in D:\MyWebSite\business_design\admin\admin-class.php on line 82
The code:
public function get_email() {
$username = $_SESSION['admin_login'];
global $db;
$info = $db->get_row("SELECT `email` FROM `user` WHERE `username` = '" . $db->escape($username) . "'");
if(is_object($info))
return $info->email;
else
return '';
}
Okay, are you using a framework?
Your $db variable is not instantiated, so when you call $db->get_row:
PHP can't find the $db object; and so,
get_row() can't exist.
To start with, make sure you know which class $db should be referring to. The same class will have the function "get_row"
First you have to include that class file in your php scrip file. That can be done easily by following script -
function __autoload($class_name){
require_once("RELATIVE_ADDRESS_OF_THE_CLASS".$class_name.".php");
}
Say for example you have your class is Database, so you should first instantiate $db as below -
$db = new Database();
Later if all the scripts are working properly in "Database" class, it should work straightaway...
Hope it helps..

Is it possible to set smarty directories in a derived constructor?

Hey, I'm trying to do something like this:
<?php
class MySmarty extends Smarty {
public function __construct() {
parent::__construct();
$smartyRoot = '/home/smarty/';
parent::setTemplateDir($smartyRoot. "/templates/");
parent::setCompileDir($smartyRoot."/templates_c/");
parent::setCacheDir($smartyRoot."/cache/");
parent::setConfigDir($smartyRoot."/configs/");
}
}
$smarty = new MySmarty();
$smarty->display("index.tpl");
?>
But it fails with a SmartyException("Unable to load template file"). From smarty_internal_template.php line 163, which looks like it checks for the existence of $this before doing any display.
My system seems to be set up correctly, since the suggested way of doing it (calling $smarty->set*Dir($smartyRoot.'foo'); works.
Any ideas?
You can use $this even in context of the constructor. So try:
$this->setTemplateDir($smartyRoot. "/templates/");
$this->setCompileDir($smartyRoot."/templates_c/");
$this->setCacheDir($smartyRoot."/cache/");
$this->setConfigDir($smartyRoot."/configs/");
Should work.
The template dir can be an array, and it might internally do that. There is also addTemplateDir(). Smarty will traverse them in order. Using $this-> is the correct approach for the constructor.
Are you sure the problem is from that code? I use in all my apps code like this:
class MySmarty extends Smarty {
function __construct() {
global $path, $template_dir, $production;
parent::__construct();
$this->template_dir = $template_dir;
$this->compile_dir = "$template_dir/tpl_c";
$this->config_dir = "$template_dir/configs";
$this->cache_dir = "$template_dir/cache";
(...)

Categories