如何在codeigniter中的帮助文件中正确创建函数

时间:2014-01-31 19:15:35

标签: php codeigniter

我正在尝试学习代码点火器并创建帮助文件。我把它作为我的functions_helper.php文件,位于我的applications / helpers文件夹中:

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function __construct()
{
    parent::__construct();
    $this->load->model('user','',TRUE);
}
if (!function_exists('check_status')){

function check_status()
{
$CI = & get_instance();
    $result=$CI->user->get_status(); //this is line 14!!
        if($result)
    {
        $status_array=array();
        foreach($result as $row)
        {
            $status_array=array(
                'source' => $row->status,
                'current' => $row->id
                );

        if ($status_array['current'] == 1) {
    $status_array['current'] = "Live";
} else {
    $status_array['current'] = "Amazon is Down";
}

            $CI->session->set_userdata('status',$status_array);
        }
        return TRUE;


    }
    else
    {

        return false;
    }
} // END OF CHECK_STATUS FUNCTION
} 

从我能找到的一切,我正确地做到了,但我收到了这个错误:

PHP Fatal error:  Call to a member function get_status() on a non-object in function_helper.php on line 14. exactly what am i doing wrong?  Is this the best way to call a function?  Ultimately I am trying to get information returned from a db query.

我的get_status函数是:

//FUNCTION TO SEE THE STATUS OF AMAZON
function get_status() {

    $query = $this->db->query("select * from amz where active = 1");

    if($query->num_rows()==1) {
        return $query->row_array();
    }else  {
        return false;
    }
} //END OF GET_STATUS FUNCTION

1 个答案:

答案 0 :(得分:1)

删除此代码

function __construct()
{
    parent::__construct();
    $this->load->model('user','',TRUE);
}

辅助文件中没有OO。你没有类 - __construct()用于初始化类。

然后移动

$this->load->model('user','',TRUE);check_status函数

$CI =& get_instance();
$CI->load->model('user','','TRUE');