Retrieving meta/title data from mysql and displaying on page selection.

I would be grateful for some help.

I have a very basic dynamic site, from a tutorial, the root directory is made up of the following

index.php

an inc folder that contains
home.php
contact.php
product.php
services.php
fetchmeta.php

Depending on selection through navbar (index calls in the selected page) I want to display the meta data and title related to that page. At the moment all I get is the homepage meta data displaying on all pages.

I have been trying to get this working for a week now with input from several people but I am still not managing to get it to work.

My script for retrieving data from the MySQL DB is

[php]

<?php include 'connect.php'; $page_id = 'meta_id'; $title = 'title'; $pname = 'name'; $desc = 'description'; $key = 'keywords'; $aut = 'author'; $copyr = 'copyright'; $email = 'email'; $rating = 'rating'; $robots = 'robots'; $visit = 'revisit'; $exp = 'expires'; $dist = 'distribution'; $sql = "SELECT * FROM `meta_data` WHERE `meta_id` = {$page_id}"; $query = $db->query($sql); $row = $query->fetch(PDO::FETCH_ASSOC); unset ($row['meta_id']) ; // discard that one foreach ($row as $desc => $content) { echo "\n" ; } echo "" . $title . "\n"; ?>

[/php]

index.php is

[php]

<?php require_once 'inc/header.php'; ?>
<?php $pages_dir = 'inc'; if(!empty($_GET['page'])){ $pages = scandir($pages_dir, 0); unset($pages[0], $pages[1]); $page = $_GET['page']; if (in_array($page.'.php', $pages)){ include $pages_dir.'/'.$page.'.php'; } else { echo "
Sorry that page does not exist.

You will be redirected in 5 seconds to the homepage."; header("refresh:5; index.php"); } } else { include ($pages_dir.'/home.php'); } ?>
<?php require_once 'inc/footer.php'; ?> [/php]

I do have this working in a static site using the same DB as well as putting <?php $page_id = # ?> on each page however when I came to try to insert into the dynamic site I have totally lost my way.

Thanks in advance

While I don’t know exactly what you’re trying to do, but I think the following might help you with the logic

I would build your HTML tag first something like the following

[PHP]
$output = “<meta”;

foreach ($row as $desc => $content) {

$output .= “name=”" . $desc . “” content="" . $content;

}
$output .= “” />" ;


// Then you can simply call it some like this maybe?

echo $output;

[/PHP]

Like I said I don’t know exactly what you are doing, but I do something similar to my navigational menu. I don’t know if I wrote the output a 100% correct. However, I hope you can see what I was trying to do.

I would just like to add that even the for loop I don’t think is right, but I think hopefully this will help.

When the site loads up it is on index.php, the nav buttons load content.php or home.php within the index.php page so mu urls are as follows

When the site first loads

http://localhost/dynamicmeta/dynamic3/index.php

If I select a page, say contacts the url is

http://localhost/dynamicmeta/dynamic3/index.php?page=contact

Unfortunately your example code did not work, the output was not valid.

Thanks for your reply.

The output I am getting from the code in my original post is

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="name" content="home" />
<meta name="title" content="Home Page" />
<meta name="description" content="description in top level detail" />
<meta name="keywords" content="keyword1, keyword2" />
<meta name="author" content="Author" />
<meta name="copyright" content="organisation , 2013" />
<meta name="email" content="organisations email" />
<meta name="rating" content="General" />
<meta name="robots" content="INDEX,FOLLOW" />
<meta name="revisit" content="7 Days" />
<meta name="expires" content="Tue, 24 dec 17:20:00 GMT" />
<meta name="distribution" content="Global" />
<title>Home Page</title>
<link href="inc/styles.css" rel="stylesheet" type="text/css" media="screen">
</head>

So as you see that part is working, where it breaks down is that I am only seeing the first record set from the DB no matter which page I select from my nav menu.

I realise this is due to my code and lack of knowledge in PHP but I cannot work out what I have not done to enable the pages to call the relevant meta info and title.

Sponsor our Newsletter | Privacy Policy | Terms of Service