无需cron作业即可连续运行php功能

时间:2016-09-15 05:06:39

标签: javascript php ajax cron

我首先遇到一个小问题,请忽略我的折旧函数我试图连续运行PHP函数我点击时有一个按钮触发正确运行Ajax页面的Ajax脚本将数据插入数据库然而我需要显示他喜欢的用户已经计算但是相同的函数计数脚本在我重新加载页面之前没有更新,有没有办法让我不断重新加载PHP函数,所以如果有任何新的进入它更新的数据库,请提前感谢。< / p>

       //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);
}   
?>

2 个答案:

答案 0 :(得分:0)

你的喜欢.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)

答案 1 :(得分:0)

               $('.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();
     });