• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you sort!?

#6
[eluser]mattpointblank[/eluser]
Looking at this thread, WanWizard's idea is better.

In your controller, have a function that sets a session variable which indicates how to sort the data, then redirects the user back to the previous page. When they click the 'sort ascending' button or whatever, send them here, and then when you query the database for your comments, check if the session variable exists. If it does, filter the data accordingly.

Eg:

View:
Code:
<a href="&lt;?php echo site_url('controller/sort_comments_by_date_ascending'); ?&gt;">Sort comments by date (ascending)</a>

Controller:
Code:
function sort_comments_by_date_ascending()
{
    $this->session->set_userdata('comment_sort', 'date_posted ASC');
    redirect('controller/method');
}

function method()
{
    // get comments, with optional parameter for sorting
    $this->Some_model->getComments($this->session->userdata('comment_sort'));
}

Model:
Code:
function getComments($sort_options = NULL) {
    if($sort_options)) {
       $this->db->order_by($sort_options);
    }
    return $this->db->get('comments');
}

Hope this makes sense.

Things to watch out for:

- These session variables will persist across the whole browser session - make sure this is what you want.

- You'll need to do something smart to redirect the user back to where they were before when they click the sort button. Maybe store each page URL in a session var too and redirect to it after saving the sort option?

- This could potentially be prone to attack if someone inserts malicious code into the session var. Maybe look at a better way of doing that part (eg. use a key name instead of direct SQL and then a switch statement to translate it in the model).


Messages In This Thread
How do you sort!? - by El Forum - 05-25-2010, 10:03 AM
How do you sort!? - by El Forum - 05-25-2010, 10:05 AM
How do you sort!? - by El Forum - 05-25-2010, 10:14 AM
How do you sort!? - by El Forum - 05-25-2010, 11:03 AM
How do you sort!? - by El Forum - 05-25-2010, 02:09 PM
How do you sort!? - by El Forum - 05-25-2010, 02:18 PM
How do you sort!? - by El Forum - 05-25-2010, 02:56 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.