Hashtags without database

Probably it is not possible, but I’m wondering if I could get keywords as hashatgs without a database.
Getting linked keywords is very easy:

<?php 
echo "<p class=\"flexcenter\">";
foreach (explode(',', $keywords) as $key) 
{echo "<span><a href=\"hashtag.php?tag=$key\">#{$key}</a></span>";}
echo "</p>";
?>

But the file hashtag.php could be without reference to any mysql database?

Hey @web148 I think you need to explain the context of your question. With or without database it isn’t clear what you are trying to do.

Are you talking about the key (index) of the array?

Yes, my question was too short. The fact is that I am websites with php pages but I’m not using the database.
However I have $keywords variable in any page, and I noticed that with my above code I can get linked keywords: but pointing where?
So far pointing … to nothing :slight_smile:

Well you can include a config that contains the data and use that on different pages. You do not need a database

1 Like

Thank you. Could you explain my how (or give me some links)?

$keywords = ['keyword1', 'keyword2', 'keyword3'];
echo "<p class=\"flexcenter\">";
foreach ($keywords as $key => $keyword ) {
    echo "<span><a href=\"hashtag.php?tag=$keyword\">#{$keyword}</a></span>";
}
echo "</p>";

I do not really know why you use an explode, but you can do the same for that.

$keywords = 'keyword1, keyword2, keyword3';
echo "<p class=\"flexcenter\">";

$keywords = explode(', ', $keywords);
foreach ($keywords as $key => $keyword ) {
    echo "<span><a href=\"hashtag.php?tag=$keyword\">#{$keyword}</a></span>";
}
echo "</p>";

1 Like

but what should be the content of hashtag.php?

Well you could split it up like this (did not test the code :grin:):

<?php

// some class for getting the keywords (in another file call it keywords.php)
class keywordClass
{
    private const KEYWORDS = ['keyword1', 'keyword2', 'keyword3'];
    
    public static function getKeywords(): array
    {
        return self::KEYWORDS;
    }

}

// In your keyword-export.php file you add this
include 'keywords.php';
$keywords = keywordClass::getKeywords();

echo "<p class=\"flexcenter\">";
foreach ($keywords as $key => $keyword ) {
    echo "<span><a href=\"hashtag.php?tag=$keyword\">#{$keyword}</a></span>";
}
echo "</p>";
1 Like

And if you want to do it OOP, you can use it like this:


<?php

class keywords
{
    private const KEYWORDS = ['keyword1', 'keyword2', 'keyword3'];

    public static function getKeywords()
    {
        return self::KEYWORDS;
    }
}


class keywordGeneratorToHTML
{
    private const START_VALUE = "<p class=\"flexcenter\">";
    private const STOP_VALUE = "</p>";

    public static function buildKeywords($keywordArray): string
    {
        $keywordData = '';
        foreach ($keywordArray as $key => $keyword) {
            $keywordData+= "<span><a href=\"hashtag.php?tag=$keyword\">#{$keyword}</a></span>";
        }

        return self::START_VALUE . $keywordData . self::STOP_VALUE;
    }
}

$keywords = keywords::getKeywords();

echo keywordGeneratorToHTML::buildKeywords($keywords);

?>


The only thing is that you need to include the php file, or use composer like the rest of our poor souls

1 Like

Thank you, but I still don’t understand.
So far I don’t have in my websites a file hashtag.php. I have it in my localhost webpages to search for example books of a given writer, but always using a mysql connection.
That’s why I asked what should I put in an hashtag.php file not connected with a database, if it is possible (what I’m not sure).
The result I wish is to get a list of all the php files having the keyword selected, and the keywords are stored not in a database but in php files as a variable ($keywords).

Are your books and writers stored in your database?

1 Like

Yes they do, “always using a mysql connection”.

Where is $keywords coming from?

1 Like

as I said it is a variable within a (almost any) php file, such as

<?php
[many other variables in the head of a php file]
$keywords = "Science,Newton,Galileo,Einstein";
?>

That way it won’t work. You’d need to separate your data from your display somehow. Then you can access the data from multiple places.

As a small example you’d have one file with a giant array that contains all data like so:

<?php

return [
    'key' => [
        'url' => '/foo',
        'keywords' => 'keyword1,keyword2,keyword3',
        'title' => 'Some title',
        'content' => [
            'pargraph1', 'paragraph2', 'etc'
        ]
     ]
];

Let’s call that content.php.

And then in hashtag.php you can loop over the content and get all articles with the asked hashtag

<?php

$hashtag = $_GET['hashtag'];
// throw exception when empty or missing

$content = require __DIR__ . '/content.php';

// get only the articles that have $hashtag as one of the keywords
$articles = array_filter(
    $data,
    function (array $article) use ($hashtag) {
        return in_array($hashtag, explode(',', $article['keywords']));
    }
}

foreach ($articles as $article) {
    printf('<a href="%s">%s</a>', $article['url'], $article['title']);
}
2 Likes

Sorry, but I didn’t manage to use your code.
It seems that it is a code for one file at once, so to say, isn’t it?
Or, at least it seems require that I re-write all my php files, doesn’t it?
Maybe what I ask is not (yet) possible …
Thank you!

That is correct.

1 Like

Therefore, at the moment, it require too time for me. Sorry…
Thank you any way for your kindness :slight_smile:

2 Likes

It seems the “#hashtags” need to correlate to the actual pages already defined with a URL link.
So I would define your $keywords as an array with #hash as the KEY and the URL as the VALUE. Something like this.

$keywords = array('keyword1' => 'pagekeyword1.php', 'keyword2' => 'pagekeyword2.php', 'keyword3' => 'pagekeyword3.php');

Then your links would use both the HASH and the URL so they become links to your actual pages.

//Then your links would use both the HASH and the URL
echo '<p class="flexcenter">'."\r";	
foreach ($keywords as $hash => $url): 
	echo '<span><a href="'.$url.'">#'.$hash.'</a></span>'."\r";	
endforeach;
echo '</p>'."\r"

This code results to:

<p class="flexcenter">
<span><a href="pagekeyword1.php">#keyword1</a></span>
<span><a href="pagekeyword2.php">#keyword2</a></span>
<span><a href="pagekeyword3.php">#keyword3</a></span>
</p>

Displayed as:

#keyword1 #keyword2 #keyword3 

Now if all your pages already have this keyword list and they are all pointing to hashtag.php, what does hashtag.php do or have as far as coding? If you don’t have the page or it isn’t doing anything, then make a page which will redirect the user to the selected page.
Start by making the keyword array that defines all URL pages with the HASH TAG keys.

$keywords = array('keyword1' => 'pagekeyword1.php', 'keyword2' => 'pagekeyword2.php', 'keyword3' => 'pagekeyword3.php', 'keyword4' => 'pagekeyword4.php', 'keyword5' => 'pagekeyword5.php'); 
//etc

You would then be looking for Request Method GET and not empty. Then define the URL based on if $_GET['tag'] is a KEY in the $keywords array. If it is a KEY in the array use the array[key] => value, i.e. $keywords[$_GET['tag']] to get the URL value and use a header() tag to redirect the user to the webpage or send them to a default page. something like this.

<?php
$keywords = array('keyword1' => 'pagekeyword1.php', 'keyword2' => 'pagekeyword2.php', 'keyword3' => 'pagekeyword3.php', 'keyword4' => 'pagekeyword4.php', 'keyword5' => 'pagekeyword5.php'); 

if($_SERVER["REQUEST_METHOD"] == "GET" && !empty($_GET['tag'])):
	$url = (!empty($_GET['tag']) && array_key_exists($_GET['tag'],$keywords) ? $keywords[$_GET['tag']] : 'defaulturl.php');	
	header("location: ".$url);
	exit;
endif;
?>
2 Likes