Reputation: 205
i am trying to call a function in core php file from a common_helper.php file which is in application/helpers folder of codeigniter. The function which is wrtten in helper file is used in member controller also . So in codeigniter site this is working. But not in core php which is outside.Please help me
File structure:
project/Test.php
application/controllers/Member
application/helpers/common_helper.php
here is my common_helper.php
function reg_code()
{
$CI =& get_instance();
echo "in registration_code";
$registration_code = get_reg_code();
return $registration_code;
}
function check_code($registration_code)
{
$CI =& get_instance();
$sql = "SELECT * FROM membership WHERE reg_code = '$registration_code'";
$query = $CI->db->query($sql);
if($query->num_rows() > 0){
return FALSE;
}else{
return TRUE;
}
}
function get_reg_code()
{
$CI =& get_instance();
$CI->load->helper('string');
$alha_result = random_string('alpha',1);
$numeric_result = random_string('numeric',3);
$reg_code = $alha_result.$numeric_result;
$reg_code = strtoupper($reg_code);
//check if code exists
if(check_code($reg_code)){
return $reg_code;
}else{
get_reg_code();
}
return $reg_code;
}
Test.php
include_once "../application/helpers/common_helper.php";
$reg = reg_code();
echo "Reg Code :".$reg;
Upvotes: 3
Views: 1993
Reputation: 1067
My Custom Helper -- (custom_helper.php)
<?php
if(!function_exists('p'))
{
function p($arr)
{
echo '<pre>';
print_r($arr);
echo '</pre>';
die;
}
}
?>
and then i have created i sample.php file in project root directory
sample.php --
<?php
include('./application/helpers/custom_helper.php');
$arr = array('1','2','3','4');
p($arr);
?>
Output --
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
please use this way, it will work 100% and do not use below line in helper file using that we can access helper methods outside the application folder.
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Upvotes: 0
Reputation: 2901
You use $CI = & get_instance(); in your helper functions after this call there is main framework class object in that $CI variable, that's why when you use it in Controller everything work. But when you use it inside core php file, you don't initialize framework classes, so method call get_instance() fails and nothing work.
Upvotes: 0