Welcome Guest, Not a member yet? Register   Sign In
How do you sort!?
#1

[eluser]Tom Taila[/eluser]
I basically have a comment system set up, and each comment has a "score" which is very similar if not the same as the "like" button in facebook, the more users that click my "vote up" button the higher the comments score becomes. Now, thats all fine, the problem is however i want to create a sort code where by i can sort the comments by "highest scoring to lowest scoring" and also vice versa. Anyone have any ideas? thanks very much Smile
#2

[eluser]mattpointblank[/eluser]
Add an optional argument to your controller indicating how to sort (eg by score descending, or ascending, etc). Send this argument to your model when fetching comments and order your query by it accordingly.
#3

[eluser]WanWizard[/eluser]
I store sort fields, sort order, filter and pagination values in the session (in an array with a "screen_id" as index so I can store it for multiple screens), so I don't have to pass them in the URL, and they are retained when the user revisits the page later in the session.
#4

[eluser]pickupman[/eluser]
Tom,
Are you creating a feature similar to digg's comment system where you can sort by votes or by thread? I know you were working on creating threaded comments before. Are you planning on sorting on the main parent comment?
#5

[eluser]Tom Taila[/eluser]
Thanks for the posts guys, Judging by all your comments it seems i have some reading to do, matt,i hope im not asking you of too much but could you please maybe give me a small example of what you mean, and wanwizard im not sure i understand what you mean, not that you havnt explained it well its probably my noobiness lol
pickupman, yea im not 100% familiar with digg ive never used it but ive briefly checked it out upon reading your comment and yea i guess its quite similar, except im not sure if in digg you can comment on comments or sort it as ive specified. Thanks again
#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).
#7

[eluser]Tom Taila[/eluser]
thanks for the help man, really helps and pushes me in the right direction and gives me something to work from. i think imma have to look into it more tho coz it still doesnt make perfect sense but im sure ill figure it out. I'll probably leave this towards the end of my project coz it seems kind of advanced now i know the answer lol, this could me trying to run before i can crawl if you kno what i mean, thanks again so much, cheers




Theme © iAndrew 2016 - Forum software by © MyBB