2013-12-20 21 views
0

我想從數據庫中獲取前三個值並顯示在頁面上。顯示的內容應該被刷新每隔1小時長輪詢從數據庫獲取數據和頁面上顯示

invite.php:

<html>  
    <head></head>  
    <body>  
     <div class="page-header header">  
      <h1 class="page-header-text">thenwat</h1>  
     </div>  
      <tble border="0" width="100%">  
       <tr>  
       <div class="middle">  
         <td style="width:60%">  
          <div>  
       <div id="show"> 
        url1.com - sentiment value <br> 
        url2.com - sentiment value <br> 
        url3.com - sentiment value <br> 
       </div>  
      </div>            

         </td>     
        </div>  
       </tr>  
      </table> 
    </body> 
    <script type="text/javascript"> 
     var id =123; 
     function doIt(){ // I want this functoin to be called after 5 second of the page load 
      $("#show").load("refresh.php"); // How to post id to refresh.php page here? 
     } 
     $(document).ready(function(){ 
      timeoutId = setTimeout(function(){ 
       doIt(); 
       intervalId = setInterval(function(){ 
        doIt(); 
       }, 5000); //Request the doIt() method every 5ms. 
      }, 3000); //Delay calculated on the server to trigger the function at the appropriate time 
     }); 
    </script> 
</html> 

refresh.php:

<?php    
     $id=$_POST['id']; 
     $con = mysqli_connect('127.0.0.1', 'root', '', 'karim'); 
     if (mysqli_connect_errno()) 
     { 
      echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
      return; 
     } 
     $insertQuery1 = "SELECT url,sentiment, count(url) from userpost where userid='".$userid."' group by url order by count(url) desc limit 3"; 

     //Get top thee url with their sentiment value 
     // This url would be displayed on invite.php page 
     if (!mysqli_query($con,$insertQuery1)) 
      { 
       die('Error: ' . mysqli_error($con)); 
      } 
?> 

問題:

  1. 如何文章ID新鮮.php
  2. 如何在refresh.php上從invite.php獲取記錄記錄 - show
  3. 如何從refresh.php發送前三名紀錄invite.php

回答

1

1.how到文章ID fresh.php

2.how到diplay記錄從refresh.php牽強在invite.php - 顯示DIV

3.how從refresh.php發送前三名紀錄invite.php

因爲你可以做一個單一的解決方案這三個問題。

使用AJAX

在您invite.php使用AJAX像folllowing

$(document).ready(function(){ 
    setInterval("getData()",3600000);//Polls in every 1 hour 
}); 

function getData(){ 
    var id =123; 
    $.ajax({ 
       url  : "refresh.php", 
       type  : "POST", 
       data  : {"id" : id}, 
       dataType : "json", 
       success : function(data) { 
       var response = JSON.stringify(data); 
       res    = JSON.parse(response); 
       console.log(res);//To see the response in browser console 
       //palce the contents in show div 
       } 
      }); 
} 

一個在invite.php

執行代碼發送到效應初探您ajax.Then發響應爲json編碼的響應。請參閱json_encode()瞭解更多信息。

相關問題