1

どの異性のユーザーが自分のプロフィールを閲覧したかを各ユーザーに表示したい場合、MySQL でそれらすべての閲覧を追跡する最良の方法は何でしょうか?

各ユーザーにはuserid、メインUsersテーブルからの一意の がありsexます。

各ユーザーに表示したユーザーを、最新のビューから最も古いビューの順に表示したいと考えています。

ユーザーがたまたま自分のプロフィールを表示した場合、ユーザー自身を表示したくないことは明らかです。

男性には自分を見た女の子だけを表示し、女の子には自分を見た男性だけを表示したいと考えています。

  1. それを行うために のテーブルをどのように設定しますProfileViewsか?
  2. どのインデックスを使用しますか?
  3. それらを表示した各ユーザーを表示するために必要なクエリは何でしょうか?
4

4 に答える 4

3

これは私があなたのために作る簡単な例です。

SQL:

CREATE TABLE user
(
    user_id BIGINT NOT NULL AUTO_INCREMENT,
    sex VARCHAR(10) NOT NULL,
    CONSTRAINT PK_USER PRIMARY KEY (user_id)
) ENGINE=INNODB;


CREATE TABLE profileview
(
    profileview_id BIGINT NOT NULL AUTO_INCREMENT,
    user_id BIGINT NOT NULL,
    visitor_user_id BIGINT NOT NULL,
    date_time DATETIME NOT NULL,
    CONSTRAINT PK_PROFILEVIEW PRIMARY KEY (profileview_id)
) ENGINE=INNODB;

ALTER TABLE profileview
ADD FOREIGN KEY FK_PROFILEVIEW_USER(user_id) 
REFERENCES user (user_id);

ALTER TABLE profileview
ADD FOREIGN KEY FK_PROFILEVIEW_VISITOR(visitor_user_id) 
REFERENCES user (user_id);

PHP:

これは、ユーザー プロファイル ページの簡単な例です - www.domain.com/profile.php?id=xxx。この時点で、ユーザーがサイトにログインするときにセッションで 2 つの変数を定義する必要があります: $_SESSION['user_id'] (int) / $_SESSION['user_logged'] (boolean)

<?php
if ($_GET && isset($_GET['id']){

    if(isset($_SESSION['user_id']){
       $profile_user_id = $_GET['id'];

       // the user that visits the profile has a session with his/her id on it.
       session_start();
       $visitor_user_id = $_SESSION['user_id'];
    } else {
       // if visitor specified an id but there is no session, redirect to login.
       header("location: login.php");
    }
} else {
    // if no id passed redirect to index
    header("location: index.php");
}
?>
<html>
<head>
<title>Your title</title>
</head>
<script src="scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
//here you will store the visit with jquery.
$(document).ready(function(){
  // store the values from php.
  var profile_user_id = <?php echo $profile_user_id ?>;
  var visitor_user_id = <?php echo $visitor_user_id ?>;

  // here, the user information goes to the visit.php file.
  $.post('visit.php' { profile_user_id:profile_user_id, visitor_user_id:visitor_user_id } );
});
</script>
<body>
Here print user information from a SQL select or something of the id passed in the GET.
</body>
</html>

次に、データを保存するためのvisit.phpファイル:

<?php
if ($_POST && isset($_POST['profile_user_id']) && isset($_POST['visitor_user_id'])) {

    session_start();

    // this will end the execution of the script if there is no session from user logged
    if ($_SESSION['user_logged'] != true) {
      exit();
    }

    // everything is ok, do the process:
    // functions.php contains your SQL connection, I suppose you know how to do it.
    include('../cgi-bin/functions.php');
    $link = dbconn();

    $profile_user_id = mysql_real_escape_string($_POST['profile_user_id']);
    $visitor_user_id = mysql_real_escape_string($_POST['visitor_user_id']); 

    // this will store the data in profileview including date and time if id's are not equal.
    if($profile_user_id != $visitor_user_id){
       $sql = "INSERT INTO profileview (user_id, visitor_user_id, date_time) VALUES ($profile_user_id, $visitor_user_id, NOW())";
       mysql_query($sql, $link);
    }
}
?>

おまけ: functions.phpが何をするかわからない場合は、ここにあります:

<?php

function dbconn() { 
if(!include_once('db.php')) { 
  die('Error include file.'); 
}

if (!$link = mysql_connect($db['hostname'],$db['username'],$db['password'])) { 
  die('Error connecting.'); 
}

if (!mysql_select_db($db['database'])) { 
  die('Error selecting.'); 
}

return $link; 
}
?>

上記のファイルにもこのファイルが必要です。ここで、データベースへの接続パラメーターを設定します。

db.php

<?php
  $db = array( 
   'hostname' => 'localhost', 
   'username' => 'root', 
   'password' => 'mysql', 
   'database' => 'mydb' 
  );
?>
  • ファイルコードcgi-binでわかるように、より良いプラクティスのために、これをホスティングのフォルダーに入れることをお勧めします。visit.php

ここで、 という別のファイルを作成し、 に従ってプロファイル ビューをvisitors.php?id=xxx実行します。この時点で、次のことができるようになります。select * fromuser_id

  • 情報を入手し、user_idそれが男性であれば(例えば)...

    • 訪問者を性別で選択し、女性の訪問者のみをリストするルールを実行します。
    • profileview テーブルに保存されている時間に従って訪問者を一覧表示します。
于 2012-04-24T16:56:41.303 に答える
1

プロフィールビュー:

profile
userwhoviewed
timestamp

プロファイル列にインデックスを付けます。

したがって、ユーザーがページを表示したときに、それがプロファイル所有者であるかどうかを確認し、プロファイル所有者の性別を取得し、閲覧者の性別を確認し、異なる場合は、閲覧者とタイムスタンプでテーブルを更新します。

結果をクエリするときは、ターゲット プロファイルに一致するすべての行を選択し、タイムスタンプの降順で並べ替え、繰り返してそれらのプロファイルへのリンクを構築します。

私は通常、これらのフィールドで INT データ型を使用し (行を小さく保ち、ルックアップを高速化します)、それらの UID を auto_increment 主キーとして生成するユーザー テーブルを用意します。これにより、性別や好みのフィールド、その他の補助的なユーザーデータも保持され、必要に応じてログイン名を簡単に変更できます.

しかし、あなたは同性愛者のユーザーを除外しています. それらをすべてログに記録し、ユーザーが好みに基づいてフィルタリングできるようにすることをお勧めします。:)

于 2012-04-24T16:09:32.183 に答える
1
UsersTable
UserID      Sex
1           Boy
2           Girl
3           Girl


UsersViewsTable
UserID      View    Unixtimestamp
1           2       342143243432
1           3       142143243432
2           3       242143243432
3           1       442143243432

ユーザープロファイルにアクセスするときは、これを使用します:

IF CurrentUserSex != UserProfileSex
    INSERT INTO UsersViewsTable (UserID, View, Unixtimestamp) VALUES (CurrentUser, CurrentProfileUserID, time)

さて、これをページで取得して、最後に見た異性を確認しますか?

SELECT * FROM UsersViewsTable LEFT JOIN UsersTable USING (UserID) WHERE Sex != CurrentUserSex GROUP BY View ORDER BY Unixtimestamp DESC

編集 :

IF CurrentUserSex != UserProfileSex {
    $Res = SELECT CurrentProfileUserID FROM UsersViewsTable WHERE UserID = CurrentUserID AND View = UserProfileID LIMIT 1

    if($Res['Count'] == 1){
        // Exist
        UPDATE UsersViewsTable SET Unixtimestamp = time WHERE UserID = CurrentUserID AND View = UserProfileID LIMIT 1
    } else {
        // Doesnt exist
        INSERT INTO UsersViewsTable (UserID, View, Unixtimestamp) VALUES (CurrentUser, CurrentProfileUserID, time)
    }

}
于 2012-04-24T16:44:54.563 に答える
0

各ユーザープロファイルページのn比較を、訪問者IDとプロファイルIDで確認するだけです。2つが異なる場合は、日付と時刻、および必要な情報を含む訪問テーブルのストア。挿入する前に、prof id、vistor idがすでに存在するかどうかをテーブルの行で確認してから、時刻を更新してください。そうでない場合は、データを挿入してください。

ありがとう。

于 2012-04-24T16:40:31.973 に答える