# models/Prac_model.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Prac_model extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper(array('url', 'date', 'form'));
}
function getSearchfaqs($perPage, $start_index='', $search_term=null, $is_count=0) {
// if($perPage != '' && $start_index != '') {
// $this -> db -> limit($perPage, $start_index);
// }
$this -> db -> limit($perPage, $start_index);
if ($search_term != NULL) {
$this->db->like('title', $search_term, 'both');
$this->db->or_like('body', $search_term, 'both');
$this->db->or_like('answer', $search_term, 'both');
$this->db->or_like('created_at', $search_term, 'both');
}
if($is_count==1) {
$query = $this->db->get('faq');
return $query->num_rows();
} else {
$query = $this->db->get('faq');
return $query->result_array();
}
}
}
$perPage
와 $start_index
를 주의해서 본다.
# controllers/Prac.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Prac extends CI_Controller {
public function index() {
$this->load->model('prac_model');
// $this->data['faqs'] = $this->prac_model->getfaqs();
$this->load->library('pagination');
$perPage = 10;
$config['base_url'] = 'https://www.brandzip.net/prac';
$page = 0;
// $config['page_query_string'] = 'page';
if($this->input->get('page')) {
$page = $this->input->get('page');
}
$start_index = 0;
if($page !== 0) {
$start_index = $perPage * ($page - 1);
}
else{
$start_index = 0;
}
$total_rows = 0;
if ($this->input->get('search_term') != null) {
$search_term = $this->input->get('search_term');
$this->data['faqs'] = $this->prac_model->getSearchFaqs($perPage, "$start_index", $search_term, $is_count=0);
$total_rows = $this->prac_model->getSearchFaqs(null, null, $search_term, $is_count=1);
} else {
$this->data['faqs'] = $this->prac_model->getSearchFaqs($perPage, "$start_index", null, $is_count=0);
$total_rows = $this->prac_model->getSearchFaqs(null, null, null, $is_count=1);
}
$config['total_rows'] = $total_rows;
$config['per_page'] = $perPage;
$config['enable_query_strings'] = true;
$config['use_page_numbers'] = true;
$config['page_query_string'] = true;
$config['query_string_segment'] = 'page';
$config['reuse_query_string'] = true;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = '처음';
$config['last_link'] = '끝';
$config['first_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['first_tag_close'] = '</span></li>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['prev_tag_close'] = '</span></li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['next_tag_close'] = '</span></li>';
$config['last_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['last_tag_close'] = '</span></li>';
$config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['num_tag_close'] = '</span></li>';
$this->pagination->initialize($config);
$this->data['page'] = $page;
$this->data['links'] = $this->pagination->create_links();
$this->load->view('prac/index', $this->data);
}
}
# prac/index.php
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Search with Pagination</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h3>Search with Pagination</h3>
<div class="card">
<div class="card-body">
<form action="/prac" method="get">
<div class="row g-3 align-items-center">
<div class="col-auto">
<input type="text" id="search_term" name="search_term" class="form-control" placeholder="검색어 입력" value="<?php if($this->input->get('search_term')) { echo $this->input->get('search_term'); } ?>" aria-describedby="">
</div>
<div class="col-auto">
<input type="submit" class="btn btn-success" value="검색" />
<input type="button" class="btn btn-light" onclick='location.href="/prac"' value="초기화" />
</div>
</div>
</form>
<table class="table table-striped table-bordered" style="margin-top: 20px; ">
<thead>
<tr>
<th>Num</th>
<th>Title</th>
<th>Body</th>
<th>Answer</th>
<th>Created_at</th>
<th>Hits</th>
</tr>
</thead>
<tbody>
<?php foreach ($faqs as $faq) { ?>
<tr>
<td><?= $faq['num'] ?></td>
<td><?= $faq['title'] ?></td>
<td><?= $faq['body'] ?></td>
<td><?= $faq['answer'] ?></td>
<td><?= $faq['created_at'] ?></td>
<td><?= $faq['hits'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?= $links ?>
</div>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
-->
</body>
</html>