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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀