javascript - how to make voting system like stackoverflow using ajaxjqueryphp (efficient) - Stack Overflow

I'm trying to make post voting something similar to stack overflow vote up and vote down, now I ma

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 ments

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.

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 ments

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.

Share Improve this question edited Jan 18, 2010 at 22:08 Gandalf StormCrow asked Jan 18, 2010 at 21:36 Gandalf StormCrowGandalf StormCrow 26.2k70 gold badges179 silver badges268 bronze badges 6
  • Which database engine you are using? – Juha Syrjälä Commented Jan 18, 2010 at 21:45
  • 1 Don't forget the Who in voting. – Martijn Laarman Commented Jan 18, 2010 at 21:47
  • @Juha I'm using Mysql , @ Martijn - I'll worry about those things later I'm just trying to improve the performance – Gandalf StormCrow Commented Jan 18, 2010 at 21:49
  • To take jQuery out of it: try to make a normal HTML form where you can POST against your vote-up URL localhost/questions/vote_up/4. If that's taking just as long, it's not jQuery. – Wim Commented Jan 18, 2010 at 22:02
  • You could also remove the query for now, and see if that makes a difference. If it doesn't, it's something else in your code (maybe the startup phase of loading your app and all its configuration is what takes the most amount of time?). – Wim Commented Jan 18, 2010 at 22:03
 |  Show 1 more ment

3 Answers 3

Reset to default 6
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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742348101a4426958.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信