doupu1949 2014-01-31 19:15
浏览 38
已采纳

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

I am trying to learn code igniter and creating helper files. I have this as my functions_helper.php file located in my applications/helpers folder:

<?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
} 

From everything i can find, I am doing it correctly, yet I am getting this error:

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.

My get_status function is:

//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条回答 默认 最新

  • duan5362 2014-01-31 19:27
    关注

    Remove this code

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

    There is no OO in helper files. You do not have a class -__construct() are used to initialize classes.

    Then move

    $this->load->model('user','',TRUE); to the check_status function

    $CI =& get_instance();
    $CI->load->model('user','','TRUE');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面