Welcome Guest, Not a member yet? Register   Sign In
Sorting Database columns using codeigniter
#9

[eluser]ramabodhi[/eluser]
i have been working diligently on getting this to work properly in one of my apps, and I've almost got it done, heres what i have so far hopefully some of the more experienced people can build on it:


This is what your controller should look like, minus all the validation and any other special stuff you need for your specific app
Code:
class Search extends Controller
{
    function Search()
    {
        parent::Controller;
        $this->load->model('search_model');
    }
    
    function Index()
    {
        $this->load->view('search_form');
    }
    function Basic($keyword = $this->input->post('keyword'), $sort_field = 'default_field', $sort_order = 'default_order' )
    {
        //Using form input to determine what fields to search in the table with $keyword
        $section = $this->input->post('section');
        //Start prepping the query
        foreach($section as $key => $tbl_field)
        {
            //For first field generate 'like' statement, the rest get 'or_like'
            if($key == 0) {$this->db->like($tbl_field, $keyword); }
            if($key > 0) { $this->db->or_like($tbl_field, $keyword); }
        }
        //Perform the query, and set the results as an array
        $query = $this->db->get('table_name');
        $result = $query->result_array;
        //Sort the Array
        $result = $this->search_model->orderBy($result, $sort_field, $sort_order);
        $data['result'] = (object)$result;  //I like to work with objects in my views
        //Load the view with the sorted search results
        $data['keyword']=$keyword;        //
        $data['sort_field']=$sort_field;  // send these to the view for sorting links
        $data['sort_order']=$sort_order   //
        $this->load->view('search_results', $data);
   }
}

Here is the Search Model
Code:
class Search_Model extends Model
{
function Search_Model()
{
    parent::Model;
}
    
function orderBy($results, $tbl_field, $order)
{    
    if($order=='asc'){
        $code = "return strnatcmp(\$a['$tbl_field'], \$b['$tbl_field']);";
        usort($results, create_function('$a,$b', $code));
        return $results; }
    elseif($order=='desc'){
        $code = "return strnatcmp(\$b['$tbl_field'], \$a['$tbl_field']);";
        usort($results, create_function('$a,$b', $code));
        return $results; }
    else return $results;
}

//This function will be used in the view, to reverse sort order upon clicking same field twice
function fieldTest($field, $curr_field, $curr_order)
{
    function orderSwap($order)
    {
        if($order == 'asc') { return 'desc'; }
        else { return 'asc'; }
    }
    if($field == $curr_field) { return orderSwap($curr_order); }
    else { return $curr_order; }
}

Then your results will be something like this
Code:
<?php

<h1>Search Results</h1>
<table id="search_results">
  <tr>
    <th>&lt;?=anchor('search/basic/'.$keyword.'/title/'.$this->search_model->fieldTest('title', $sort_field, $sort_order), 'Title')?&gt;</th>
    <th>&lt;?=anchor('search/basic/'.$keyword.'/description/'.$this->search_model->fieldTest('description', $sort_field, $sort_order), 'Description')?&gt;</th>
    <th>&lt;?=anchor('search/basic'.$keyword.'/post_date/'.$this->search_model->fieldTest('post_date', $sort_field, $sort_order), 'Date')?&gt;</th>
  </tr>
&lt;?php foreach($result as $row): ?&gt;
  <tr>
    <td>$row->title</td>
    <td>$row->description</td>
    <td>$row->post_date</td>
  </tr>
&lt;?php endforeach; ?&gt;
</table>

The beauty of this is that your sorting isn't restricted to the database, you can tweak and adjust your results as you please before running them through the sort function.

Now i've got one issue, on some of my pages all of my css is disappearing when i add the 5th uri string (the sort order).. everything sorts fine, but i lose my css..

any input on the whole scheme, and any advice on solving the css problem would be appreciate


Messages In This Thread
Sorting Database columns using codeigniter - by El Forum - 04-26-2009, 12:05 PM
Sorting Database columns using codeigniter - by El Forum - 04-26-2009, 12:12 PM
Sorting Database columns using codeigniter - by El Forum - 04-26-2009, 12:24 PM
Sorting Database columns using codeigniter - by El Forum - 04-26-2009, 12:26 PM
Sorting Database columns using codeigniter - by El Forum - 04-26-2009, 12:32 PM
Sorting Database columns using codeigniter - by El Forum - 04-26-2009, 12:35 PM
Sorting Database columns using codeigniter - by El Forum - 04-26-2009, 08:39 PM
Sorting Database columns using codeigniter - by El Forum - 04-26-2009, 09:49 PM
Sorting Database columns using codeigniter - by El Forum - 04-30-2009, 06:29 PM
Sorting Database columns using codeigniter - by El Forum - 04-30-2009, 07:43 PM



Theme © iAndrew 2016 - Forum software by © MyBB