I am on my way to program my first tiny mini CMS in 15 years, and got a few questions.
This applies to extracting data from the database and displaying it on the page, and how this is done today VS how it was done 15 - 20 years ago. A lot has happened in that time, and PHP has developed enormously. The system I am setting up is a simplified version of a CMS/SPA. I could have done this the simple way like in the old days and opened the tap on full and extracted all the data at once by using a SELECT* This entails a lot of extra traffic on the server, and is probably not recommended anymore. It probably never was, but in the old days it was an easy solution. Here’s how this is intended to work:
The page is built around header.php, main.php and footer.php, which are included with required_once in index.php.
It is main.php that will be the part of the system that will display and generate the content from the database, this is where the part about a Single Page Application comes in, but very simplified.
What I am trying to explain from now on is an example.
It is header.php that will contain everything that is needed on other pages, so all the PHP that should be at the top before the tag and what is between the and tag, for example. metadata etc.
I am pasting some code below so you can see how I think:
This should be inserted in main.php:
$stmt = $pdo->prepare(‘SELECT title_tag
, title
, content
FROM articles
WHERE id
= :id’);
$stmt->execute([‘id’ => $id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$title = $row[‘title’];
$content = $row[‘content’];
Here I have only inserted title_tag, title and content. the difference between title_tag and title is that title_tag is for the title in the title tag, and title is the heading inside the page/content that is retrieved from the DB. So to the question that brings me a little deeper into the system. How to do it with e.g. title_tag. This should change as the pages and the content of the pages change. I refer to the code block above, and I insert this “<?php echo $title_tag; ?>” between and in the head in header.php, but this will ensure that all pages have the same title. What is the best way to solve this? Before then, I had set the faucet on full. This had resulted in a large amount of unnecessary data traffic and data processing.