homepage Welcome to WebmasterWorld Guest from 50.19.172.0
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Newbie Question - Php, Mysql, Meta Tags and retrieving the data
dynamic meta information from mysql to appropriate pages
Skorpios



 
Msg#: 4552688 posted 6:53 pm on Mar 8, 2013 (gmt 0)

I started what should have been a fairly basic exercise yesterday. I have a mysql DB set up, I have the connection script and I have a SELECT query. With the help from someone else I have got to the stage where the data for the fields is being delivered to the page however it is not dynamic. Meaning I am only getting 1 record.

My Code so far


include 'connect.php';

$id = 'meta_id';
$title = 'page_title';
$pname = 'page_name';
$desc = 'meta_description';
$key = 'meta_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 = 1 LIMIT 1";
$query = $db->query($sql);

$row = $query->fetch(PDO::FETCH_ASSOC);

unset ($row['meta_id']) ; // discard that one

foreach ($row as $desc => $content) {
echo "<meta name=\"$desc\" content=\"$content\" />\n" ;
}


Now I need to get the number in the select statement to be dynamic so that it chooses the right record from the DB according to what page is being displayed.

My code for the home page is


<!DOCTYPE HTML>
<html>
<head>
<link rel="apple-touch-icon" href="apple_touch_icon.png"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php include_once('../includes/fetchmetadata.php'); ?>
<link rel="shortcut icon" href="favicon.ico" type="image/icon" /> <!-- favicon for browser, the image is .ico file in root directory-->
<title></title>
</head>

<body>

</body>
</html>


I just want to get this working before putting any effort into the site itself.

I have been advised that I should

You would typically use the URL query string to specify the id, then use $_GET to get it.


Although that may give me an answer, I do not know how to do this, can anyone help please as although this in itself is not of major importance once I have it working it will help me learn a little about php.

Thanks

 

Skorpios



 
Msg#: 4552688 posted 6:54 pm on Mar 8, 2013 (gmt 0)

My pages currently are only

home
products
services
contact.php

Skorpios



 
Msg#: 4552688 posted 9:22 pm on Mar 8, 2013 (gmt 0)

With some help I know have the basics working, not exactly as I would like though.

How can I improve on the following code to make it more efficient and less chance of mistakes being made?


// connect.php

<?php

$config['db'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => 'pass',
'dbname' => 'database'
);

$db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);

?>


The connection is working fine.


// getmetadata.php

<?php

include 'connect.php';

$id = 'meta_id';
$title = 'page_title';
$pname = 'page_name';
$desc = 'meta_description';
$key = 'meta_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} LIMIT 1";

$query = $db->query($sql);

$row = $query->fetch(PDO::FETCH_ASSOC);

unset ($row['meta_id']) ; // discard that one

foreach ($row as $desc => $content) {
echo "<meta name=\"$desc\" content=\"$content\" />\n" ;
}
echo "<title>";
echo $row[$title];
echo "</title>\n";
?>


The title is working fine however the meta data from the foreach loop is adding a couple of extra fields.

The web pages are at their basic level, all are laid out the same and as follows


<?PHP
//### Set the page ID to retrieve data from database
$page_id = 4;
?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="apple-touch-icon" href="apple_touch_icon.png"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php include_once('../includes/fetchmetadata.php'); ?>
<link rel="shortcut icon" href="favicon.ico" type="image/icon" /> <!-- favicon for browser, the image is .ico file in root directory-->

</head>

<body>
<p><a href="contact.php">contact</a></p>
<p><a href="products.php">products</a></p>
<p><a href="services.php">services</a></p>
<p><a href="home.php">home</a></p>
</body>
</html>


The output I get in all the pages is correct however there would be a risk of error in the php at the top of each page
declaring the $page_id = 4; for example.

Can someone suggest a more efficient way of doing this, please keep in mind that I do not have much knowledge of php .

Thanks

Skorpios



 
Msg#: 4552688 posted 9:25 pm on Mar 8, 2013 (gmt 0)

The data returned when looking at the source is as follows


<!DOCTYPE HTML>
<html>
<head>
<link rel="apple-touch-icon" href="apple_touch_icon.png"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="page_name" content="services" /> <!-- dont want this field -->
<meta name="page_title" content="Our Services" /> <!-- dont want this field -->
<meta name="meta_description" content="introduction to our services offered at competitive prices" />
<meta name="meta_keywords" content="training, tuition, onsite, offsite" />
<meta name="author" content="author" />
<meta name="copyright" content="&copy; 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="no data in this field at the moment" />
<meta name="distribution" content="Global" />
<title>Our Services</title>
<link rel="shortcut icon" href="favicon.ico" type="image/icon" /> <!-- favicon for browser, the image is .ico file in root directory-->

</head>

<body>
<p><a href="contact.php">contact</a></p>
<p><a href="products.php">products</a></p>
<p><a href="services.php">services</a></p>
<p><a href="home.php">home</a></p>
</body>
</html>

Skorpios



 
Msg#: 4552688 posted 12:34 am on Mar 9, 2013 (gmt 0)

And the main question is how do I implement this code in a dynamic site rather than a static one?

swa66

WebmasterWorld Senior Member swa66 us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4552688 posted 9:55 am on Mar 9, 2013 (gmt 0)

how do I implement this code in a dynamic site rather than a static one?

Have you ever made a "dynamic site" ?

The reason I ask: once you can dynamically determine the content to be shown based on the URL asked by the visitor, it becomes quite trivial to also change the meta tags.

Skorpios



 
Msg#: 4552688 posted 11:30 am on Mar 9, 2013 (gmt 0)

I have by following a tutorial but what I am trying to do was not covered, hence the question. The site is using ajax/js but tying the two together so that I have a head.php file that changes the meta and title dynamically is not something I have done b4

Skorpios



 
Msg#: 4552688 posted 11:43 am on Mar 9, 2013 (gmt 0)

The dynamic site I have so far is again just basic but the code is


<!-- index.php -->

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>website</title>
<link href="css/screen/screen.css" rel="stylesheet" type="text/css" media="screen">
</head>

<body>
<ul id="nav">
<li><a href="home">Home</a></li>
<li><a href="subjects">Subjects</a></li>
<li><a href="downloads">Downloads</a></li>
<li><a href="about">About</a></li>
<li><a href="contact">Contact</a></li>
</ul>

<div id="content">
<script src="js/jquery.js"></script>
<script src="js/general.js"></script>
</div> <!-- end of content div -->
</body>
</html>



// JavaScript general.js

//alert ('working'); //test

// loading content

// initial page loading
$(document).ready(function() {
$('#content').load('content/home.php');

// handle menu clicks
$('ul#nav li a').click(function() {
//alert('OK'); // test
var page = $(this).attr('href');
//alert(page); // test
$('#content').load('content/' + page + '.php');
return false;
});
});



I also have the jquery file saved.

And currently all my pages are as follows, this is the default home


<h1>Home Page</h1>
<br />
<p>This is the home page</p>


The site works as expected but now I am trying to get the meta information from a static site/mysql DB to display when the corresponding page is selected/displayed.

Thanks

swa66

WebmasterWorld Senior Member swa66 us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4552688 posted 11:59 am on Mar 9, 2013 (gmt 0)

So if I got it right, you generate a piece of html upon a page hit and serve up some relatively static html and javascript. That javascript then changes the page using ajax techniques to interact with the visitors.
The ajax would also do "subrequests" to the server to fetch more details as needed (transferred as html).

to me it looks like you're essentially asking how to change the relatively static html depending on what the user will interact with in ajax in the future. Obviously, it can't know ... All it knows is the URL and any parameters passed there, where the visitor claims to come from (referrer), potentially a session or so.

Since the meta tags are in fact part of the DOM, you could change them using javascript (ajax), mostly like you load the content.

But to be honest: why bother with the meta tags?

Nobody reads meta tags. Set the important ones (encoding, expiration, caching, indexing, ...) .
The rest is at best for SEO. Yet, you're using ajax to fetch the content.

Also: why bother with the ajax ? you could just as easily serve this content without the use of ajax at all.
Moreover I've some doubts about fetching html pieces using ajax. To me it's more a way of e.g. letting users interact with databases etc. allowing more flexibility.

Edit: saw your last post in the mean time.

Skorpios



 
Msg#: 4552688 posted 12:07 pm on Mar 9, 2013 (gmt 0)

So now I have just carved it up into head.php, nav.php etc etc so now the index file is


<?php include"includes/head.php"; ?>

<body>
<?php include"includes/nav.php"; ?>

<?php include "includes/content.php"; ?>
</body>
</html>

Skorpios



 
Msg#: 4552688 posted 12:13 pm on Mar 9, 2013 (gmt 0)

Although meta tags are in the main not used it is something that I want to do, purely for that reason. Although for you there maybe floors in the dynamic site in my thinking if I can get the data from the database into the relevant pages upon end user selection in this then I can also implement it on other dynamic sites that use different methods of swapping the content on user selection.

Skorpios



 
Msg#: 4552688 posted 10:45 pm on Mar 9, 2013 (gmt 0)

The other option I have to hand is the following dynamic site


// index.php

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Website</title>
<link href="inc/styles.css" rel="stylesheet" type="text/css" media="screen">
</head>

<body>

<div id="header"><h1>Dynamic Site</h1></div>

<div id="menu">
<a href="index.php?page=home">Home</a>
<a href="index.php?page=contact">Contact</a>
<a href="index.php?page=about">about</a>
<a href="index.php?page=tutorials">tutorials</a>
</div>

<div id="content">
<?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.<br>You will be redirected in 5 seconds to the homepage.";
// need 5 second timer b4 page changes
header("refresh:5; index.php?page=home");
}
} else {
include ($pages_dir.'/home.php');
}
?>
</div>

</body>
</html>



And then all the content is laid out as


//home.php content.php products.php in the inc folder

Welcome to the home page etc etc according to the page name


I would assume that this structure would make the job easier but still I am unsure how to construct the php to swap the meta and title information out according to the users choice, can anyone help?

Thanks

Skorpios



 
Msg#: 4552688 posted 12:35 pm on Mar 10, 2013 (gmt 0)

Whilst researching I have come across a script that may resolve my problem, and others but would like some advice as it seems very long winded.

The script is


<?php

if ($_SERVER['REQUEST_URI'] == "/home.php" || $_SERVER['REQUEST_URI'] == "/index.php") {
echo "<meta name=\"title\" content=\"title content goes here, called from DB!\">\n";
echo "<meta name=\"keywords\" content=\"keyword content goes here, called from DB!\">\n";
echo "<meta name=\"description\" content=\"description content goes here, called from DB!\">\n\n";
echo "<title>page title goes here, called from DB!</title>\n\n";
} elseif ($_SERVER['REQUEST_URI'] == "/contact.php" || $_SERVER['REQUEST_URI'] == "/contact.php") {
echo "<meta name=\"title\" content=\"title content goes here, called from DB!\">\n";
echo "<meta name=\"keywords\" content=\"keyword content goes here, called from DB!\">\n";
echo "<meta name=\"description\" content=\"description content goes here, called from DB!\">\n\n";
echo "<title>page title goes here, called from DB!</title>\n\n";
} elseif ($_SERVER['REQUEST_URI'] == "/products.php" || $_SERVER['REQUEST_URI'] == "/products.php") {
echo "<meta name=\"title\" content=\"title content goes here, called from DB!\">\n";
echo "<meta name=\"keywords\" content=\"keyword content goes here, called from DB!\">\n";
echo "<meta name=\"description\" content=\"description content goes here, called from DB!\">\n\n";
echo "<title>page title goes here, called from DB!</title>\n\n";
} elseif ($_SERVER['REQUEST_URI'] == "/services.php" || $_SERVER['REQUEST_URI'] == "/services.php") {
echo "<meta name=\"title\" content=\"title content goes here, called from DB!\">\n";
echo "<meta name=\"keywords\" content=\"keyword content goes here, called from DB!\">\n";
echo "<meta name=\"description\" content=\"description content goes here, called from DB!\">\n\n";
echo "<title>page title goes here, called from DB!</title>\n\n";
} elseif ($_SERVER['REQUEST_URI'] == "/about.php") {
echo "<meta name=\"title\" content=\"title content goes here, called from DB!\">\n";
echo "<meta name=\"keywords\" content=\"keyword content goes here, called from DB!\">\n";
echo "<meta name=\"description\" content=\"description content goes here, called from DB!\">\n\n";
echo "<title>page title goes here, called from DB!</title>\n\n";
} else {
echo "<meta name=\"title\" content=\"title content goes here, called from DB!\">\n";
echo "<meta name=\"keywords\" content=\"keyword content goes here, called from DB!\">\n";
echo "<meta name=\"description\" content=\"description content goes here, called from DB!\">\n\n";
echo "<title>page title goes here, called from DB!</title>\n\n";
}
?>


Now first off I have amended the page names to reflect the ones I have however from what I can see I do not need the or part of each line, would this be true? i.e.

|| $_SERVER['REQUEST_URI'] == "/index.php") on the first line and then all the way through?

thanks

Skorpios



 
Msg#: 4552688 posted 1:36 pm on Mar 10, 2013 (gmt 0)

I have changed the output for each page just to make it easier to see the output.


include 'connect.php';

if ($_SERVER['REQUEST_URI'] == "/home.php" || $_SERVER['REQUEST_URI'] == "/index.php") {
echo "<meta name=\"title\" content=\"Home title content goes here, called from DB!\">\n";
echo "<meta name=\"keywords\" content=\"Home keyword content goes here, called from DB!\">\n";
echo "<meta name=\"description\" content=\"Home description content goes here, called from DB!\">\n\n";
echo "<title>Home page title goes here, called from DB!</title>\n\n";
} elseif ($_SERVER['REQUEST_URI'] == "/contact.php" || $_SERVER['REQUEST_URI'] == "/contact.php") {
echo "<meta name=\"title\" content=\"contact title content goes here, called from DB!\">\n";
echo "<meta name=\"keywords\" content=\"contact keyword content goes here, called from DB! CONTACT\">\n";
echo "<meta name=\"description\" content=\"contact description content goes here, called from DB!\">\n\n";
echo "<title>contact page title goes here, called from DB!</title>\n\n";
} elseif ($_SERVER['REQUEST_URI'] == "/products.php" || $_SERVER['REQUEST_URI'] == "/products.php") {
echo "<meta name=\"title\" content=\"Products title content goes here, called from DB!\">\n";
echo "<meta name=\"keywords\" content=\"products keyword content goes here, called from DB!\">\n";
echo "<meta name=\"description\" content=\"products description content goes here, called from DB!\">\n\n";
echo "<title>products page title goes here, called from DB!</title>\n\n";
} elseif ($_SERVER['REQUEST_URI'] == "/services.php" || $_SERVER['REQUEST_URI'] == "/services.php") {
echo "<meta name=\"title\" content=\"Services title content goes here, called from DB!\">\n";
echo "<meta name=\"keywords\" content=\"services keyword content goes here, called from DB!\">\n";
echo "<meta name=\"description\" content=\"services description content goes here, called from DB!\">\n\n";
echo "<title>services page title goes here, called from DB!</title>\n\n";
} elseif ($_SERVER['REQUEST_URI'] == "/about.php") {
echo "<meta name=\"title\" content=\"About title content goes here, called from DB!\">\n";
echo "<meta name=\"keywords\" content=\"About keyword content goes here, called from DB!\">\n";
echo "<meta name=\"description\" content=\"About description content goes here, called from DB!\">\n\n";
echo "<title>About page title goes here, called from DB!</title>\n\n";
} else {
echo "<meta name=\"title\" content=\"Default title content goes here, called from DB!\">\n";
echo "<meta name=\"keywords\" content=\"Default keyword content goes here, called from DB!\">\n";
echo "<meta name=\"description\" content=\"Default description content goes here, called from DB!\">\n\n";
echo "<title>Default page title goes here, called from DB!</title>\n\n";
}

// The original source of the code is [url]http://support.open-realty.org/archive/index.php/t-1958.html[/url]


I now have the meta info and title in the right location on the pages however it is only picking up the default information which is the last else statement. I realise that this has something to do with the pagename I have entered and so am continuing to try to resolve it hoiwever would appreciate some input from others with more experience of working with php. I also obviously have not included echoing out the meta information yet from the DB, I will do that when I have this working so that the correct meta and title info are displayed upon which page I am viewing.

Hope to get some help with this, any advice or opinions?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved