Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter Tutorial News section
#8

(07-28-2015, 08:15 AM)christaliise Wrote: I like to learn how to create the table automatically using PHP and in accordance with the Tutorial. Undoubtedly when the table is created the Tutorial will explain the query process.

The database forge library allows you to modify/create/drop tables.
http://www.codeigniter.com/user_guide/da...forge.html

It also helps to know the database library's metadata methods for things like finding out if a table exists before you create it:
http://www.codeigniter.com/user_guide/da...adata.html

So, for example, you might do something like this:
PHP Code:
if ( ! $this->db->table_exists('table_name')) {
    $fields = array(
        'id' => array(
            'type' => 'int',
            'constraint' => 11,
            'auto_increment' => true,
        ),
        'title' => array(
             'type' => 'varchar',
             'constraint' => 255,
             'null' => false,
        ),
    );
    
    $this
->load->dbforge();
    $this->dbforge->add_field($fields);
    $this->dbforge->add_key('id'true);
    $this->dbforge->create_table('table_name');
}

// Now your table should be ready 

Note that you would normally do something like this in a migration, so you can add/remove the table as needed using the migration library.
Reply


Messages In This Thread
RE: CodeIgniter Tutorial News section - by mwhitney - 07-29-2015, 12:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB