回答
您使用的数据库类型非常重要。例如,对于像MongoDB这样的事情来说,这样做会容易得多,但为了交叉兼容性,这是一个简单的平坦方法。
选项1: 这里是一个快速入侵,每个人都会倒下,但我很好。
<?php
$tags = "tag1,tag2,tag3";
$exploded_tags = explode(",", $tags);
foreach($exploded_tags as $elem) {
echo $elem;
}
将它们存储为以逗号分隔的标记,并且当您为标记查询数据库时,将它们分解。
选项2:
你的“博客文章”必须以某种方式对数据库的标识,通过标题为“我 - 博客 - 后”之称。所以你会有一个名为“blog_posts”的表,另一个名为“tags”。在“标签”中,您将有一个名为“post_title”的列。让我们说你是通过你的网址中的文章的标题,你可以使用去抓住那个标题:
<?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;
}
此,在大多数情况下,应该工作。 Foreach可能有点矫枉过正,但这是总体思路。让我知道你是否需要解释
非常感谢你提供的这个选项,我一定会试试这个,一旦得到结果就回来。再次感谢您 –
它的工作!有效!有效!有效!有效!有效!有效! 选项1做到了一切! :D 非常感谢代码,我也能够在每个标签上添加链接。非常感谢,案件关闭。 :) –
- 1. 标签或标签?
- 2. 多个标签
- 3. 多个标签
- 4. Sonar Checkstyle标签列出多个标签?
- 5. Matplotlib中的多个标签
- 6. 附加标签或多个标签XML :: COMPILE :: SCHEMA
- 7. 多标签和多标签数据标签的评估
- 8. 多种多标签与多标签
- 9. 标签一个git用“标签”标签
- 10. VB.NET标签的前面或多个GroupBoxes
- 11. 一个或多个元标签?
- 12. Facebook多个标签
- 13. 在多个标签
- 14. CouchDB多个标签
- 15. 多个h1标签?
- 16. 由多个标签
- 17. 多个标签,多个
- 18. 如何用Javascript中的视频标签替换音频标签或用另一个标签替换标签
- 19. “For”中的标签标签
- 20. 标签页中间的多个面板标签
- 21. 如何在Beautifulsoup中的新标签下包装多个标签?
- 22. Jekyll标签有空格或多个词
- 23. img标签内的标签或给背景图片标签哪一个更好
- 24. 多个标签和php上传
- 25. PHP/MySQL - 如何添加多个标签
- 26. UIImageView上的多个标签?
- 27. 标签上的多个AssociatedControlID
- 28. 的SimpleXML与多个标签
- 29. JQuery的多个标签组
- 30. PHP,preg_replace,用标签替换标签attr
简单,例如使用一个表格:ID这样blog_post_id标签可以为一个帖子保留多个标签 – jewelhuq
标记为太宽泛。 http://stackoverflow.com/tour – DeDee
认为多对多的关系。通过链接表 – nomistic