Tell me how I can effectively save the server from a lot of unnecessary traffic and limit data processing?

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.

Code for any page should be laid out in this general order -

  1. initialization
  2. post method form processing
  3. get method business logic - get/produce data needed to display the page
  4. html document

Section #3 is where you would query for and fetch the data needed for the html document. By separating the database specific code, that knows how to query for and fetch the data, from the presentation code, that knows how to produce the output from the data, it makes it easier to design, write, test, debug, and maintain the code, or to switch to a different data source should your application need to get data from a remote api.

Ok, I’ll keep that in mind :+1:

You might also looking into doing pagination and it really isn’t that hard to setup especially if you follow phdr advise.

Here’s an example of a query →

sql = 'SELECT * FROM cms WHERE page =:page AND category =:category ORDER BY id DESC, date_added DESC LIMIT :perPage OFFSET :blogOffset';

and you can even do it using JavaScript/Fetch (I use Vanilla JavaScript) which the user won’t even notice as the pages don’t reload. An example webpage of mine - https://www.phototechguru.com/ the gallery is a javascript example → Photo Gallery

Sponsor our Newsletter | Privacy Policy | Terms of Service