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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类