Welcome Guest, Not a member yet? Register   Sign In
How to instance db class on helper
#1

[eluser]pengenbelajarCI[/eluser]
Hi, I'm newbie here. And now I'm trying to make a helper. The function in this helper is to show the sitename from config table on database.

Helper
Code:
function sitename($db) {
    $db->select('sitename');
    $query = $db->get('config');    
    $row = $query->row();
    return $row->sitename;    
}

Controller
Code:
class Front extends Controller {

    function Front()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $data['sitename'] = sitename($this->db);
        $this->load->view('main',$data);
    }
}

Is there anyway to make the function in the model just sitename() not sitename($db), so on the controller just type $data['sitename'] = sitename(); not $data['sitename'] = sitename($this->db);.


Sorry for my bad English
#2

[eluser]Thorpe Obazee[/eluser]
here's a sample of accessing the database with a helper

Code:
if ( ! function_exists('the_author'))
{
    function the_author($uri)
    {
        $ci =& get_instance();
        $ci->db->select('users.username as users_username');
        $ci->db->join('posts', 'posts.user_id = users.id');
        $query = $ci->db->get_where('users', array('posts.uri' => $uri));
        $user = $query->row();
        echo $user->users_username;
    }
}

usage:
Code:
the_author('the post');
#3

[eluser]pengenbelajarCI[/eluser]
thanks.
just put $ci =& get_instance() at the function.

solved




Theme © iAndrew 2016 - Forum software by © MyBB