2017-06-19 52 views
0

我想我的網站顯示每個用戶的硬幣,當我給用戶添加一些硬幣時,我希望他不需要重新加載頁面以獲得正確的金額。MYSQL實時顯示

這麼短,我只是想實時顯示mysql值。

我的代碼

   <?php 
 
       if($admin == 1) 
 
\t \t \t \t { 
 
\t \t \t \t echo' 
 
\t \t \t \t <li class="text-muted menu-title">Admin Panel</li> 
 
\t \t \t \t <li class="has_sub admin-content"> 
 
       <a href="#" class="waves-effect '; if($page=="ad"){echo'subdrop';} echo'"><i class="ti-user"></i><span>Admin</span> </a> 
 
       <ul class="list-unstyled '; if($page=="ad"){echo'"style="display: block;"';} echo'" style=""> 
 
\t \t \t \t <li><a href="apu.php">Premium users</a></li> 
 
\t \t \t \t <li><a href="abu.php">Banned users</a></li> 
 
\t \t \t \t <li><a href="au.php">Users</a></li> 
 
       </ul> 
 
\t \t \t \t </li> 
 
\t \t \t \t '; 
 
          
 
    } 
 
\t if(isset($_GET["action"])) 
 
{ 
 
\t if($_GET["action"] == "view") 
 
\t { 
 
\t \t $sid = $_GET["id"]; 
 
\t } 
 
\t if($_GET["action"] != "view") 
 
\t { 
 
\t \t echo '<script>location.href="index.php" </script>'; 
 
\t } 
 

 
} 
 
if(!isset($_GET["action"])) 
 
{ 
 
\t if(isset($_SESSION["steamid"])) 
 
\t { 
 
\t \t $sid= $_SESSION['steamid']; 
 
\t } 
 
\t else 
 
\t { 
 
\t \t echo '<script>location.href="index.php" </script>'; 
 
\t } 
 
} 
 

 
$sid=mysql_real_escape_string($sid); 
 

 
$exists=fetchinfo("steamid","users","steamid",$sid); 
 

 
if(!$exists) 
 
{ 
 
\t echo '<script>location.href="index.php" </script>'; 
 
} 
 

 
\t $crdts=fetchinfo("credits","users","steamid",$sid); \t 
 

 
\t 
 
\t if($reg) 
 
\t { 
 
\t \t $reg2date=date('Y-m-d', $reg); 
 
\t } 
 
\t else 
 
\t { 
 
\t \t $reg2date='Unknown'; 
 
\t } 
 
\t if($gp==0 || $gw==0) 
 
\t { 
 
\t \t $wr=0; 
 
\t } 
 
\t else 
 
\t { 
 
\t \t $wr=($gw/$gp)*100; 
 
\t } 
 
\t 
 
\t if($premium==1) 
 
\t { 
 
\t $id=$_SESSION['steamid']; 
 
\t $time=time(); 
 
\t $puntil = fetchinfo("puntil","users","steamid","$id"); 
 
\t if($puntil<=$time) 
 
\t { 
 
\t \t 
 
\t \t mysql_query("UPDATE users SET `premium`='0' WHERE `steamid`='$id'"); 
 
\t \t mysql_query("UPDATE users SET `profile`='1' WHERE `steamid`='$id'"); 
 
\t \t 
 
\t } 
 
} 
 

 
?> 
 
\t \t 
 
\t \t 
 
\t \t \t \t \t \t \t \t \t  <div class="credits1"> 
 
             <li class="text-muted menu-title">Credits</li> 
 
             <br> 
 
\t \t \t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t \t \t \t <div id="credits"><?php echo $crdts; ?></div> 
 
        
感謝您的關注,我希望你能幫助我。

+0

要改變的東西沒有重新加載你需要使用Ajax。 看看這個 https://www.w3schools.com/xml/ajax_php.asp –

+0

你應該也看看網絡套接字。這將確保您僅在需要時才從數據庫檢索新數據。我認爲對於PHP http://socketo.me/ – Tik

+0

這涉及到的問題遠遠多於提及的問題。爲了進行實時更新,您需要有一種機制將請求發送到服務器,查詢適當的表並在接收到響應後操作DOM。 Ajax和websockets是執行此操作的兩種常見機制。 – btl

回答

0

您需要使用ajax請求才能從源獲取異步數據。最常見的方式是JavaScript的Ajax請求,但我相信你已經知道了jQuery的,如果不是你可以很快得到它,請參閱:http://jquery.com

首先,你需要設置一個PHP文件,從數據庫返回的數據,你需要

假設ajax.php

// make db connection 
// get related data from db 
// return the data (coins) 

// set result array 
$result = [ 
    'coins' => $dbResult['coins'] 
]; 

// you can use json, so that you can easily manipulate it 
// encode to json the array 
$json = json_encode($result); 

echo $json; 

// or you could jus echo the coins as well, like: 
echo $dbResult['coins']; 

ajax.js

$(function(){ 
    $.ajax({ 
     url: "/api/getWeather", 
     dataType: json, // if you get a json, using this better than parsing result to json 
     success: function(result) { 
     // updating coins in the DOM for json return 
     $("selector to show coins").text(result.coins); 

     // or updating coins for plain text return 
     $("selector to show coins").text(result); 
     } 
    }); 
}); 

您可以更新或添加新的功能,你的代碼。