title: Can I Use a Single CodeIgniter Model for Multiple Tables?
body: <p>I have a database for a sports event that has multiple tables. Some of the tables hold the records for the Results, there is a User Profiles table, as well as a few Miscellaneous tables.</p>
  
  <p>My Model is as follows:</p>
  
  <pre><code>class Results extends CI_Model
  {
      public function __construct()
      {
          parent::__construct();
      }
  
      public function displayrecords($table) {
          $query=$this-&gt;db-&gt;query("select * from " . $table);
          return $query-&gt;result();
      }
  
      public function getdisplayname($table) {
          $query=$this-&gt;db-&gt;query("select * from " . $table);
          return $query-&gt;result();
      }
  
      public function updatetable() {
  
      }
  
      public function deleterecords($table, $id) {
          $this-&gt;db-&gt;query("delete * from " . $table . " where id='" . $id . "'");
      }
  }
  </code></pre>
  
  <p>And the Controller for the Index View:</p>
  
  <pre><code>public function index() {
      // Load the models
      $this-&gt;load-&gt;model('Results');
      $this-&gt;load-&gt;model('Info');
      $this-&gt;load-&gt;model('TableNames');
  
      // Create the array to store the data for the index page
      $data['results'] = array();
  
      // Get a list of all the tables in the event database
      $tables = $this-&gt;db-&gt;list_tables();
      $data['tables'] = array();
  
      // Loop through the list of table names and extract the results tables
      foreach($tables as $table) {
          if (strpos($table, "rs_") === 0) {
              array_push($data['tables'], $table);
              $temp = $this-&gt;Results-&gt;displayrecords($table);
              array_push($data['results'], $temp);
              $data['headnames'] = $this-&gt;db-&gt;list_fields($table);
          }
      }
  
      // Get the event info data and the list of Human Readable Table Names
      $data['tablenames'] = $this-&gt;TableNames-&gt;getnames();
      $data['eventinfo'] = $this-&gt;Info-&gt;getinfo();
  
      // Render the templates
      $this-&gt;parser-&gt;parse('templates/index_header', $data);
      $this-&gt;parser-&gt;parse('pages/index', $data);
      $this-&gt;parser-&gt;parse('templates/footer', $data);
  }
  </code></pre>
  
  <p>What I am trying to figure out is if it is advisable to use a single CodeIgniter Model, with all of the Results tables. An example of the Results tables: Top Ten Male 30 to 39 Years for the 21KM event and Top Ten Males 40 to 49 years also for the 21KM event. I currently have all of the Results tables using the same Models methods such as:</p>
  
  <pre><code>public function displayrecords($table) {
      $query=$this-&gt;db-&gt;query("select * from " . $table);
      return $query-&gt;result();
  }
  </code></pre>
  
  <p>which takes a result table as the parameter, and displays the Results. I would create Models for each of the Results tables, but I do not know before hand what name the table will have, columns it will have etc. Each Results table is created through a webpage by an Administrator, and added to the database as needed. Each table will have the same operations performed on them, delete rows, update row. That is why i thought it would be best to have one Model that can work with all the tables.</p>
  
  <p>I am not sure if the way I am going about this is correct though as I seem to already be having a few problems. When trying to delete a record from a table for example, I can get the id of the row that would need to be deleted, but I am not sure how to get the database table name as the deleterecords method would need 2 parameters, $id, and $tablename to delete the record correctly. The model method is as such:</p>
  
  <pre><code>public function deleterecords($tablename, $id) {
      $this-&gt;db-&gt;query("delete from " . $tablename . " where id='" . $id . "'");
  }
  </code></pre>
  
  <p>and then in the view I am displaying each results table:</p>
  
  <pre><code>foreach ($table as $row) {
      echo "&lt;tr id='result_" . $row-&gt;id . "'&gt;";
      echo "&lt;td&gt;" . $row-&gt;Place . "&lt;/td&gt;";
      echo "&lt;td class='bibcell'&gt;" . $row-&gt;BibNumber . "&lt;a class='editbut' href=''&gt;&lt;img class='bibnum' src='" . base_url() . "assets/images/editicon.png' width='80' style='padding-left: 20px;'&gt;&lt;/a&gt;&lt;a class='delbut' href='deletedata?id=" . $row-&gt;id . "'&gt;&lt;img src='" . base_url() . "assets/images/deleteicon.png' width='75' style='padding-left: 5px;'&gt;&lt;/a&gt;&lt;/td&gt;";
      echo "&lt;td&gt;" . $row-&gt;Name . "&lt;/td&gt;";
      echo "&lt;td&gt;" . $row-&gt;Age . "&lt;/td&gt;";
      echo "&lt;td&gt;" . $row-&gt;Club . "&lt;/td&gt;";
      echo "&lt;td&gt;" . $row-&gt;Time . "&lt;/td&gt;";
      echo "&lt;td&gt;&lt;select class='user-options'&gt;&lt;option value='pending' style='background: red;'&gt;Pending&lt;/option&gt;&lt;option value='checked' style='background: green;'&gt;Checked&lt;/option&gt;&lt;/select&gt;&lt;/td&gt;";
      echo "&lt;/tr&gt;";
  }
  </code></pre>
  
  <p>Would using a hidden input be the way to get the table name is there possibly features within CodeIgniter that would handle using multiple tables with a single Model?</p>
  
  <p>EDIT:</p>
  
  <p>I have managed to figure out this:</p>
  
  <pre><code>echo "&lt;td class='bibcell'&gt;" . $row-&gt;BibNumber . "&lt;a class='editbut' href=''&gt;&lt;img class='bibnum' src='" . base_url() . "assets/images/editicon.png' width='80' style='padding-left: 20px;'&gt;&lt;/a&gt;&lt;a class='delbut' href='deletedata?id=" . $row-&gt;id . "&amp;tablename=" . $tables[$i] . "'&gt;&lt;img src='" . base_url() . "assets/images/deleteicon.png' width='75' style='padding-left: 5px;'&gt;&lt;/a&gt;&lt;/td&gt;";
  </code></pre>
  
  <p>to pass the id and tablename to the Model method to be deleted, but I am getting undefined variable tablename. I can see the tablename variable is in the url though.</p>
  
  <pre><code>http://localhost/results/pages/deletedata?id=1&amp;tablename=rs_toptenfemale
  </code></pre>