Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make voting system like stackoverflow using ajax/jquery/php (efficient)

I'm trying to make post voting something similar to stack overflow vote up and vote down, now I made it work with (but-it-works approach) and yet something doesn't feel right, hopefully someone will suggest some useful tweaks. Here is my jquery code :

 var x = $("strong.votes_balance").text();
   $("input.vote_down").click(function(){
     $.ajax({   
       type: "POST",  
       url: "http://localhost/questions/vote_down/4",   
       success: function()   
       {   
       $("strong.votes_balance").html((parseInt(x) - parseInt(1)));
       $("input[type=button]").hide();
       $(".thumbsup_hide").show();
       }   
      });  
    });


   $("input.vote_up").click(function(){
     $.ajax({   
       type: "POST",  
       url: "http://localhost/questions/vote_up/4",   
       success: function()   
       {   
       $("input[type=button]").hide();
       $("strong.votes_balance").html((parseInt(x) + parseInt(1)));
       $(".thumbsup_hide").show();
       }   
       });
    });


    });

Here is my HTML :

<div class="thumbsup thumbsup_template_up-down" id="thumbsup_49">


 <form method="post" id="voting_form">

<input type="hidden" value="49" name="thumbsup_id"/>
  <span class="thumbsup_hide">Result:</span>
  <strong class="votes_balance"><?=$row_q->post_vote?></strong>

  <input type="button" title="Good Comment!" value="+1" name="thumbsup_rating" class="vote_up"/>
  <input type="button" title="Bad Comment!" value="-1" name="thumbsup_rating" class="vote_down"/>
 </form>

</div>

$row_q->post_vote equals some number. Now when I click the vote up button it increments the value of strong and if I click vote down it decrements it.

I'm working with CI(codeigniter) not native php.

How do I measure performance of this, not so long ago vote up or vote down took more than two seconds to perform, I added LIMIT 1 to my query and now it works somewhat faster, I think this should perform lot faster. Thank you for your comments

And yes sorry for ommiting this here are my vote up and vote down functions :

$this->db->query("UPDATE $table SET $what_field = ($what_field + 1) $wheremore WHERE $what_id = '$value' LIMIT 1");

AND VOTE DOWN BELOW:

$this->db->query("UPDATE $table SET $what_field = ($what_field - 1) $wheremore WHERE $what_id = '$value' LIMIT 1");

UPDATE I think the reason why things are generaly slow is because of my inexpirience with jquery, I think my table is okey

UPDATE II

I just removed the php part from the jquery ajax function, just to increment the number by 1 and it still works really slow.

UPDATE III

When I run query with phpmyadmin it runs from range 0.3 second until 1.77 seconds it varies for some reason.

like image 206
Gandalf StormCrow Avatar asked Jan 18 '10 21:01

Gandalf StormCrow


2 Answers

WHERE $what_id = '$value'

You shouldn't use strings/varchars as identifiers. It's unnatural. Use numbers/integers.

WHERE $what_id = $value

Yes, this has influence on DB performance, especially when the rowcount gets high.

like image 121
BalusC Avatar answered Sep 19 '22 14:09

BalusC


Your database table $table should have an index on $what_id. Normally it would be the primary key, but since adding LIMIT 1 changed anything this likely is not the case?

like image 33
Wim Avatar answered Sep 21 '22 14:09

Wim