I have a php site with few pages. And i have a common header file for all of the pages. Also every page contains values from database.
Like page product.php shows a product list and page category.php shows list of categories. I can set the page title for product.php to “Products” but when i click on some product the url becomes product.php?id=3 (example)
So i want that product name listed under row id 3 of mysql database as title of the page.
I have different table for every page like for products the table name is product. For categories table name is category.
Please help me out how can i set dynamic titles and meta description of the page.
just set them as variables
// product.php
$product = $db->sometStuffToGetProductById($_GET['id']);
$header = $product->title;
$meta = [
'image' => $product->image,
'description' => $product->text
];
require_once 'header.php';
// etc...
// header.php
<?php
$title = isset($title) ? $title : 'Default title';
$meta['description'] = isset($meta['description']) ? $meta['description'] : 'Default description';
$meta['image'] = isset($meta['image']) ? $meta['image'] : 'https://yoursite.com/images/default.jpg';
?>
<html>
<head>
<title><?= $title?><title>
<meta name="description" content="Yourwebsite.com - <?= $meta['description'] ?>">
<meta property="og:title" content="Yourwebsite.com - <?= $title?>">
<meta property="og:description" content="Yourwebsite.com - <?= $meta['description'] ?>">
<meta property="og:image" content="<?= $meta['image'] ?>">