Skip to content

Commit 440dfb4

Browse files
committed
fitur pencarian untuk halaman public
1 parent dc2064b commit 440dfb4

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

application/controllers/Search.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
defined('BASEPATH') or exit('No direct script access allowed');
3+
4+
class Search extends CI_Controller
5+
{
6+
public function index()
7+
{
8+
$data['keyword'] = $this->input->get('keyword');
9+
$this->load->model('article_model');
10+
11+
$data['search_result'] = $this->article_model->search($data['keyword']);
12+
13+
$this->load->view('search.php', $data);
14+
}
15+
}

application/models/Article_model.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ public function find($id)
6868
return $query->row();
6969
}
7070

71+
public function search($keyword)
72+
{
73+
if(!$keyword){
74+
return null;
75+
}
76+
$this->db->like('title', $keyword);
77+
$this->db->or_like('content', $keyword);
78+
$query = $this->db->get($this->_table);
79+
return $query->result();
80+
}
81+
7182
public function insert($article)
7283
{
7384
return $this->db->insert($this->_table, $article);

application/views/_partials/navbar.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<nav class="navbar">
22
<a href="<?= site_url() ?>">Home</a>
33
<a href="<?= site_url('article') ?>">Article</a>
4+
<a href="<?= site_url('search') ?>">Cari</a>
45
<a href="<?= site_url('page/about') ?>">About</a>
56
<a href="<?= site_url('page/contact') ?>">Contact</a>
67
<a href="<?= site_url('auth/login') ?>" style="margin-left:auto">Login</a>

application/views/search.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<?php $this->load->view('_partials/head.php'); ?>
6+
</head>
7+
8+
<body>
9+
<?php $this->load->view('_partials/navbar.php'); ?>
10+
11+
<div class="container">
12+
<h1>Cari Artikel</h1>
13+
<p>Tuliskan kata kunci artikel yang ingin kamu cari</p>
14+
<form action="" method="get" style="flex-direction: row; align-items:center">
15+
<div>
16+
<input type="search" name="keyword" style="width: 360px;" placeholder="Keyword.." value="<?= html_escape($keyword) ?>" required maxlength="32" />
17+
</div>
18+
19+
<div>
20+
<input type="submit" class="button button-primary" value="Cari">
21+
</div>
22+
</form>
23+
24+
<?php if ($search_result) : ?>
25+
<div class="search-result">
26+
<hr>
27+
<?php foreach ($search_result as $article) : ?>
28+
<h2>
29+
<a href="<?= site_url('article/' . $article->slug) ?>"><?= html_escape($article->title) ?></a>
30+
</h2>
31+
<p><?= strip_tags(substr($article->content, 0, 200)) ?></p>
32+
<?php endforeach ?>
33+
</div>
34+
<?php else : ?>
35+
<?php if ($keyword) : ?>
36+
<div style="height: 400px;">
37+
<h1>Tidak ada yang ditemukan</h1>
38+
<p>Coba dengan kata kunci yang lain</p>
39+
</div>
40+
<?php endif ?>
41+
<?php endif ?>
42+
</div>
43+
<?php $this->load->view('_partials/footer.php'); ?>
44+
</body>
45+
46+
</html>

0 commit comments

Comments
 (0)