2012-05-03 6 views
-3

私のウェブサイト用のダッシュボード機能を作成しています。正しくソートするのに問題があります。ダッシュボードには、彼らが従っている人々の最近のステータスアップデートがすべて含まれます。私はこれに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. 

投稿日によってソートする必要があります。私が問題を抱えているのは、ユーザーがフォローしている人々を入手しなければならないからです。どんな助け?

+1

自己参照関係の区切られたテキストリスト?正規化自殺... –

+0

何を; sのdb querry、希望の並べ替え順序は何ですか? –

+0

他にどのように保存すればよいですか? – user1059057

答えて

1

ここで私は行きます。 (これはあなたがすでにデータベースに接続されている前提)

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>'; 

      } 

     } 
    } 

私を、私は少し

相続人はSQLにSQLテーブルを変更しなければなりませんでしたテストされ、それはちょうどうまく動作するように思われる

あなたが必要なものを希望します。

関連する問題