CodeIgniter Forums
Digg/Vote UP/Down Issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Digg/Vote UP/Down Issue (/showthread.php?tid=2489)



Digg/Vote UP/Down Issue - El Forum - 08-08-2007

[eluser]CodyPChristian[/eluser]
Hello I'm building a quote system where the users of the site can post quotes to and they can vote/digg them up and down, everything so far is going great but I have one slight problem and I don't understand which way to go with it. Here is the current code I have.

This is where I need the new code, this is the part I don't get how to do.
Code:
$order_id = '';

Here is the full code and the way it works is /quotes/$id/up or down
Code:
function up()
    {
        $order_id = '0';
        $id = $this->uri->segment(2);
        
        
        $this->db->where('id', $id);
        $this->db->set('order_id', $order_id);
        $this->db->update('quotes', $_POST);
        
        
        redirect('quotes');
        
    }
    
    function down()
    {
        $order_id = '0';
        $id = $this->uri->segment(2);
        
        
        $this->db->where('id', $id);
        $this->db->set('order_id', $order_id);
        $this->db->update('quotes', $_POST);
        
        
        redirect('quotes');
        
    }

I tested it above but entering a number in the $order_id and it does in fact update it to that number, so everything else is working fine, its just writing it to actually work.

I hope someone understands this. Thanks for the help!


Digg/Vote UP/Down Issue - El Forum - 08-08-2007

[eluser]Michael Wales[/eluser]
I really don't know what you are asking for here... it looks as if your code is doing what you are telling it to - placing a 0 in the order_id field.

I think you are asking for help in your logic - how do return the ordered items on the homepage? I would just use the up/down votes to tally a 'score' field. Then order by that score.

Once I had that working, I would add in some code that would scale the score based on how old the posting is - if it's from today, it's worth a whole point, yesterday only 0.8, this week 0.6, last week 0.4, any time further 0.1. This will ensure some of your older quotes from months ago don't magically reappear on the homepage because it received a lot of votes.

Finally, I would add in a time function - you don't always want to return the quotes with the highest score - you just want the recent ones with the highest score. This is easy using CI's now() function compared to a varchar(10) field in your database that is a Unix timestamp.

Hope this helps a bit...


Digg/Vote UP/Down Issue - El Forum - 08-08-2007

[eluser]CodyPChristian[/eluser]
okay, basicly what you said is what I'm needing todo. :-) But exactly how I need to go about doing it like you said is what is confusing me, examples would help. Thanks for replying :-)


Digg/Vote UP/Down Issue - El Forum - 08-08-2007

[eluser]Michael Wales[/eluser]
I'm not going to write out the code for you - but here's a rundown:

Make a score field (INT)
Make a created_at field (VARCHAR(10))
In your up/down controller's determine the time between the vote and the quote's created_at and +/- the score field based on that time (and the modifiers you have selected)
In your home controller do a simple MySQL query (SELECT * FROM quotes WHERE (NOW() - created_at < 7200) ORDER BY score DESC) - that would work for a 2 hour timeframe of quotes.

I think - I didn't test - but it's your app, I charge $65 / hr. for this.


Digg/Vote UP/Down Issue - El Forum - 08-08-2007

[eluser]CodyPChristian[/eluser]
Ah okay, Thanks :-)


Digg/Vote UP/Down Issue - El Forum - 08-08-2007

[eluser]CodyPChristian[/eluser]
Okay, I understand everything but one part and its the only part I don't have added in just yet. Is how the up and down part of it works with the 'score' and 'created_at' fields. Like walesmd said: "In your up/down controller’s determine the time between the vote and the quote’s created_at and +/- the score field based on that time (and the modifiers you have selected)". I have no clue how to do that and thats what I'm not understanding.

I've removed the order_id and added the score and created_ at fields but I can revert back to order_id if that is a easier way to do it since I'm not worried about how long ago something was posted and when it was posted, since the type of quotes that they are it is fine if quotes from months ago reappear on the first page.

Thanks for anyone who can help me.


Digg/Vote UP/Down Issue - El Forum - 08-08-2007

[eluser]CodyPChristian[/eluser]
Okay guys after many hours finally my good buddy Shadowhand on the IRC server for CI helped me out and man did it help me so much! He agreed that I could post the answer here so here you go!

Code:
function up()
    {
        $id = (int) $this->uri->segment(2);
        $top_id = 0;
    
        $query = $this->db->select('id')->orderby('order_id', 'desc')->limit(1)->get('quotes');
        if ($query->num_rows() > 0)
        {
            $top_id = $query->row()->id;
        }
    
        if ($top_id != $id)
        {
            $this->db->query('UPDATE quotes SET order_id = order_id + 1 WHERE id = ?', array($id));
        }
    
        redirect('quotes');
    }
    
    function down()
    {
        $id = (int) $this->uri->segment(2);
        $low_id = 0;
    
        $query = $this->db->select('id')->orderby('order_id', 'asc')->limit(1)->get('quotes');
        if ($query->num_rows() > 0)
        {
            $low_id = $query->row()->id;
        }
    
        if ($low_id != $id)
        {
            $this->db->query('UPDATE quotes SET order_id = order_id - 1 WHERE id = ?', array($id));
        }
    
        redirect('quotes');
    }

I hope you understand this and if anyone has a question just let me know Smile Thanks again to Shadowhand.