I am working on implementing a search method in codeigniter, which uses code-breaking functionality. This required a little research, since we enable filters, and the user can select a variable number of filters, resetting the uri_segment count. I have a working solution below (only the published code that I felt was relative to understanding the process, not to be confused. If I missed something, let me know) that will get someone to start if they run into the same question, but you want to ask a few questions:
QUESTIONS:
Is there a better, more efficient way to paginate search results?
Is there anything that could be done to improve this method?
How to display the current uri in the user's browser after the first message? The way the pagination library works, it does not display "first_url" until it is clicked through the generated links. I gave up the idea of redirecting, but wanted to get feedback before going along this route.
VIEW
<form action="/account/classified/browse" method="post" id="searchForm">
<p>Search
<br/>
<input type="text" name="phrase" id="searchString" value="<?php echo $selectedPhrase; ?>"/></p>
<p>Type<br/>
<?php echo form_dropdown('type', $types, $selectedType); ?>
</p>
<p>County<br/>
<?php echo form_dropdown('location', $locations, $selectedLocation); ?>
</p>
<p>Market<br/>
<?php echo form_dropdown('market', $markets, $selectedMarket); ?>
</p>
<p>
<input type="submit" id="searchClassifiedButton" name="" value="Search"/>
</p>
</form>
<table>
<?
if (count($classifieds) > 0) {
foreach ($classifieds as $classified) {
echo '<tr>';
echo '</tr>';
}
} else {
echo 'There are no classifieds posted at this time';
}
?>
</table>
CONTROLLER:
<?
public function browse() {
$this -> load -> helper("url");
$this -> load -> library("pagination");
$this -> load -> model('classified_model');
$post = $this -> input -> post();
if ($post) {
foreach ($post as $key => $val) {
if ($val != '') {
$filters[$key] = $val;
}
}
} elseif ($this -> uri -> uri_to_assoc(4)) {
$filters = $this -> uri -> uri_to_assoc(4);
} else {
$filters = array();
}
$pageConfig = array();
$pageConfig['first_url'] = !empty($filters) ? '/account/classified/browse/0/' . $this -> uri -> assoc_to_uri($filters) : '/account/classified/browse/0';
$pageConfig["suffix"] = !empty($filters) ? '/' . $this -> uri -> assoc_to_uri($filters) : '';
$pageConfig["base_url"] = "/account/classified/browse/";
$pageConfig["per_page"] = 15;
$pageConfig["uri_segment"] = 3;
$pageConfig["total_rows"] = $this -> classified_model -> approved_classifieds_count($filters);
$this -> pagination -> initialize($pageConfig);
$page = ($this -> uri -> segment(3)) ? $this -> uri -> segment(3) : 0;
$data['classifieds'] = $this -> classified_model -> approved_classifieds($filters, $pageConfig["per_page"], $page);
$data['links'] = $this -> pagination -> create_links();
$data['selectedPhrase'] = (isset($filters['phrase'])) ? $filters['phrase'] : '';
$returnTypes = array('' => '-- View All --');
foreach($this->classified_model->classified_search_types() as $type){
$returnTypes[$type->id] = $type->name;
}
$data['types'] = $returnTypes;
$data['selectedType'] = (isset($filters['type'])) ? $filters['type'] : '';
$returnMarkets = array('' => '-- View All --');
foreach($this->classified_model->market_types() as $market){
$returnMarkets[$market->id] = $market->name;
}
$data['markets'] = $returnMarkets;
$data['selectedMarket'] = (isset($filters['market'])) ? $filters['market'] : '';
$returnLocations = array('' => '-- View All --');
foreach($this->classified_model->locations() as $key => $val){
$returnLocations[$key] = $val;
}
$data['locations'] = $returnLocations;
$data['selectedLocation'] = (isset($filters['location'])) ? $filters['location'] : '';
$this -> template -> build('browse_classified', $data);
}
MODEL:
public function classified_search_types(){
$q = $this->db->query("SELECT * FROM classified_types");
return $q->result();
}
public function market_types(){
$query = $this -> db -> get('market_types');
return $query -> result();
}
public function locations(){
$query = $this -> db -> get('locations');
return $query -> result();
}
public function approved_classifieds_count($filters){
$result = $this->search($filters);
return $result->num_rows();
}
public function approved_classifieds($filters, $limit, $start) {
$result = $this->search($filters, $limit, $start);
return $result->result();
}
function search($filters, $limit='', $start=''){
$this->load->helper('security');
$phrase = xss_clean((isset($filters['phrase'])) ? $filters['phrase'] : '');
$type = xss_clean((isset($filters['type'])) ? $filters['type'] : '');
$market = xss_clean((isset($filters['market'])) ? $filters['market'] : '');
$location = xss_clean((isset($filters['location'])) ? $filters['location'] : '');
$q = $this->db->query($query);
return $q;
}
source
share