Thursday, April 11, 2013

How to create and view data in CodeIgniter






hi guys, i will share a tutorial User login in CodeIgniter.
this tutorial is very simple to implementation... follow this tutorial and if you have problem in this tutorial, you can post comment and please correction.

let's try guys..

1. Create a table users in database


CREATE TABLE IF NOT EXISTS `users` (
  `uid` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  `name` varchar(30) NOT NULL,
  PRIMARY KEY (`uid`)
)


2. Modify file in application -> config -> autoload.php

check list code
$autoload['libraries'] = array('');

you can modify to this
$autoload['libraries'] = array('database');

3. modify file in application -> config -> routers.php

$route['default_controller'] = "welcome";

to
$route['default_controller'] = "users";

4. Create file in application -> controllers -> users.php
and type a function same with below :


function __construct() {
    parent::__construct();
}



public function index()
{
    $this->load->view('template');
}

5. Create file in application -> views -> template.php
<html>
<head>
<title>Create a Users</title>
</head>
<body>
<form action="<?php echo base_url(); ?>/index.php/users/" action="post">
username :<input type="text" name="users"><br/>
password :<input type="password" name="pass"><br/>
Name :<input type="text" name="name"><br/>
<input type="submit" value="Create"><br/>
</form>

</body>
</html>

6. add a code in function construct and index in application -> controller -> users.php

function __construct() {
    parent::__construct();
    $this->load->model('users_model');
}



public function index()
{
    $param = array(
       'username' => $this->input->post('users'),
       'password' => $this->input->post('pass'),
       'name' => $this->input->post('name'),
    );
    $this->users_model->create($param);
    $this->load->view('template');
}



7. next, create a model in application -> models -> users_model.php

function create($param=array()){
    if(!empty($param)){
        $data = array(
          'usersname' =>  $this->input->post('users'),
          'password'=>  $this->input->post('pass'),
          'name' =>  $this->input->post('name'),
         );
            $this->db->insert('users', $data);
        } else {
            $this->db->insert('users', $param);
    }
}

step 1 - 7 is a process create a data user. 

8. now, we will show data users in database.
in add list code in application -> controller -> users.php

public function index()
{
    $param = array(
       'username' => $this->input->post('users'),
       'password' => $this->input->post('pass'),
       'name' => $this->input->post('name'),
    );
    $this->users_model->create($param);

    //new code
    $data['data_users'] = $this->users_model->findAll();
    $this->load->view('template',$data);
}

9. and you add a function findAll in application -> models -> users_model.php

function findAll() {
    $this->db->select('*');
    $this->db->order_by('users','ASC');
    $query = $this->db->get('users');

    if ($query->num_rows() > 0) {
        return $query->result_array();
    }
}

10. and the you must add a list code in application -> views -> template.php

<html>
<head>
<title>Create a Users</title>
</head>
<body>
<form action="<?php echo base_url(); ?>/index.php/users/" action="post">
username :<input type="text" name="users"><br/>
password :<input type="password" name="pass"><br/>
Name :<input type="text" name="name"><br/>
<input type="submit" value="Create"><br/>
</form>

//new code
<table>
<tr>
  <td>Username</td>
  <td>Name</td>
</tr>

<?php if(isset($data_users) && count($data_users)){?>
<?php foreach ($data_users as $dtusers): ?>
<tr>
  <td><?php echo $dtusers['users']; ?></td>
  <td><?php echo $dtusers['name']; ?></td>
</tr>
<?php
  endforeach;
else{
  echo "data not found";
}?>
</table>
</body>
</html>

finish.. you can try, if you have problem in this tutorial. you can comment in this post.
and i'm sorry if my english is very bad..








No comments:

Post a Comment