How do i properly create a function in a helper file in codeigniter

Jim

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
raidenace

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');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i redirect properly in CodeIgniter 3?

How do I properly test python function doing file operation?

How do I create a javascript helper function / class that can perform an async task before each exposed method

How do I create a Gulp Task to do minification and source maps to a .min file properly

How do I create a function to search within a CSV file?

How do I duplicate file n times and then create a function in .zshrc?

How do i create a function to export data to a excel file PHP

How do I properly send the "this" object to a function

How do i use timer function properly

How do I properly write with ?. in a JSX File

How to properly implement a helper function in Haskell

How do i upload .ini file in codeigniter?

How do I properly use asyncio.create_subprocess_shell() in an async def function?

How to transpose my upload function into Helper in codeigniter?

How to access codeigniter objects and variables in helper file?

How do I access my config variable in my helper file?

CodeIgniter: Create new helper?

How do I create a function with a function in Python?

How do i print my result by helper from one view to another view in codeigniter

How do I properly use a function in a do/while loop?

How to create ajax request to function and return in same file with DataTable and Codeigniter

How can I properly create this xml file in Powershell using XmlWriter()?

Can i use Model or Helper function in routes.php [codeigniter]

How do I properly create custom text codecs?

How do I properly create a new Instance of a subclass

How do I create hyperlinks properly using python's docxtpl?

How do I create a wrapper for a pragma and have it compile properly?

How do I properly use strings to create a Python MySQL query?

How do I properly create a matrix in C with malloc?