Welcome Guest, Not a member yet? Register   Sign In
When to Build your own Library ?
#5

[eluser]CroNiX[/eluser]
Take a look at a helper, like /system/helpers/form_helper.php and see how they do it. They key is to bring the CI superglobal into the local helper function using $CI =& get_instance();. Then you just use $CI when referencing CI instead of using $this. Like,

Code:
function email_helper($from = '')
{
  //make sure the FROM was set
  if ($from === '')
  {
    show_error('You must include the FROM portion of the email.')
  }
  $CI =& get_instance();       // Get CI
  $CI->load->library('email'); // Load the CI email library
  $CI->email->from($from);     // Set the FROM in the email
  // ...
}
Just have your helper function accept parameters and them pass them to CI however you need. This isn't a complete example, obviously, but should help with your question. Again, look at some of the existing helpers for better ways (like you should check to see if the function exists first...etc)

Then, to use it like in a controller or view:
Code:
$this->load->helper('email');  //load the helper
email_helper('a name');        //use the helper function and send "a name" as the FROM


Messages In This Thread
When to Build your own Library ? - by El Forum - 02-10-2012, 01:12 PM
When to Build your own Library ? - by El Forum - 02-11-2012, 04:14 AM
When to Build your own Library ? - by El Forum - 02-11-2012, 05:04 AM
When to Build your own Library ? - by El Forum - 02-11-2012, 05:40 PM
When to Build your own Library ? - by El Forum - 02-11-2012, 06:41 PM



Theme © iAndrew 2016 - Forum software by © MyBB