2016-05-12 24 views
0

我製作了一個服務器,用於檢查人們是否在線,如果他們登錄,並且希望將該信息放入HTML表格中。Javascript在HTML表格中輸出在線用戶名

<?php 
session_start(); 
include_once '../php\connect.php'; 
function removeRefresh(){ 
    $query = connection()->prepare("UPDATE `online` SET `time` = `time`+1"); 
    $query->execute(); 
    $query = connection()->prepare("DELETE FROM `online` WHERE `time` > 5"); 
    $query->execute(); 
} 
function addOrRefresh($ID){ 
    $query = connection()->prepare("SELECT COUNT('id') FROM `online` WHERE `ID` = :ID"); 
    $query->bindParam(':ID', $ID); 
    $query->execute(); 
    $count = $query->fetchColumn(); 
    if($count == 0){ 
     $query = connection()->prepare("INSERT INTO `online`(`ID`, `time`) VALUES(:ID, 0)"); 
     $query->bindParam(':ID', $ID); 
     $query->execute(); 
    } 
    else{ 
     $query = connection()->prepare("UPDATE `online` SET `time` = 0 WHERE `ID` = :ID"); 
     $query->bindParam(':ID', $ID); 
     $query->execute(); 
    } 
} 
$action = filter_input(INPUT_GET, 'action'); 
if($action == 'heartbeat'){ 
    removeRefresh(); 
    $ID = $_GET['id']; 
    addOrRefresh($ID); 
    $query = connection()->prepare("SELECT u.username FROM `nusers` u INNER JOIN `online` o ON o.ID = u.ID"); 
    $query->execute(); 
    $onlineUsers = $query->fetchAll(PDO::FETCH_ASSOC); 
    $resultaat = ""; 
    foreach($onlineUsers as $user){ 
     $resultaat .= "<p>{$user['username']} <br></p>"; 
    } 
    echo "$resultaat"; 
} 

?> 

可以註冊,當你登錄進大堂,你加入到「在線」表中,檢查每一秒與功能「心跳」。

請原諒我可憐的英語。

編輯:我有系統工作。我只想改變'echo'$ resultaat'',這樣它就可以將$ resultaat放入一個表格中。 在'}'後面添加不起作用,我嘗試過繞過這個方法,如果我在回顧過程中嘗試了所有可能性,則不確定。請求後,我發佈了整個文檔。 另一個javascript部分被整合到另一個文件中;包含:

<?php 
session_start(); 
$ID = $_SESSION['ID']; 


if (isset($_POST['logout'])) { 
    session_destroy(); 
    $query = connection()->prepare("DELETE FROM `online` WHERE `ID` = :ID"); 
    $query->bindParam(':ID', $ID); 
    $query->execute(); 
} 
?> 

<html> 
    <head> 
     <title>Stratego</title> 
     <Link href="../layout/style.css" type="text/css" rel="stylesheet" /> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 

     <script src="./js/HeartBeat.js"></script> 
     <script> 
      $(document).ready(function() { 
       heartbeat.setSpelerId(<?php echo $ID; ?>); 
       heartbeat.polling(); 
      }); 
     </script> 
    </head> 
    <body> 
     <form method='post' name='logout' action="../setup/logout.php"> 
      <input type='submit' name='loggout' value='Log me out'>    
     </form> 
     <a href="lobby1.php">Singleplayer</a> 
     <div id="online-list"> 
     </div> 
    </body> 

    <?php echo "<script> 
     function heartbeat(){ 
      var xhttp = new XMLHttpRequest(); 
      xhttp.onreadystatechange = function() { 
       if (xhttp.readyState == 4 && xhttp.status == 200) { 
       console.log(xhttp.responseText); 
       document.getElementById(\"online-list\").innerHTML = xhttp.responseText; 
       } 
      }; 
      xhttp.open(\"GET\", \"./wachtruimterInterface.php?action=heartbeat&id=" . $ID . "\", true); 
      console.log('keeping in touch ༼つ ◕_◕ ༽つ'); 
      xhttp.send(); 
     } 
     setInterval(heartbeat, 1000); 
    </script>"; ?> 
</html> 

控制檯日誌在那裏檢查連接是否被維護。人們使用他們的ID登錄,沒有密碼。

這就是它現在的樣子,有1個人在線;命名爲Luuk。 http://www.filedropper.com/screenproof 我的目標是讓在線的所有人進入一張桌子。 我試過

 $resultaat .= "<tr><td>{$user['username']} <br></td></tr>"; 
} 
echo "<table>$resultaat</table>"; 

剛纔,好像沒有工作,就如何取得進展的任何提示?

+0

你到目前爲止嘗試過什麼? – itzmukeshy7

+0

我試過多次在'Echo'$ resultaat''之前,之後編輯代碼,但我根本沒有經驗/知道足以在JavaScript腳本中創建html表。 –

+0

確保'connection() - > p'是正確的; – itzmukeshy7

回答

1

我已經解決了這個問題;

$resultaat = ""; 
foreach($onlineUsers as $user){ 
    $naam= $user['username'] 
    $resultaat .= "<tr><td>$naam</td></tr>"; 
} 
echo "<table border=1>$resultaat</table>";