Skip to content

Instantly share code, notes, and snippets.

@nfaiz
Last active October 9, 2025 07:01
Show Gist options
  • Select an option

  • Save nfaiz/d3ae59cc8feaaac1e73970753151823e to your computer and use it in GitHub Desktop.

Select an option

Save nfaiz/d3ae59cc8feaaac1e73970753151823e to your computer and use it in GitHub Desktop.
<!-- start create.php -->
<?= $this->extend('template/bootstrap') ?>
<?= $this->section('content') ?>
<h1>Create Customer</h1>
<?php if (session()->getFlashdata('errors')): ?>
<?php
$msg = '<ul>';
foreach (session()->getFlashdata('errors') as $e) {
$msg .= '<li>' . esc($e) . '</li>';
}
$msg .= '</ul>';
?>
<?= view_cell('BootstrapCell::AlertMessage', [
'type' => 'danger',
'message' => $msg
]) ?>
<?php endif; ?>
<form action="<?= site_url('customer/store') ?>" method="post">
<?= csrf_field() ?>
<div class="mb-3">
<label>Name</label>
<input type="text" name="name" class="form-control" value="<?= old('name') ?>">
</div>
<div class="mb-3">
<label>Email</label>
<input type="email" name="email" class="form-control" value="<?= old('email') ?>">
</div>
<div class="mb-3">
<label>Phone</label>
<input type="text" name="phone" class="form-control" value="<?= old('phone') ?>">
</div>
<div class="mb-3">
<label>Address</label>
<textarea name="address" class="form-control"><?= old('address') ?></textarea>
</div>
<div class="mb-3">
<label>State</label>
<select name="state_id" class="form-control">
<?php foreach($states as $s): ?>
<option value="<?= $s['id'] ?>" <?= old('state_id')==$s['id']?'selected':'' ?>>
<?= esc($s['name']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<button class="btn btn-success">Save</button>
<a href="<?= site_url('customer') ?>" class="btn btn-secondary">Back</a>
</form>
<?= $this->endSection() ?>
<!-- end create.php -->
<?php
namespace App\Database\Seeds;
use CodeIgniter\Database\Seeder;
use Faker\Factory;
class CustomerSeeder extends Seeder
{
public function run()
{
$faker = Factory::create('ms_MY');
$stateIds = $this->db->table('states')->select('id')->get()->getResultArray();
$ids = array_column($stateIds, 'id');
for ($i=0;$i<200;$i++) {
$this->db->table('customers')->insert([
'name'=>$faker->name,
'email'=>$faker->unique()->safeEmail,
'phone'=>$faker->phoneNumber,
'address'=>$faker->address,
'state_id'=>$ids[array_rand($ids)],
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s'),
]);
}
}
}
<!-- start deleted.php -->
<?= $this->extend('template/bootstrap') ?>
<?= $this->section('content') ?>
<h1>Deleted Customers</h1>
<p><a href="<?= site_url('customer') ?>" class="btn btn-outline-secondary btn-sm">Back to Active</a></p>
<?php if (session()->getFlashdata('success')): ?>
<?= view_cell('BootstrapCell::AlertMessage', ['type' => 'success', 'message' => session()->getFlashdata('success')]) ?>
<?php endif; ?>
<table class="table table-bordered">
<thead>
<tr><th>#</th><th>Name</th><th>Email</th><th>State</th><th>Actions</th></tr>
</thead>
<tbody>
<?php foreach($customers as $c): ?>
<tr>
<td><?= esc($c['id']) ?></td>
<td><?= esc($c['name']) ?></td>
<td><?= esc($c['email']) ?></td>
<td><?= esc($c['state_name']) ?></td>
<td>
<form action="<?= site_url('customer/restore/'.$c['id']) ?>" method="post" style="display:inline">
<?= csrf_field() ?>
<button class="btn btn-sm btn-success">Restore</button>
</form>
<form action="<?= site_url('customer/purge/'.$c['id']) ?>" method="post" style="display:inline" onsubmit="return confirm('Permanently delete?')">
<?= csrf_field() ?>
<button class="btn btn-sm btn-danger">Purge</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?= $this->endSection() ?>
<!-- end deleted.php -->
<!-- start edit.php -->
<?= $this->extend('template/bootstrap') ?>
<?= $this->section('content') ?>
<h1>Edit Customer</h1>
<?php if (isset(session()->getFlashdata('errors'))): ?>
<?php
$msg = '<ul>';
foreach (session()->getFlashdata('errors') as $e) {
$msg .= '<li>' . esc($e) . '</li>';
}
$msg .= '</ul>';
?>
<?= view_cell('BootstrapCell::AlertMessage', [
'type' => 'danger',
'message' => $msg
]) ?>
<?php endif; ?>
<form action="<?= site_url('customer/update/'.$customer['id']) ?>" method="post">
<?= csrf_field() ?>
<div class="mb-3">
<label>Name</label>
<input type="text" name="name" class="form-control" value="<?= old('name',$customer['name']) ?>">
</div>
<div class="mb-3">
<label>Email</label>
<input type="email" name="email" class="form-control" value="<?= old('email',$customer['email']) ?>">
</div>
<div class="mb-3">
<label>Phone</label>
<input type="text" name="phone" class="form-control" value="<?= old('phone',$customer['phone']) ?>">
</div>
<div class="mb-3">
<label>Address</label>
<textarea name="address" class="form-control"><?= old('address',$customer['address']) ?></textarea>
</div>
<div class="mb-3">
<label>State</label>
<select name="state_id" class="form-control">
<?php foreach($states as $s): ?>
<option value="<?= $s['id'] ?>" <?= $customer['state_id']==$s['id']?'selected':'' ?>>
<?= esc($s['name']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<button class="btn btn-primary">Update</button>
<a href="<?= site_url('customer') ?>" class="btn btn-secondary">Back</a>
</form>
<?= $this->endSection() ?>
<!-- end edit.php -->
<!-- create bootstrap pager app/Views/template/pager.php -->
<?php $pager->setSurroundCount(2) ?>
<nav aria-label="Page navigation">
<ul class="pagination">
<?php if ($pager->hasPrevious()) : ?>
<li class="page-item">
<a class="page-link" href="<?= $pager->getFirst() ?>" aria-label="<?= lang('Pager.first') ?>">
<span aria-hidden="true"><?= lang('Pager.first') ?></span>
</a>
</li>
<li class="page-item">
<a class="page-link" href="<?= $pager->getPrevious() ?>" aria-label="<?= lang('Pager.previous') ?>">
<span aria-hidden="true"><?= lang('Pager.previous') ?></span>
</a>
</li>
<?php endif ?>
<?php foreach ($pager->links() as $link): ?>
<li class="page-item" <?= $link['active'] ? 'class="active"' : '' ?>>
<a class="page-link" href="<?= $link['uri'] ?>">
<?= $link['title'] ?>
</a>
</li>
<?php endforeach ?>
<?php if ($pager->hasNext()) : ?>
<li class="page-item">
<a class="page-link" href="<?= $pager->getNext() ?>" aria-label="<?= lang('Pager.next') ?>">
<span aria-hidden="true"><?= lang('Pager.next') ?></span>
</a>
</li>
<li class="page-item">
<a class="page-link" href="<?= $pager->getLast() ?>" aria-label="<?= lang('Pager.last') ?>">
<span aria-hidden="true"><?= lang('Pager.last') ?></span>
</a>
</li>
<?php endif ?>
</ul>
</nav>
<!-- end edit app/Views/template/pager.php -->
<!-- start show.php -->
<?= $this->extend('template/bootstrap') ?>
<?= $this->section('content') ?>
<h1>Customer Details</h1>
<table class="table">
<tr><th>ID</th><td><?= esc($customer['id']) ?></td></tr>
<tr><th>Name</th><td><?= esc($customer['name']) ?></td></tr>
<tr><th>Email</th><td><?= esc($customer['email']) ?></td></tr>
<tr><th>Phone</th><td><?= esc($customer['phone']) ?></td></tr>
<tr><th>Address</th><td><?= esc($customer['address']) ?></td></tr>
<tr><th>State</th><td><?= esc($customer['state_name']) ?></td></tr>
</table>
<a href="<?= site_url('customer') ?>" class="btn btn-secondary">Back</a>
<?= $this->endSection() ?>
<!-- end show.php -->
<?php
namespace App\Database\Seeds;
use CodeIgniter\Database\Seeder;
class StateSeeder extends Seeder
{
public function run()
{
$states = [
'Johor','Kedah','Kelantan','Melaka','Negeri Sembilan','Pahang',
'Pulau Pinang','Perak','Perlis','Selangor','Terengganu','Sabah','Sarawak','Wilayah Persekutuan'
];
foreach($states as $s) {
$this->db->table('states')->insert(['name'=>$s]);
}
}
}
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class Customer extends Migration
{
public function up()
{
$this->forge->addField([
'id' => ['type'=>'INT','constraint'=>11,'unsigned'=>true,'auto_increment'=>true],
'name' => ['type'=>'VARCHAR','constraint'=>200],
'email' => ['type'=>'VARCHAR','constraint'=>150,'null'=>true],
'phone' => ['type'=>'VARCHAR','constraint'=>30,'null'=>true],
'address' => ['type'=>'TEXT','null'=>true],
'state_id' => ['type'=>'INT','constraint'=>11,'unsigned'=>true],
'created_at' => ['type'=>'DATETIME','null'=>true],
'updated_at' => ['type'=>'DATETIME','null'=>true],
'deleted_at' => ['type'=>'DATETIME','null'=>true],
]);
$this->forge->addKey('id', true);
$this->forge->addForeignKey('state_id', 'states', 'id', 'CASCADE', 'CASCADE');
$this->forge->createTable('customers');
}
public function down()
{
$this->forge->dropTable('customers');
}
}
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class State extends Migration
{
public function up()
{
$this->forge->addField([
'id' => ['type'=>'INT','constraint'=>11,'unsigned'=>true,'auto_increment'=>true],
'name' => ['type'=>'VARCHAR','constraint'=>100],
'created_at' => ['type'=>'DATETIME','null'=>true],
'updated_at' => ['type'=>'DATETIME','null'=>true],
'deleted_at' => ['type'=>'DATETIME','null'=>true],
]);
$this->forge->addKey('id', true);
$this->forge->createTable('states');
}
public function down()
{
$this->forge->dropTable('states');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment