dragon8997474 2013-12-02 23:06
浏览 115
已采纳

使用页面创建网站以显示在线用户,包括用户名和图片[关闭]

I am in the stages of the class diagram, I was creating the class diagram for a website I am planning to create. It was going fine until I reached the stage of wanting to have a web page that displayed the online users e.g. showing their username and profile picture of all online users. I am not sure on how I would do this, the image is of what I have so far. I would appreciate any help or guidance.

Here is my current class diagram http://imgur.com/sgjJwkc

  • 写回答

2条回答 默认 最新

  • doujia8801 2013-12-02 23:53
    关注

    You could also set up a column for user's status (logged in, logged out) and make it toggle between 0 (logged out) and 1 (logged in). You could update this information every 5 seconds (in the background of course) using an AJAX call. Something like this:

    //JAVASCRIPT
    <script>
        $(document).ready(function() {
            setInterval(function() {
                $.post('Path To PHP File', {x : Pass Variables, y: If You Want}, function(res)
                    //Do something with the result (res)
                );
            }, 5000);
        });
    </script>
    
    //PHP FILE
    <?php
    //If you passed any variables to the script:
    $x = $_POST['x'];
    $y = $_POST['y'];
    
    //Connect to your database
    $dbConn = "I hope you're using PDO for this.";
    
    //Create your query
    $sql = "SELECT * FROM users WHERE status=1";
    $res = $dbConn->prepare($sql);
    $res->execute();
    
    //Return/echo results
    foreach($res as $x) {
        echo "<div id='useTheIdToStyleTheResults'>".$x['name']."</div>";
    }
    ?>
    

    res is whatever your php script returns. You can simply run an SQL query on your database in that script to get all users who are logged in and use a foreach() loop to return each item as an html div element. Style those elements to your liking and there you go. If you have questions, just ask!

    EDIT:

    After reading a little more of your question, SQL JOIN and UNION are a couple of concepts you might want to look into. http://www.w3schools.com/sql/sql_join.asp

    EDIT #2:

    //Define Variables
    $hostname = '127.0.0.1';
    $username = 'userName';
    $password = 'passWord';
    $dbname = 'database in use';
    
    //Create Connection
    try {
        $con = new PDO("mysql:host=$hostname;dbname=$dbname",$username,$password);
        $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        //echo "Connected to database";  //Uncomment statement to the left to check for connection
    } catch (PDOException $e) {
        print "Unable to connect: " . $e->getMessage();
        mysql_close($con);
        die();
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算