title: Help with creating a reusable PHP function which will 'dynamically' call other functions
body: <p>This is my first time I am trying to make something seriously with relational db in MySQL and some kind of CMS created in PHP with CodeIgniter.<br>
  I came to the part where I have to insert some data in few many-to-many related tables.<br>
  In my code everything works fine (well, with just few minutes of testing), but I need some help with creating reusable function which will help me a lot to make all relations in my tables...</p>
  
  <p>I tried to comment everything in my code, so all explanations are in there...</p>
  
  <pre><code>&lt;?php
  function add(){
  
   // This is what user posted in form.
   // There are two input fields:
   // name is always only one record
   // country can be a single record or array separated with " | " characters
   // I use CodeIgniter's $this-&gt;input-&gt;post instead of $_POST[]
   $name = $this-&gt;input-&gt;post('name');
   $countries = $this-&gt;input-&gt;post('country');
  
   // Inserting data to first table
   $data = array('firstName' =&gt; htmlentities($name)); // preparing array for inserting
   $insert_name = $this-&gt;db-&gt;insert('names', $data); // inserting with CodeIgniter's help
   $last_inserted_ID = $this-&gt;db-&gt;insert_id(); // getting last inserted ID
  
   // Inserting data to second table
  
   // Formatting of posted string of countries
   // Users can post strings similar to this:
   // "Austria"
   // "Austria |"
   // "Austria | "
   // "Austria | Australia"
   // "Austria | Australia |"
   // "Austria | Australia | "
   // and similar variations
   // What I need here is clear array with country names
   $separator = strpos($countries,"|"); // check for "|" character
   if ($separator === FALSE){ // if there is no "|" character in string
    $countries_array[] = $countries; // array is only one value (only one country)
   } else {
    $countries_array = explode(" | ", $countries); // explode my array
    if (end($countries_array) == ""){ // if last item in array is ""
     array_pop($countries_array); // eliminate last (empty) item
    }
   }
  
   // Now, this is the part I think I will use lots of times.
   // I would like to make this a separate function so I could use it in many places :)
   // I would pass to that function few values and I would use one of them
   // to call different functions in this same class.
   // I guess I should pass data ($countries_array) and function names I wish to call?????? This is problematic part for my brain :))
   // Check the comments below...
   for ($i = 0; $i &lt; sizeof($countries_array); $i++){
    $insertIDS = array(); // this will be an array of IDs of all countries
    $tempdata = $this-&gt;get_countries($countries_array[$i]); // query which looks if there is a country with specific name
                  // Right here, instead of calling $this-&gt;get_countries
                  // I would like to call different functions, for example
                  // $this-&gt;get_links($links_array[$i])
                  // or $this-&gt;get_categories($categories_array[$i])
                  // etc.
    if(sizeof($tempdata) != 0){ // so, if a record already exists
     foreach ($tempdata as $k =&gt; $v){
      $insertIDS[] = $k; // insert those IDs in our array
     }
    } else { // and if a record does not exist in db
     $this-&gt;add_country($countries_array[$i]); // add it as a new record...
                 // This is also one of the places where I would call different functions
                 // for example $this-&gt;add_link($links_array[$i])
                 // or $this-&gt;add_categories($categories_array[$i])
                 // etc.
     $insertIDS[] = $this-&gt;db-&gt;insert_id(); // ...get its ID and add it to array
    }
  
    // Finally, insert all IDs into junction table!
    foreach ($insertIDS as $idKey =&gt; $idValue){
     $this-&gt;add_names_countries($last_inserted_ID, $idValue); // Another place for calling different functions
                    // example $this-&gt;add_names_links($last_inserted_ID, $idValue)
                    // etc.
    }
   }
  
  }
  ?&gt;
  </code></pre>
  
  <p>Well, looking at this code now, I see that I could put that formatting part also in that function, but that's not so much important right now...</p>
  
  <p>Thank you very very much for any help with this!!</p>