没有数据库的PHP标签系统(纯文本文件)


PHP Tag system without database (plain text files)

我想在我的网站上实现一个标签系统。该网站是用PHP制作的,但没有使用数据库(sql)系统。它从纯文本文件中读取文件并包含它们。

页面位于文件中,如果请求页面,

则读取该文件,如果页面位于其中,则站点返回该文件。如果页面不在那里,它会给出错误(所以没有路径遍历问题,我可以让页面"blablabla"转到"other-page.inc.php")。

页面列表是一个大案例陈述,如下所示:

case "faq":
$s_inc_page= $s_contentdir .  "static/faq.php";
$s_pagetitle="FAQ";
$s_pagetype="none";
break;

($s_pageype 用于 CSS 主题)。

我想要的是这样的东西:

case "article-about-cars":
$s_inc_page= $s_contentdir .  "article/vehicles/about-cars.php";
$s_pagetitle="Article about Cars";
$s_pagetype="article";
$s_tags=array("car","mercedes","volvo","gmc");
break;

一个将标签作为 get 变量的标签页面,检查哪些案例在 $s_tag 数组中具有该标记,然后返回这些案例。

这是可能的,还是我想错了方向?

我会将您的页面详细信息保存在数组中,例如:

$pages['faq']['s_inc_page'] = $s_contentdir .  "static/faq.php";
$pages['faq']['s_pagetitle'] = "FAQ";
$pages['faq']['s_pagetype'] = "none";
$pages['faq']['s_tags'] = array("car","mercedes","volvo","gmc");

然后,您可以使用 foreach 循环遍历此数组并提取具有匹配标记的项目:

$tag = "car";
foreach($pages as $page) {
    if (in_array($tag, $page['s_tags'])) {
        //do whatever you want to do with the matches
        echo $page['s_pagetitle'];
    }   
}

这是可能的,但您可能需要跳出当前的结构进行思考。

这样的东西会起作用:

$pages = array(
    "article-about-cars" => array ("car", "mercedes", "volvo"),
    "article-about-planes" => array ("757", "747", "737")
); //an array containing page names and tags
foreach ($pages as $key => $value) {
   if (in_array($_GET['tag'], $value)) {
       $found_pages[] =  $key;
   }
}
return $found_pages; //returns an array of pages that include the tag