【问题标题】:Running php function continuously without cron job在没有cron作业的情况下连续运行php函数
【发布时间】:2017-01-23 00:43:27
【问题描述】:

我首先遇到了一个小问题,请忽略我的折旧函数他的喜欢计数的用户,但喜欢功能计数脚本在我重新加载页面之前不会更新,有没有办法让我不断重新加载 PHP 函数,所以如果有任何新的喜欢进入数据库,它会更新,提前致谢。

       //php function for counting likes functions.php
    function likes_count($postid){ 
      set_time_limit(0);
    $g = mysql_query("SELECT COUNT(*) FROM postlikes WHERE postid = $postid") or die (mysql_error());
    $co = mysql_fetch_array($g);
    $count = $co[0];
    echo $count;
    }

          //like button index.php where the image post appears for the user to like
        <button  style="margin-left:13px;" id="<?php echo $postid;?>" class="col-md-5 n btn btn-default btn-xs like"><i id="f"  class="fa fa-thumbs-o-up"></i> <span id="like_<?php echo $postid ?>"><?php echo likes_count($postid); ?> </span> Likes</button>


             //
        //ajax for calling the like page on index.php
       <script>
 $('.like').on('click', function (e){

              var userid = "<?php echo $ida ?>";
            var postid = $(this).attr('id');

        if ((postid == "")) {
        alert("no info bro");
    } else {

                $.ajax({
                type: "POST",
                url: "like.php",

               data: {postid: postid, userid: userid},
                cache: false,

                });
                } 
        e.preventDefault();
    });
      </script>



         //like page it self like.php
              <?php 

   include "connect.php";
   mysql_select_db("unilag");



if(isset($_POST['postid'])) {
   $postid=$_POST['postid'];
   $id=$_POST['userid'];

    $e = mysql_query("SELECT * FROM postlikes WHERE userid = $id AND postid = $postid") or die(mysql_error());
    if(mysql_num_rows($e) > 0) {
        // remove like&
        $re = mysql_query("DELETE FROM postlikes WHERE userid = $id AND postid = $postid") or die (mysql_error());
        $notify = mysql_query("DELETE FROM notification WHERE nfrom = $id AND type = 'like_post' AND  postid = $postid") or die (mysql_error());


    } else {
        // like post
        $re = mysql_query("INSERT INTO postlikes SET userid = $id, postid = $postid, date = now()") or die (mysql_error());
        $rr = mysql_query("SELECT userid FROM post WHERE post_id = $postid");
        $y = mysql_fetch_array($rr);
        $iii = $y['userid'];
        $notify = mysql_query("INSERT INTO notification SET nfrom = $id, nfor = $iii, type = 'like_post',  postid = $postid, seen = 'no', date = now()") or die (mysql_error());

    }
    echo likes_count($postid);
}   
?>

【问题讨论】:

  • 在js中使用setInterval,调用ajax函数定时更新点赞数。
  • 谢谢,但我在功能页面上使用了设置间隔,但我不断收到未定义的功能 setInterval @Sasikumar

标签: javascript php ajax cron


【解决方案1】:
               $('.like').on('click', function (e){

      var userid = "<?php echo $ida ?>";
    var postid = $(this).attr('id');

if ((postid == "")) {
alert("no info bro");
  } else {

        $.ajax({
        type: "POST",
        url: "like.php",

       data: {postid: postid, userid: userid},
        cache: false,
       }).then(function(count){
      $(".like-"+postid).text(count);
        });
        } 
e.preventDefault();
     });

【讨论】:

  • 它在本地主机上工作,谢谢,但在实时服务器上它没有工作可能是问题@Patrick Evans
【解决方案2】:

您的like.php 回显超出了点赞数,但您没有使用该值做任何事情。将成功回调添加到您的 ajax 调用并更新包含类似计数的元素

$.ajax({
  type: "POST",
  url: "like.php",
  data: {
    postid: postid,
    userid: userid
  },
  cache: false
}).then(function(count){
   $("#like_"+postid).text(count);
});

作为旁注,你有echo likes_count($postid);,但你的函数没有返回任何东西,所以没有什么可以回显,你已经回显了函数中的值。所以你不需要echo之前的likes_count($postid)

【讨论】:

  • 这是我在上面尝试过的@Patrick Evans,但仍然没有结果,请给我示例或解决方案,谢谢你整晚都在努力
  • @zuwi,我的示例没有为您的元素使用正确的选择器,经过编辑以匹配您的 id。确保使用正确的选择器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多