だから私はtwitterのようなユーザータグ付けシステムに取り組んでいます。このシステムでは、「blablabla @username blablabla」のようにコメントでタグ付けすることができます。私はpreg_replaceを使用して、テキスト内の "@username"をクリック可能なリンクに変換し、データベースに挿入するときはpreg_match_allを使用しています。foregループでpreg_match_all結果を照会する方法は?
$text = preg_replace('/(^|\s)@(\w+)/', '$1<a href="profile.php?poet_id=$2" class="small">@$2</a>', $text);
そして
$sanitized_comment = trim(mysql_real_escape_string(htmlspecialchars($_POST['comment'])));
if (preg_match_all("/(^|\s)@(\w+)/", $sanitized_comment, $matches)) {
$mentions = array_unique($matches[0]);
foreach($mentions as $mention) {
mysql_query("INSERT INTO `comments_mentions` SET `cm_comment_id` = @last_comment_id, `cm_mentioned` = '" . $mention . "', `cm_mentioner` = " . (int)$session_user_id . "");
}
}
これが正常に動作します。
私が今しようとしているのは、自分の「ユーザー名」ではなく、テキストにタグが付けられているユーザーの「user_id」を見つけて保存することです。
私は
$sanitized_comment = trim(mysql_real_escape_string(htmlspecialchars($_POST['comment'])));
if (preg_match_all("/(^|\s)@(\w+)/", $sanitized_comment, $matches)) {
$mentions = array_unique($matches[0]);
foreach($mentions as $mention) {
$user_id_from_username = mysql_fetch_assoc(mysql_query("SELECT `user_id` FROM `users` WHERE lower(`username`) = lower(substr('" . $mention . "', 2))"));
mysql_query("INSERT INTO `comments_mentions` SET `cm_comment_id` = @last_comment_id, `cm_mentioned` = " . $user_id_from_username . ", `cm_mentioner` = " . (int)$session_user_id . "");
}
}
をやって試してみました。しかし、これは唯一のいくつかの人がタグ付けされた場合でも、データベースに1行(最初の値)を挿入します。
上記のユーザーの「ユーザー名」を適切に照会し、データベースに複数のユーザーがタグ付けされている場合は複数の行にこれらのIDを格納するにはどうすればよいですか。
あなたが探しているJOINではありませんか?さらに、mysql_関数は古くから使用されなくなりました。新しいmysqli_関数や 'PDO'の使用を検討してください。 – Jan
** deprecatedを使用して停止し、PHP7で削除された** mysql_ *関数を削除してください。 PDOに移行し、Prepared Statementの使用を開始します。 –
あなたの返信ありがとう!私はこれを調べます。 – kimnthorstensen