[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="<?php echo site_url('controller/sort_comments_by_date_ascending'); ?>">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).