Welcome Guest, Not a member yet? Register   Sign In
how can I access database in a helper function ?
#1

[eluser]core-coder[/eluser]
Hi all,

I want to call a particular function many times, so I put it in a helper. but I am not able to use any database operations in my helper function, How can I perform database operations from a helper function ?

please help me

Thank you guys
#2

[eluser]Thorpe Obazee[/eluser]
Code:
function i_rock()
{
    $CI =& get_instance();
    $CI->db->get('table_name');

    // OR

    $CI->load->model('your_model');
    $result = $CI->db->get_the_contents_of_your_table();
}

Use the get_instance() function.
#3

[eluser]n0xie[/eluser]
Usually helpers are used in the view to format and output a particular datablock. If you need database logic I would recommend either moving your code to a library, model or making it part of of one of your base controllers.
#4

[eluser]Zeeshan Rasool[/eluser]
[quote author="n0xie" date="1268752620"]Usually helpers are used in the view to format and output a particular datablock. If you need database logic I would recommend either moving your code to a library, model or making it part of of one of your base controllers.[/quote]

Yeah, absolutely right suggestion.
#5

[eluser]sudinem[/eluser]
Can you please provide an example to show how to use model's function in your own library????


Sudinem
#6

[eluser]sudinem[/eluser]
[quote author="PDP" date="1268738102"]
Code:
function i_rock()
{
    $CI =& get_instance();
    $CI->db->get('table_name');

    // OR

    $CI->load->model('your_model');
    $result = $CI->db->get_the_contents_of_your_table();
}

This above code is not working...

You have load the model but you haven't call the function inside the model..How to call the functions of model in helper.

Sudinem
#7

[eluser]danmontgomery[/eluser]
get_instance() returns a copy of the codeigniter instance, you treat $CI like the controller object.

Code:
// In Controller
$this->load->model('my_model');
$this->my_model->my_function();

// Outside of controller
$CI =& get_instance();
$CI->load->model('my_model');
$CI->my_model->my_function();
#8

[eluser]sudinem[/eluser]
$ci->model_name->somefunction();

Doesn't work when I tried on my own library.....
#9

[eluser]danmontgomery[/eluser]
Try as I might, I'm unable to read minds... You're going to have to give some details (error messages) and post some code




Theme © iAndrew 2016 - Forum software by © MyBB