<?php namespace App\Models;
use CodeIgniter\Model;
class ContactModel extends Model
{
protected $table = 'users';
protected $primaryKey = 'id';
protected $allowedFields = ['name', 'age', 'email'];
protected $useTimestamps = false;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
}
<?php namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\ContactModel;
class Contact extends BaseController
{
public function __construct(){
}
public function index()
{
$data['title'] = "Contact List";
$model = new ContactModel();
$data['posts'] = $model->findAll();
return view('view',$data);
}
public function view_edit($id = null)
{
$model = new ContactModel();
$data['user'] = $model->where('id', $id)->first();
return view('update', $data);
}
public function update($id = null){
$data = $this->request->getVar();
$id = $this->request->getVar('id');
$validation = \Config\Services::validation();
$validation->setRules([
'name' => 'required|string',
'age' => 'required|integer|greater_than[0]',
'email' => 'required|valid_email'
]);
$res = $validation->withRequest($this->request)
->run();
if(!$res){
$data['title'] = "Contact";
echo view('contact',$data, [
'validation' => $validation
]);
}
else{
//$contactModel = new \App\Models\ContactModel();
$session = \Config\Services::session();
try{
$model = new ContactModel();
$user=$model->update($id,$data);
$session->setFlashdata('msg', 'Record Updated successfully');
}
catch(\Exception $e){
$session->setFlashdata('msg', 'Something went wrong');
}
$data['title'] = "Contact";
return redirect()->to( base_url('contact/index') );
}
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Codeigniter 4 CRUD Tutorial - posts List Example - Expertphp.in</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css">
</head>
<body>
<div class="container mt-4">
<div class="register-box col-md-4">
<?= \Config\Services::validation()->listErrors(); ?>
<?= \Config\Services::session()->getFlashdata('msg'); ?>
<form method="post" action="<?= base_url() ?>/Contact/save">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" name="name" class="form-control" placeholder="Enter Name" id="name">
</div>
<div class="form-group">
<label for="age">Age:</label>
<input type="number" name="age" class="form-control" placeholder="Enter Age" id="age">
</div>
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" name="email" class="form-control" placeholder="Enter email" id="email">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Codeigniter 4 CRUD Tutorial - posts List Example - Expertphp.in</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css">
</head>
<body>
<div class="container mt-6">
<a href="<?php echo base_url('contact/create') ?>" class="btn btn-sm btn-success">Create</a>
<div class="row mt-3">
<table class="table table-bordered" id="posts">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php if($posts): ?>
<?php foreach($posts as $post): ?>
<tr>
<td><?php echo $post['id']; ?></td>
<td><?php echo $post['name']; ?></td>
<td><?php echo $post['age']; ?></td>
<td><?php echo $post['email']; ?></td>
<td>
<a href="<?php echo base_url(); ?>/contact/delete/<?php echo $post['id']; ?>">Delete</a></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js" type="text/javascript"></script>
<script>
$(document).ready( function () {
$('#posts').DataTable();
} );
</script>
</body>
</html>
update.php (View)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Codeigniter 4 CRUD Tutorial - posts List Example - Expertphp.in</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css">
</head>
<body>
<div class="container mt-4">
<div class="register-box col-md-4">
<?= \Config\Services::validation()->listErrors(); ?>
<?= \Config\Services::session()->getFlashdata('msg'); ?>
<form method="post" action="<?= base_url() ?>/Contact/update">
<input type="hidden" name="id" class="form-control" id="id" value="<?php echo $user['id'];?>">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" name="name" class="form-control" placeholder="Enter Name" id="name" value="<?php echo $user['name']; ?>">
</div>
<div class="form-group">
<label for="age">Age:</label>
<input type="number" name="age" class="form-control" placeholder="Enter Age" id="age" value="<?php echo $user['age']; ?>">
</div>
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" name="email" class="form-control" placeholder="Enter email" id="email" value="<?php echo $user['email']; ?>">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</body>
</html>