用户单击更新时下拉更新

时间:2014-07-21 17:26:02

标签: javascript php jquery html mysql

大家好,请帮帮我!我想要实现的是类似于Twitter更新通知栏,它显示新推文的数量和点击它时;它删除了之前推文的最新推文。我已经好好打了好几天了,这是我试过的。

//feed.php
<?php
session_start();
        $cxn = mysqli_connect('localhost','root','','my_db');
        $query = "SELECT insertion_time FROM feeds ORDER BY insertion_time DESC LIMIT 0,1";
    $result = mysqli_query($cxn, $query) or die (mysqli_error($cxn));
    $latest_feed =  mysqli_fetch_assoc($result);
        $_SESSION['latest_id'] = $latest_feed['insertion_time'];
        $latest_news = $_SESSION['latest_id']; 

 echo $check = <<<JS_SCRIPT
    <script>
         interval = setInterval(function(){
            check_update($latest_news);
        },5000);
    </script>
JS_SCRIPT;
?>
<script src='jquery.js'></script>
<script>
    function check_update(old_feed)
    {
        $.post('server.php',{get_num_update: old_feed},function(data){
                $("#update_bar").html(data);
            }); //checks for number of updates


        $.post('server.php',{retrieve_update: old_feed},function(data){
                $("#hidden_div").html(data);
            }); //retrieves the update into a div 
    }
      $(function(){
       $("#update_bar").click(function(){
          $("#hidden_div").prependTo("#news_feed_container").fadeIn(500);
        });
 });

</script>


//server.php
if(isset($_POST['get_num_update']) && !empty($_POST['get_num_update']) && is_numeric($_POST['get_num_update']))
{
$old_feed = $_POST['get_num_update'];

$query = "SELECT id FROM feeds WHERE insertion_time > $old_feed ORDER BY  insertion_time DESC";
$exec = mysqli_query($cxn, $query) or die(mysqli_error($cxn));
    $num_updates = mysqli_num_rows($exec);
    echo ($num_updates > 0) ? $num_updates.' new updates' : '';

}
  if(isset($_POST['retrieve_update']) && !empty($_POST['retrieve_update']) && is_numeric($_POST['retrieve_update']))
{ 

      while($result = mysqli_fetch_assoc($exec))
     {
        extract($result);

        echo <<<HTML
            //inserting the variable into html
   HTML;


      }

  }

// 当用户点击update_bar div时,会显示类似于&#39; 5个新更新&#39;我希望更新从隐藏的div中删除最新的Feed,所以一切都不能正常工作,因为我希望有人请帮助我

1 个答案:

答案 0 :(得分:0)

未经测试,但应该大致有效......

//feed.php
<?php
session_start();
        $cxn = mysqli_connect('localhost','root','','my_db');
        $query = "SELECT insertion_time FROM feeds ORDER BY insertion_time DESC LIMIT 0,1";
    $result = mysqli_query($cxn, $query) or die (mysqli_error($cxn));
    $latest_feed =  mysqli_fetch_assoc($result);
        $_SESSION['latest_id'] = $latest_feed['insertion_time'];
        $latest_news = $_SESSION['latest_id']; 

 echo $check = <<<JS_SCRIPT
    <script>
         // made the parameter of check_update a js variable and not hard coded in PHP
         var latest_new=$latest_news;
         // add a temp js variable for the last feed received (but not displayed)
         var last_received=$latest_news;
         interval = setInterval(function(){
            check_update(latest_news);
        },5000);
    </script>
JS_SCRIPT;
?>
<script src='jquery.js'></script>
<script>
    function check_update(old_feed)
    {
        // change your AJAX request to deal with JSON data and received several informations (number of new feed, insertion time of the last one)
        $.post('server.php',{get_num_update: old_feed},function(data){
                $("#update_bar").html(data.number_recents+" new updates.");
                last_received=data.last_time;
            },'json');
        $.post('server.php',{retrieve_update: old_feed},function(data){
            $("#hidden_div").html(data);
        }); //retrieves the update into a div
    }
      $(function(){
       $("#update_bar").click(function(){
          $("#hidden_div").prependTo("#news_feed_container").fadeIn(500);
          latest_new=last_received;
        });
 });

</script>


//server.php
if(isset($_POST['get_num_update']) && !empty(get_num_update']))
{
// header to serve JSON data
header('application/json');
$old_feed = $_POST['get_num_update'];
// request the number of new feed and the mst recent insertion time
$query = "SELECT count(*) as number,max(insertion_time) as last_time FROM feeds WHERE insertion_time > $old_feed ORDER BY  insertion_time DESC";
$exec = mysqli_query($cxn, $query) or die(mysqli_error($cxn));
    $feed_info =  mysqli_fetch_assoc($exec);
    //write the JSON data
    echo '{"number_recents":'.$feed_info['number'].',"last_time":'.last_time.'}';

} else if(isset($_POST['retrieve_update']) && !empty($_POST['retrieve_update']) && is_numeric($_POST['retrieve_update']))
{ 

      while($result = mysqli_fetch_assoc($exec))
     {
        extract($result);

        echo <<<HTML
            //inserting the variable into html
   HTML;


      }

  }