It’s good that I searched and searched all around, but I still can’t find a solution to my problem. I'm still new to php and codeigniter, so maybe I skipped the answer already, but anyway, this is what I'm trying to do.
This is my controller (c_index.php) - it calls the search function and parses the resulting array.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_index extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('m_search');
$this->load->library("pagination");
$this->load->library("table");
}
public function index()
{
if($this->session->userdata('logged_in')){
$this->load->view('v_user');
}
else{
$this->load->view('index');
}
}
public function search()
{
$search_term = $this->input->post('word');
$books = $this->m_search->search_books($search_term);
$config['base_url'] = "http://localhost/test/index.php/c_index/search";
$config['per_page'] = 5;
$config['num_links'] = 7;
$config['total_rows'] = count($books);
echo $config['total_rows'];
$this->pagination->initialize($config);
$data['query'] = array_slice($books,$this->uri->segment(3),$config['per_page']);
$this->load->view("index",$data);
}
}
Here's my look (index.php) - basically just displays the pagination result
<h3> Search Results </h3>
<table class="table table-condensed table-hover table-striped" id="result_table">
<tbody>
<?php
if(isset ($query)){
if(count($query)!=0){
foreach($query as $item){
echo "<tr><td>". $item['title'] ." by " .$item['author'] ."<br/></td></tr>";
}
echo $this->pagination->create_links();
}
else
{
echo "No results found for keyword ' ". $this->input->post('word')." ' .";
}
}
else
{
echo "Start a search by typing on the Search Bar";
}
?>
</tbody>
</table>
My model (m_search.php) is basically looking for a database and returning an array of results.
<?php
class M_search extends CI_Model {
function search_books($search_term='default')
{
$filter = $this->input->post('filter');
if($filter == 'title')
{
$this->db->select('*');
$this->db->from('book');
$this->db->like('title',$search_term);
$query = $this->db->get();
return $query->result_array();
}else if($filter == 'author')
{
$this->db->select('*');
$this->db->from('book');
$this->db->like('author',$search_term);
$query = $this->db->get();
return $query->result_array();
}else if($filter == 'type')
{
$this->db->select('*');
$this->db->from('book');
$this->db->like('book_type',$search_term);
$query = $this->db->get();
return $query->result_array();
}else if($filter == 'status')
{
$this->db->select('*');
$this->db->from('book');
$this->db->like('book_status',$search_term);
$query = $this->db->get();
return $query->result_array();
}else
{
$this->db->select('*');
$this->db->from('book');
$this->db->like('book_status',$search_term);
$this->db->or_like('book_type',$search_term);
$this->db->or_like('author',$search_term);
$this->db->or_like('title',$search_term);
$query = $this->db->get();
return $query->result_array();
}
}
}
Now my problem is saving the results for pagination.
, , , .
- , , search_term , , , , .
. .