Monday, October 10, 2016

Codeigniter Simple CRUD [part 2] 1 of 4

To continue this discussion, please follow the advance previously.
  1. Integration Between CodeIgniter With Bootstrap Framework [part 1]
STEP 1
Open file autoload.php in /application/config/autoload.php
$autoload['libraries'] = array(
    'template',
    'form_validation',
    'session',
    'database'
);

$autoload['helper'] = array(
    'style',
    'url'
);
STEP 2
Open file menu.php in /application/views/content/menu.php
find these scripts
<div id="navbar" class="navbar-collapse collapse">
And then type like this more or less
<div id="navbar" class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">CRUD <span class="caret"></span></a>
            <ul class="dropdown-menu">
                <li>
                    <?php echo anchor('welcome/crud/simple', 'Simple');?>
                </li>
            </ul>
        </li>
    </ul>
    <form class="navbar-form navbar-right">
        <div class="form-group">
            <input type="text" placeholder="Email" class="form-control">
        </div>
        <div class="form-group">
            <input type="password" placeholder="Password" class="form-control">
        </div>
        <button type="submit" class="btn btn-success">Sign in</button>
    </form>
</div>
STEP 3
Open file welcome.php in /application/controllers/Welcome.php
Add this function
public function crud($params = null) {
    if ($params == 'simple') {
        $this->_crud_simple();
    }
}

private function _crud_simple() {
    $validation = array(
        array('field' => 'first_name', 'rules' => 'required'),
        array('field' => 'last_name', 'rules' => 'required'),
        array('field' => 'age', 'rules' => 'required|numeric', 'label' => 'only between 1-9'),
        array('field' => 'street', 'rules' => 'required')
    );
    $this->form_validation->set_rules($validation);
    if ($this->form_validation->run() == true) {
        $this->submit->insert_simple_crud();
    }
            
    $this->data = array(
        'first_name' => set_value('first_name'),
        'last_name' => set_value('last_name'),
        'age' => set_value('age'),
        'street' => set_value('street')
    );
            
    $this->template->set('title', 'Simple CRUD');
    $this->template->load('main_template', 'content/simple_crud', $this->data);
}
And then find these scripts
public function __construct() {
And then type like this
public function __construct() {
    parent::__construct();
        
    $this->load->model('submit_model', 'submit', true);
}
STEP 4
Create new file with the name Submit_model.php and save in /application/models/Submit_model.php
<?php
class Submit_model extends CI_Model {

    function insert_simple_crud() {
        $this->db->insert('tb_bio', array(
            'first_name' => $this->input->post('first_name', true),
            'last_name' => $this->input->post('last_name', true),
            'age' => $this->input->post('age', true),
            'street' => $this->input->post('street', true)
        ));
        $this->session->set_flashdata('msg', '
            <p class="bg-primary" style="padding:10px">Success!</p>
        ');
        redirect('welcome/crud/simple');
    }

}
STEP 5
Create a database with names like the following example : appmurah_blog and this table script
CREATE TABLE `tb_bio` (
  `bio_ID` int(11) NOT NULL,
  `first_name` varchar(20) NOT NULL,
  `last_name` varchar(20) NOT NULL,
  `age` varchar(3) NOT NULL,
  `street` varchar(40) NOT NULL
);
STEP 6
Create new file with the name simple_crud.php save in /application/views/content/simple_crud.php
<div class="jumbotron">
    <div class="container">
        <h1>Simple CRUD!</h1>
    </div>
</div>
<div class="container">
    <div class="row">
    
        <?php echo form_open();?>
    
        <div class="col-md-12">
        
            <?php echo $this->session->flashdata('msg');?>
        
            <div class="form-group">
                <?php
                echo form_label('First Name', 'first_name');
                echo form_input(array(
                    'name' => 'first_name',
                    'id' => 'first_name',
                    'class' => 'form-control',
                    'value' => $first_name
                ));
                ?>
            </div>
            <div class="form-group">
                <?php
                echo form_label('Last Name', 'last_name');
                echo form_input(array(
                    'name' => 'last_name',
                    'id' => 'last_name',
                    'class' => 'form-control',
                    'value' => $last_name
                ));
                ?>
            </div>
            <div class="form-group">
                <?php
                echo form_label('Age', 'age');
                echo form_input(array(
                    'name' => 'age',
                    'id' => 'age',
                    'class' => 'form-control',
                    'value' => $age
                ));
                echo form_error('age');
                ?>
            </div>
            <div class="form-group">
                <?php
                echo form_label('Street', 'street');
                echo form_input(array(
                    'name' => 'street',
                    'id' => 'street',
                    'class' => 'form-control',
                    'value' => $street
                ));
                ?>
            </div>
            <div class="form-group">
                <?php
                echo form_submit(array(
                    'value' => 'Submit',
                    'class' => 'btn btn-primary'
                ));
                ?>            
            </div>
        </div>
        
        <?php echo form_close();?>
        
    </div>
</div>
Download this project

0 comments:

Post a Comment