user520476
user520476

Reputation:

PHP Tag system without database (plain text files)

I want to implement a tag system on my website. The website is made in PHP, but uses NO database (sql) system. It reads the files from plain text files and includes them.

The pages are in a file, if a page is requested that file is read, and if the page is in there the site returns it. If the page is not in there it gives an error (so no path traversal issues, I can let page "blablabla" go to "other-page.inc.php").

The page list is a big case statement, like this:

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

($s_pageype is for the css theme).

What I want is something like this:

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;

And a tag page which takes a tag as get variable, checks which cases have that tag in the $s_tag array and then returns those cases.

Is this possible, or am I thinking in the wrong direction?

Upvotes: 3

Views: 586

Answers (2)

kaese
kaese

Reputation: 10599

It's possible, but you may need to think outside your current structure.

Something like this will work:

$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

Upvotes: 0

John Lawrence
John Lawrence

Reputation: 2923

I would do this by keeping your page details in an array such as:

$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");

You could then use a foreach loop to go through this array and pull out the items with matching tags:

$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'];
    }   
}

Upvotes: 1

Related Questions