-3

Web サイトのダッシュボード機能を作成しています。うまく整理できなくて困っています。ダッシュボードには、フォローしているユーザーの最近のステータス更新がすべて含まれます。これにはMySQLとPHPを使用しています。

Status Table:

id: key value, auto-increments
user: the username of the poster of the status
status: the actual status that was posted
date: an int value, has the time that the status was posted

Users table: (Unneeded rows are excluded)
username: The user's name
following: All of the users that he is following. This is a text field, and is delimited by semicolons.

投稿日順にソートする必要があります。私が問題を抱えている理由は、ユーザーがフォローしている人を取得する必要があるためです。何か助けはありますか?

4

1 に答える 1

1

ここで私は行きます。SQLテーブルを少し変更する必要がありました

ここにSQL:

CREATE TABLE IF NOT EXISTS `status` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `user_id` int(3) NOT NULL,
  `user_status` text NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


INSERT INTO `status` (`id`, `user_id`, `user_status`, `date`) VALUES
(1, 1, 'Hi, Im Dave, User One.', '2012-05-03'),
(2, 2, 'Hi, Im Amy, User Two.', '2012-05-01'),
(3, 3, 'Hi, Im Lisa user 3', '2012-05-01'),
(4, 4, 'Hi, Im Joe user 4', '2012-05-02');


CREATE TABLE IF NOT EXISTS `users` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `following` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


INSERT INTO `users` (`id`, `username`, `following`) VALUES
(1, 'Dave', '2:3'),
(2, 'Amy', '1:4'),
(3, 'Lisa', '1:4'),
(4, 'Joe', '1:2:3');

そしてここにphpがあります(これはあなたがすでにデータベースに接続していることを前提としています):

    // Get user 2 (id 2) details from user table
    $res = mysql_query("SELECT * FROM users WHERE id='2'");
    while ($row = mysql_fetch_assoc($res)) {

        $username = $row['username'];
        $following = $row['following'];

    }

    if(!empty($following)) {
        $data = explode(':',$following);

        foreach($data as $user){

            // Now get user 2 (id 2) followers statuses
            $res = mysql_query("SELECT * FROM status WHERE user_id='$user'");
            while ($row = mysql_fetch_assoc($res)) {
                echo $user_status = $row['user_status'].'<br>';
                echo $date = $row['date'].'<br>';

            }

        }
    }

私はテストしました、そしてそれはうまくいくようです

それがあなたが必要とするものであることを願っています:)

于 2012-05-03T01:41:14.537 に答える