hldueq
Last Updated: February 25, 2016
·
9.074K
· gwinn

Generate html form with CodeIgniter

Sometimes marking up forms in Codeigniter it's really boring. But there is a simple solution for this.

First of all take a look at your MySQL table structure. It can have different fields such as 'varchar', 'int', 'text' and many others.

Let's try to convert them into html form selectors.

Getting table structure

Let our table has the following structure

id: int
title: varchar
description: text
started_at: datetime

And we want to do the following conversion

(int) id => <input type="hidden" name="id"/>
(varchar) title => <input type="text" name="title"/>
(text) description => <textarea name="description"></textarea>
(datetime) started_at => <input type="text" name="started_at" class="datepicker"/> (jquery datepicker)

Let's do it

Take a look at CodeIgniter user guide. You can find an amazing tool such as field_data()

Model

class EventModel extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getFields()
    {
        return $this->db->field_data('event');
    }
}

Controller

class Event extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('EventModel', 'event');
        $this->data = $this->session->userdata('user_data');
    }

    public function edit()
    {
        $data['id'] = $this->data['event_id']; #hidden field `id`
        $data['fields'] = array_slice($this->event->getFields(), 1);
        $this->layout->view('layout/event', 'edit', $data);
    }
}

Yes, CodeIgniter can use layouts: github link

View

<?php $hidden = array('id' => $id) ?>
<?= form_open('event/save', array('class' => 'form'), $hidden);?>
    <legend>Edit form</legend>
    <?php foreach ($fields as $field): ?>
    <div>
        <?php
            $data = array('name' => $field->name);
            switch ($field->type) {
                case 'varchar':
                    echo form_input($data);
                    break;
                case 'datetime':
                    $data['class'] = 'datepicker';
                    echo form_input($data);
                    break;
                case 'text':
                    $data['rows'] = 5;
                    $data['cols'] = 10;
                    echo form_textarea($data);
                    break;
            }
        ?>
    </div>
    <?php endforeach; ?>
    <div>
        <input type="submit" value="Save">
    </div>
</form>

Of course the number of selectors and the number of data types much more but this example illustrates how you can use CodeIgniter. You can create your own libraries and helpers to make your code more readable.

For example: View

<?php $hidden = array('id' => $id) ?>
<?= form_open('event/save', array('class' => 'form'), $hidden);?>
    <legend>Edit form</legend>
    <?= buildFieldsFor($fields); ?>
    <div>
        <input type="submit" value="Save">
    </div>
</form>