File tree Expand file tree Collapse file tree 4 files changed +73
-0
lines changed Expand file tree Collapse file tree 4 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -68,6 +68,17 @@ public function find($id)
6868return $ 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+
7182public function insert ($ article )
7283{
7384return $ this ->db ->insert ($ this ->_table , $ article );
Original file line number Diff line number Diff line change 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>
Original file line number Diff line number Diff line change 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>
You can’t perform that action at this time.
0 commit comments