doulin9679 2016-09-15 05:06
浏览 33

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

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 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条回答 默认 最新

  • dourao1896 2016-09-15 05:15
    关注

    Your like.php echo's out the like count but you are not doing anything with that value. Add a success callback to your ajax call and update your element that holds the like count

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

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分