<?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);
}
}
<!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>
</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>
</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>