[eluser]Devon Lambert[/eluser]
I've been at this one for a few hours now and thought it couldn't hurt to hit up the CI community for some much needed Ninja Style assistance.
Here's the deal:
I've created a quick and dirty CI search function and have made use of it in my controller and view. I've whipped up a table with a little bit of css styling that is calling in my search results. I do not want to break this setup and I do not want to shift any view code into my controller or model, besides, that wouldn't be the right MVC thing to do would it?
The main issue is that when I click on my 2nd paginated link, I receive the following error:
Code:
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: search.php
Line Number: 14
Here is the code I have so far:
CONTROLLER:
Code:
<?php
class search extends Controller {
function search()
{
parent::Controller();
$this->load->model('mdl_search');
}
function index()
{
$data['title'] = "Better Search";
$keyword = $this->input->xss_clean($this->input->post('keyword'));
$data['count_results'] = $this->mdl_search->countResults($keyword);
$this->load->library('pagination');
$config['base_url'] = base_url().'search/index';
$config['total_rows'] = $data['count_results'];
$config['uri_segment'] = '2';
$config['per_page'] = '10';
$this->pagination->initialize($config);
$offset = $this->uri->segment(3);
$data['search_results'] = $this->mdl_search->getSearchResults($keyword, $config['per_page'], $offset);
$this->load->view('search', $data);
}
}
?>
MODEL:
Code:
<?php
class mdl_search extends Model {
function mdl_search()
{
parent::Model();
}
function getSearchResults ($keyword, $limit, $offset)
{
$keyword = trim($keyword);
if($keyword != ''){
$this->db->like('name', $keyword);
$this->db->or_like('desc', $keyword);
$this->db->or_like('cat', $keyword);
$this->db->or_like('instructions', $keyword);
$this->db->orderby('name', 'ASC');
$result = $this->db->get('games', $limit, $offset);
$output = $result->result_array();
return $output;
}
else {return 0; }
}
function countResults ($keyword){
$keyword = trim($keyword);
$this->db->like('name', $keyword);
$this->db->or_like('desc', $keyword);
$this->db->or_like('cat', $keyword);
$this->db->or_like('instructions', $keyword);
$this->db->orderby('name', 'ASC');
$this->db->from('games');
$query = $this->db->count_all_results();
return $query;
}
}
?>
VIEW:
Code:
<div class="search-container">
<h2><?= $title;?></h2>
<div class="pagination"><?php echo $this->pagination->create_links(); ?></div>
<table class="search-results">
<tr>
<th><a href="#"><span>Game Title</span></a></th>
<th><a href="#"><span>Players</span></a></th>
<th><a href="#"><span>Rating</span></a></th>
<th><a href="#"><span>Reviews</span></a></th>
<th><a href="#"><span>Play Type</span></a></th>
</tr>
<? foreach ($search_results as $search_result): ?>
<tr>
<td><a href="#"><?php echo $search_result['name'];?></a><br /><?php echo $search_result['desc'];?></td>
<td>1,262</td>
<td><b>5.00</b><br />out of 5.0</td>
<td><span class="up">72%</span><br />491 reviews</td>
<td></td>
</tr>
<? endforeach; ?>
</table>
</div>
So far, I can get the search function to work just fine, but when I combine this with the pagination and go to any other page besides the first search page, it's as if the post data I went searching for gets thrown out.
Please, any help is definitely appreciated in getting this working. :-)