CodeIgniter Forums
function help [solved] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: function help [solved] (/showthread.php?tid=49199)



function help [solved] - El Forum - 02-10-2012

[eluser]superfake123[/eluser]
Hello,

How/where can I put this function so that I can access it from within any controller/model or view etc?

Code:
function get_setting($setting)
{
$query = $this->db->where('setting_name',$setting)->get('settings')->row();
return $query->value;
}

I tried putting it in a helper but the db class doesn't work from helpers I guess?


Thanks!


function help [solved] - El Forum - 02-10-2012

[eluser]CroNiX[/eluser]
Lots of ways, actually. You can extend the Controller with MY_Controller and put it there. You could create a library. You could also put it in a helper, but not the way you are doing it. A helper is a regular php function, so it doesn't know anything about CI ($this) unless you bring the CI superglobal into it, like:
Code:
function get_setting($setting)
{
  $CI =& get_instance();  //now access CI using $CI instead of $this
  $query = $CI->db->where('setting_name',$setting)->get('settings')->row();
  return $query->value;
}




function help [solved] - El Forum - 02-10-2012

[eluser]superfake123[/eluser]
oh I like the $CI =& get_instance() way! thank you very much!