In fact, I add those tags that I wrote above. But the problem is that my site is one-page (there is only index.php , and the rest of the pages are opened on the principle "$page = $_GET['page']" ).

So far there are 6 categories of goods on the site, each of which is recorded in a database. I would like each category to have its own title , description and keywords . But, unfortunately, each category does not have separate pages, such as categry_1.php , category_2.php . They are invoked using the include() function in index.php in the body block. And those tags can only be written in the head block.

Related to this question: is there a solution to this problem or not? I hope my problem is clear. Thank you in advance.

  • one
    create additional variables in the script and fill them in depending on $page = $_GET['page'] , and output them in the right place) - Ruslan
  • "there is only index.php" - this is called Front Controller ( design-pattern.ru/patterns/front-controller.html ), and not SPA - Yuriy Prokopets

2 answers 2

You can always do something like this:

 <?php $availablePages = [ "cat1" => [ "title" => "Title Cat 1", "header" => "Header Cat 1", "description" => "Description Cat 1", "keywords" => ['a', 'b', 'c'], "file" => "path/to/file/to/include.1.php" ], "cat2" => [ "title" => "Title Cat 2", "header" => "Header Cat 2", "description" => "Description Cat 2", "keywords" => ['d', 'e', 'f'], "file" => "path/to/file/to/include.2.php" ] ]; $page = $_GET["page"]; if (array_key_exists($page, $availablePages)) { $catInfo = $availablePages[$page]; include($catInfo['file']); echo $catInfo['title'] . "</br>"; echo $catInfo['description'] . "</br>"; echo $catInfo['header'] . "</br>"; echo implode(", ", $catInfo['keywords']) . "</br>"; } else { header("HTTP/1.0 404 Not Found"); echo "Wrong Category"; } 

In essence, an array is a whitelist that stores known values.

    You need to use the MVC pattern (separation of logic and display). At the same time begin to generate content after the logic runs. If simple, then do not use echo to generate the page, but save everything to a variable, and make a conclusion at the end. In order not to reinvent the wheel yourself, you can use ready-made solutions, for example, integrate the template engine into your script ( https://twig.symfony.com/ ) or use some simple framework ( https://silex.symfony.com/ )