在数据库中检索的信息旁边添加不同的链接

时间:2011-10-13 10:33:30

标签: php sql

所以我设法让我的注册用户的信息显示为已登录的成员,但我现在想要实现的是在每个用户名旁边添加一个链接,例如。

user_name< - “ADD AS FRIEND”

user_name< - “ADD AS FRIEND”

user_name< - “ADD AS FRIEND”

将另一位用户添加为您的朋友。

现在我不完全确定如何做到这一点......我必须使用某种for循环或什么?

我有一个members.php和一个database.sql文件

members.php

<!DOCTYPE HTML>
<head>
    <title> ShareLink </title>
    <link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
    <div id="wrapper">
    <?php

    //create_cat.php
    include 'connect.php';
    include 'header.php';

    if(isset($_SESSION['signed_in']) == false || isset($_SESSION['user_level']) != 1 )
        {
            //the user is not an admin
            echo '<br/>';
            echo 'Sorry! You have to be <a href="/signin.php"><b>logged in</b></a> to view all the <a href="signup.php" title="Become a registered user!"><b>registered</b></a> members.';
            echo '<br/><br/>';
        }
        else
        {
            echo '<h2>Registered users:</h2>';

            $sql    = "SELECT `user_name` FROM `users` ORDER BY `user_name` ASC";
            $result = mysql_query($sql);

            while($user = mysql_fetch_array($result))

            echo $user['user_name']. '<a href="#"><b> Add as friend </b></a><br/><br/>';  //14 spaces

            //NOW I WANT TO MAKE A SPECIFIC "ADD AS FRIEND" LINK NEXT TO EACH USER


        }
        include 'footer.php';
    ?>
</div>

和我的database.sql

CREATE TABLE users (  
user_id     INT(8) NOT NULL AUTO_INCREMENT,  
user_name   VARCHAR(30) NOT NULL,  
user_pass   VARCHAR(255) NOT NULL,  
user_email  VARCHAR(255) NOT NULL,  
user_date   DATETIME NOT NULL,  
user_level  INT(8) NOT NULL,  
UNIQUE INDEX user_name_unique (user_name),  
PRIMARY KEY (user_id)  
);

CREATE TABLE categories (  
cat_id          INT(8) NOT NULL AUTO_INCREMENT,  
cat_name        VARCHAR(255) NOT NULL,  
cat_description     VARCHAR(255) NOT NULL,  
UNIQUE INDEX cat_name_unique (cat_name),  
PRIMARY KEY (cat_id)  
);

CREATE TABLE topics (  
topic_id        INT(8) NOT NULL AUTO_INCREMENT,  
topic_subject       VARCHAR(255) NOT NULL,  
topic_date      DATETIME NOT NULL,  
topic_cat       INT(8) NOT NULL,  
topic_by        INT(8) NOT NULL,  
PRIMARY KEY (topic_id)  
); 

CREATE TABLE posts (  
post_id         INT(8) NOT NULL AUTO_INCREMENT,  
post_content        TEXT NOT NULL,  
post_date       DATETIME NOT NULL,  
post_topic      INT(8) NOT NULL,  
post_by     INT(8) NOT NULL,  
PRIMARY KEY (post_id)  
);

我是否必须添加好友表我希望用户将其他用户添加为朋友?

1 个答案:

答案 0 :(得分:1)

是的,你必须跟踪谁是谁的朋友...它本身不会发生;-)。友元表将包含user_id和friend_id(其他user_id的外键)。这种组合是独一无二的。