开发者

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.


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.


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?


What does your database look like? Make sure that $what_id field is properly indexed, preferably it should be the primary key if possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜