How do I use a Codeigniter method within my own function?

Colin Hu

I use Codeigniter as my PHP framework, and in one of my controllers, I want to include a Codeingiter method within a function I created myself. The CodeIgniter method that I want to include in my function is "$this->db->escape($var);", which is an escaping query for Codeigniter. My PHP code is the following:

foreach ($array as $item)
{
  $name = $item["name"];
  $name = processvar($name);
}

function processvar($var) {
  $var = $this->db->escape($var);
  return $var;
}

However, the code above produces the following error message:

Fatal error: Using $this when not in object context in [folder path]/index.php on line #

Please advise how I can include the "$this->db->escape()" method in my own custom function.

Michael D.
function processvar($var) {
    $CI =& get_instance();
    $var = $CI->db->escape($var);

    return $var;
}

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 load my own library dynamically, and invoke a method in it?

How do I get the name of a function or method from within a Python function or method?

How do I use OAuth within my GWT application?

How do I define my own SelectableChannel?

How do I pass function references into my method and use them?

How do I call a method on my ServiceWorker from within my page?

How do i use my own images in TensorFlow?

How do i properly use the replace function within my code?

How do I use my own modules in a JuliaBox notebook?

How do I load my own function with 'tabBarOnPress'?

How do you use the bind() method without "this" within the function definition?

How do I access a property within an array for my function?

How do I use my own folder structure with Shotwell?

AngularJS - How can I use .config() method with my own services?

How to use android function in my own class?

How do I use "<<" with my own struct?

How do I call a function within ajax using codeigniter to update

How do I add "args=(...)" into arguments of my own function?

How do I call my function from within my function?

How do I use timeseries index within lambda function

How can I display my buttons on their own line within a flexbox

How do I use a checkbox within my WordPress website?

How Do I Use Map Within a Function - Apps Script

How do I use style visibility in an if condition within a function?

How do i extend Django's auth user manager to add a method of my own?

How to use my own javascript function in QML?

How do I use the input from my box to my function?

What do the third and fourth arguments of the mat-palette function do (not the first/second) and how do I use those for my own components

How do I add missing 'IChainable' properties to my own construct so that I can use it within a state machine definition?