【问题标题】:Best approach for dynamic Laravel/Ajax image voting page动态 Laravel/Ajax 图像投票页面的最佳方法
【发布时间】:2014-02-07 19:32:00
【问题描述】:

我正在为图像投票比赛编写应用程序。我是这种基于 ajax 的动态页面的新手。我对如何正确制作有疑问,因为这是我的第一次尝试,我需要一些基于性能的问题的答案。我可以添加投票,但我无法获得图像的总票数。因此,我对性能感到困惑,因为所有用户都可以同时投票。如何显示每张图片的总票数。我的数据库表在第二个问题下面。

我为每张图片添加了一个投票按钮:

  <button type="submit" class="btn btn-mini upvote newsbutton" id="{{ $contestimage->id }}">
        <i class="fa fa-thumbs-up fa-2x"></i>
  </button>
  <div id="voteresponse"></div>

并使用 ajax 将表单发送到路由:

$('.upvote').click( function(event) {

          event.preventDefault();

          $("#voteresponse").html('');
            var voteID = $(this).attr("id");

            $.ajax({
              url: "voteimage",
              type: "post",
              dataType: "json",
              data: {id : voteID},
              success: function(data, textStatus){
                if(data.success == 'true'){
                  $('#voteresponse').html(data.message);
                  return true;
                }else{
                  $('#voteresponse').popover({
                    title: 'Hata!',
                    content: data.message 
                  });
                }
              },
              error:function(){
                  $('#voteresponse').popover({
                    title: 'error!',
                    content: 'Server error' 
                  });
              }
            }); 
          });

我的控制器动作是

   public function postVote($id = null)
    {    
        if (Sentry::check()) {

            if (Request::ajax())
            {

                $id=Input::get('contestImageId');
                $image = ContestImage::find($id);

                $vote = "1";
                $user = Sentry::getUser()->id;

                // Grab the vote if it already exists.
                $entry = Vote::where('user_id', $user)->where('contest_id', $image)->first();

                if (!empty($entry))
                {
                    $entry->vote = $vote;
                    $entry->save();
                }
                else
                {
                    $entry = new Vote;
                    $entry->user_id = $user;
                    $entry->contest_id = $image->id;
                    $entry->vote ="1";
                    $entry->save();
                }
            }
            else
            {
                return "Not an AJAX request.";
            }
        }
        else
        {
            return "User not logged in.";
        }
    }

代码有效,但我被困在这一点上。我不知道如何发回总投票数以及所有其他 ajax 页面如何添加脚本以在任何其他用户投票时刷新投票数?这会产生任何性能问题吗?

第二个问题与第一个问题有关:我在数据库中创建了我的投票迁移和收集

user_id(int)
contestimage_id(int)
vote(tinyinteger to use as 1)

我在选票和比赛图像之间有一个 belongsTo 和 hasMany 关系,我想使用 $contestimage-&gt;votes()-&gt;count(); 获得总计数,这是计算选票的好选择吗?

【问题讨论】:

    标签: php jquery ajax laravel-4


    【解决方案1】:

    您应该编写一个计算票数的函数,查看this 页面上的“原始表达式”,这应该可以帮助您。可以使用MySqlcount(*)来统计行数。

    使用该函数,在您的页面上显示投票数,然后在您成功运行 postVote 函数后让它返回总投票数..

    $entry = new Vote;
    $entry->user_id = $user;
    $entry->contest_id = $image->id;
    $entry->vote ="1";
    $entry->save();
    // RETURN NUMBER OF VOTES HERE
    

    然后让返回的数字更新包含您的总票数的 div

    if(data.success == 'true'){
    $('#voteresponse').html(data.message);
    $('#totalvotes').html(data.totalvotes);
    return true;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      相关资源
      最近更新 更多