PHP 中的多个标签或标签
Multiple Label or tag in PHP
我目前正在 PHP 创建博客,目前博客只能接受每个 post 的单个标签或标签。
请告诉我如何在 PHP 中为每个 post 创建或添加多个标签或标记。我希望 post 有多个标签。
谢谢
您使用的数据库类型非常重要。例如,在 MongoDB 这样的事情上这样做会容易得多,但是为了 cross-compatibility,这里有一个简单的平面方法来做到这一点。
选项 1:
这是一个每个人都会投反对票但我同意的快速技巧。
<?php
$tags = "tag1,tag2,tag3";
$exploded_tags = explode(",", $tags);
foreach( $exploded_tags as $elem ) {
echo $elem;
}
将它们存储为逗号分隔的标签,当您查询数据库中的标签时,展开它们。
选项 2:
您的 "blog post" 必须以某种方式在数据库中被识别,比如通过标题 "my-blog-post"。因此,您将有一个 table 称为 "blog_posts",另一个称为 "tags"。在 "tags" 中,您将有一个名为 "post_title" 的列。假设您要在 URL 中传递 post 的标题,您可以使用 get 获取该标题:
<?php
$post_title = $_GET['post']; // my-blog-post
$post_tags = array("tag1","tag2","tag3");
function insert_tags($title, $tags) {
$query = $database->prepare("INSERT INTO `tags` (`post_title`, `tags`) VALUES(?,?)" ); // this Query inserts the post title for each tag to identify
$query->bindValue(1, $title); // injects the title to the column post_title
$query->bindValue(2, $tags); // injects the tag to the column tags
try {
$query->execute(); // executes the above query
} catch (PDOException $e) {
echo $e->getMessage();
}
return 0; // please don't do parenthesis
}
foreach( $post_tags as $elems ) {
// since the tags are in an array, we need to loop through them
insert_tags($post_title, $elems);
// this function will now insert each individual tag with the title "my-blog-post"
}
代码中有PDO,不用PDO也可以适配mysqli什么的。现在,一旦您输入了您的标签,您就可以像这样调用它:
<?php
function find_tags($post_title) {
$query = $database->prepare("SELECT * FROM `tags` WHERE `post_title` = ?' ");
$query->bindValue(1, $post_title);
$query->execute(); // getting rid of exceptions for ease. This will execute the query
return $query->fetchAll(); // Because we are fetching 2 if not more rows, we need to tell PDO to fetch everything it found and return it to the function that called it
}
$tags = find_tags($_GET['post_title']); //my-post-title
foreach( $tags as $elems ) {
echo $elems;
}
这在大多数情况下应该有效。 Foreachs 可能有点矫枉过正,但这是一般的想法。如果您需要解释,请告诉我
我目前正在 PHP 创建博客,目前博客只能接受每个 post 的单个标签或标签。
请告诉我如何在 PHP 中为每个 post 创建或添加多个标签或标记。我希望 post 有多个标签。
谢谢
您使用的数据库类型非常重要。例如,在 MongoDB 这样的事情上这样做会容易得多,但是为了 cross-compatibility,这里有一个简单的平面方法来做到这一点。
选项 1: 这是一个每个人都会投反对票但我同意的快速技巧。
<?php
$tags = "tag1,tag2,tag3";
$exploded_tags = explode(",", $tags);
foreach( $exploded_tags as $elem ) {
echo $elem;
}
将它们存储为逗号分隔的标签,当您查询数据库中的标签时,展开它们。
选项 2:
您的 "blog post" 必须以某种方式在数据库中被识别,比如通过标题 "my-blog-post"。因此,您将有一个 table 称为 "blog_posts",另一个称为 "tags"。在 "tags" 中,您将有一个名为 "post_title" 的列。假设您要在 URL 中传递 post 的标题,您可以使用 get 获取该标题:
<?php
$post_title = $_GET['post']; // my-blog-post
$post_tags = array("tag1","tag2","tag3");
function insert_tags($title, $tags) {
$query = $database->prepare("INSERT INTO `tags` (`post_title`, `tags`) VALUES(?,?)" ); // this Query inserts the post title for each tag to identify
$query->bindValue(1, $title); // injects the title to the column post_title
$query->bindValue(2, $tags); // injects the tag to the column tags
try {
$query->execute(); // executes the above query
} catch (PDOException $e) {
echo $e->getMessage();
}
return 0; // please don't do parenthesis
}
foreach( $post_tags as $elems ) {
// since the tags are in an array, we need to loop through them
insert_tags($post_title, $elems);
// this function will now insert each individual tag with the title "my-blog-post"
}
代码中有PDO,不用PDO也可以适配mysqli什么的。现在,一旦您输入了您的标签,您就可以像这样调用它:
<?php
function find_tags($post_title) {
$query = $database->prepare("SELECT * FROM `tags` WHERE `post_title` = ?' ");
$query->bindValue(1, $post_title);
$query->execute(); // getting rid of exceptions for ease. This will execute the query
return $query->fetchAll(); // Because we are fetching 2 if not more rows, we need to tell PDO to fetch everything it found and return it to the function that called it
}
$tags = find_tags($_GET['post_title']); //my-post-title
foreach( $tags as $elems ) {
echo $elems;
}
这在大多数情况下应该有效。 Foreachs 可能有点矫枉过正,但这是一般的想法。如果您需要解释,请告诉我