phpcodeigniteractiverecorddbforge

Codeigniter clone table with active record or dbforge


Is there a way to implement:

CREATE TABLE sample LIKE master;    

using either Codeigniter active record or dbforge?

The reason being is that this is being executed from an administration panel; the admin is given a form and the new table name is captured from a POST.


Solution

  • You can try this

    $tablename = $_POST['tblname'];
    $mastertable = $_POST['mastertable']
    $this->db->query("CREATE TABLE $tablename LIKE $mastertable");
    

    To copy data

    $query = $this->db->get($mastertable);
    foreach ($query->result() as $row) {
          $this->db->insert($tablename,$row);
    }