简体   繁体   English

无需cron作业即可连续运行php函数

[英]Running php function continuously without cron job

i having a little issue firstly, please ignore my depreciated functions i am trying to run PHP function continuously i have a button when clicked triggers an Ajax script which runs properly the Ajax page inserts the like into the db however i need to show the user that his like has counted but the like function count script is not updating until i reload the page, is there a way for me to continuously reload the PHP function so if any new like enters the db it updates, thanks in advance. 首先我有一个小问题,请忽略我已贬值的函数,我正在尝试连续运行PHP函数,当单击时会触发一个按钮,该按钮会触发一个Ajax脚本,该脚本可以正常运行。Ajax页面将类似的内容插入到db中,但是我需要向用户显示他的like已经计数了,但是like函数计数脚本直到我重新加载页面后才更新,有没有办法让我不断地重新加载PHP函数,所以如果有任何新的like输入它更新的数据库,请提前感谢。

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

Your like.php echo's out the like count but you are not doing anything with that value. 您的like.php echo不在此计数,但是您没有使用该值做任何事情。 Add a success callback to your ajax call and update your element that holds the like count 将成功回调添加到您的ajax调用中,并更新包含“喜欢”计数的元素

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

As a side note you have echo likes_count($postid); 附带说明一下,您可以echo likes_count($postid); but your function doesn't return anything so there is nothing to echo, you already echo out the value within the function. 但是您的函数不会返回任何内容,因此没有任何要回显的内容,您已经在函数中回显了该值。 So you do not need the echo before likes_count($postid) 所以,你不需要echo之前likes_count($postid)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM