Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Saturday, June 22, 2013

Simple Validation rule in CodeIgniter




now, i can share about validation rule in CodeIgniter. if you using validation default in CodeIgniter, you must follow rule module Library, Helper, etc in CodeIgniter.

if you not expert using JavaScript, ajax, or jQuery for validation in CodeIgniter you can try using validation in CodeIgniter this is rule validation in CodeIgniter :

Rule Reference

The following is a list of all the native rules that are available to use:
Rule Parameter Description Example
required No Returns FALSE if the form element is empty.
matches Yes Returns FALSE if the form element does not match the one in the parameter. matches[form_item]
is_unique Yes Returns FALSE if the form element is not unique to the table and field name in the parameter. is_unique[table.field]
min_length Yes Returns FALSE if the form element is shorter then the parameter value. min_length[6]
max_length Yes Returns FALSE if the form element is longer then the parameter value. max_length[12]
exact_length Yes Returns FALSE if the form element is not exactly the parameter value. exact_length[8]
greater_than Yes Returns FALSE if the form element is less than the parameter value or not numeric. greater_than[8]
less_than Yes Returns FALSE if the form element is greater than the parameter value or not numeric. less_than[8]
alpha No Returns FALSE if the form element contains anything other than alphabetical characters.
alpha_numeric No Returns FALSE if the form element contains anything other than alpha-numeric characters.
alpha_dash No Returns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes.
numeric No Returns FALSE if the form element contains anything other than numeric characters.
integer No Returns FALSE if the form element contains anything other than an integer.
decimal Yes Returns FALSE if the form element is not exactly the parameter value.
is_natural No Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc.
is_natural_no_zero No Returns FALSE if the form element contains anything other than a natural number, but not zero: 1, 2, 3, etc.
valid_email No Returns FALSE if the form element does not contain a valid email address.
valid_emails No Returns FALSE if any value provided in a comma separated list is not a valid email.
valid_ip No Returns FALSE if the supplied IP is not valid. Accepts an optional parameter of "IPv4" or "IPv6" to specify an IP format.
valid_base64 No Returns FALSE if the supplied string contains anything other than valid Base64 characters.

 
if you confused for using rule, you can read manual book or you can change and custom rule validation in class module.
for modify rule validation in CodeIgniter, you can open file in system/library/form_validation.php

for example in rule alpha..

public function alpha($str)
{
        return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
}

and using rule :

$this->form_validation->set_rules('name','Name Message','alpha');

alpha only make validation for if you input in form using space rule alpha make not valid you input form. because space not alphabetical characters. if you want using rule alpha and make space is valid in alpha rule, you just modify function alpha :

public function alpha($str)
{
        return ( ! preg_match("/^([a-z ])+$/i", $str)) ? FALSE : TRUE;
}

i just add space in [a-z] to [a-z ] very simple.
not modify? just make a new function in class form_validation.

finish, and sorry if my english not good.


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..