I have a problem with how to search for data according to keywords
this is my controller
protected $subCategori;
protected $categori;
public function __construct()
{
$this->subCategori = new SubCategoriModel();
$this->categori = new CategoriModel();
}
public function index()
{
$currentPage = $this->request->getVar('page_page') ? $this->request->getVar('page_page') : 1;
$keyword = $this->request->getVar('keyword');
if ($keyword) {
$this->subCategori->search($keyword);
} else {
$this->subCategori;
}
$data = [
'menu' => 'subCategori',
'subCategori' => $this->subCategori->getAll(),
'page' => $this->subCategori->paginate(4, 'page'),
'pager' => $this->subCategori->pager,
'currentPage' => $currentPage
];
if (session()->get('logged_in') != TRUE) {
return redirect()->to('/admin/login');
} else {
// $data = ['categori' => $this->categori->findAll()];
return view('subCategori/index', $data);
}
}
this is my model
protected $table = 'sub_categori';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'object';
protected $allowedFields = ['id_categori', 'nama_sub_categori', 'created_at', 'updated_at'];
protected $useTimestamps = true;
function getAll()
{
$builder = $this->db->table('sub_categori');
$builder->join('categori', 'categori.id = sub_categori.id_categori ');
$query = $builder->get();
return $query->getResult();
}
public function search($keyword)
{
$builder = $this->builder();
$builder->join('categori', 'categori.id = sub_categori.id_categori ');
$builder->like('nama_sub_categori', $keyword);
return $builder;
}
this is my View
<?= $this->extend('admin/template') ?>
<?= $this->section('content') ?>
<main class="col-md-9 ml-sm-auto col-lg-10 px-md-4 py-4">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="/admin/dashboard">Home</a></li>
<li class="breadcrumb-item active" aria-current="page">Sub Categori</li>
</ol>
</nav>
<a href="<?= base_url('admin/addCategori') ?>"><button type="button" class="btn btn-dark mt-3 mb-2">Add Data</button></a>
<div class="row">
<div class="col-5">
<form action="" method="post">
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Masukan Data kategori" name="keyword">
<button class="btn btn-outline-secondary" type="submit" name="submit">Cari Data</button>
</form>
</div>
</div>
</div>
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Categori</th>
<th>Sub Categori</th>
<th>edit</th>
<th>delete</th>
</tr>
</thead>
<tbody>
<?php $no = 1 + (4 * ($currentPage - 1)); ?>
<?php foreach ($subCategori as $r) : ?>
<tr>
<td><?php echo $no++ ?></td>
<td><?php echo $r->nama ?></td>
<td><?php echo $r->nama_sub_categori ?></td>
<td><a href=""><button type="button" class="btn btn-warning">Edit</button></a></td>
<td>
<form action="" method="post">
<?= csrf_field() ?>
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn btn-danger" onclick="return confirm('Apakah anda Yakin')">Hapus</button>
</form>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<?= $pager->links('page', 'admin_pagination') ?>
<footer class="pt-5 d-flex justify-content-between">
<span>Copyright © 2019-2020 <a href="https://themesberg.com">Themesberg</a></span>
<ul class="nav m-0">
<li class="nav-item">
<a class="nav-link text-secondary" aria-current="page" href="#">Privacy Policy</a>
</li>
<li class="nav-item">
<a class="nav-link text-secondary" href="#">Terms and conditions</a>
</li>
<li class="nav-item">
<a class="nav-link text-secondary" href="#">Contact</a>
</li>
</ul>
</footer>
</main>
<?= $this->endSection() ?>
what I expected when I searched for data according to the data I was looking for.
example;I looked for clothes data but all the data came up.
and also when searching for data that does not exist
New contributor
triahmad maulana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.