...

View Full Version : How do I print Meta tags in a CMS?



mexabet
10-21-2009, 03:31 PM
I'm creating a photo album and the tables have been created. In tbl_album table I have al_metakeywords and al_metadescription. And in another table, tbl_image, I have im_metakeywords and im_metadescription.

The PHP script uses $_GET['page'] to extract information from other pages. The pages are 'list-image' and 'image-detail'.

I want to print al_metakeywords and al_metadescription for the corresponding album when the list-image page loads on the browser. Also I want to print im_metakeywords and im_metadescription for the corresponding image when image-detail page loads.

I tried this method, but it didn't work:

<meta name="keywords" content="<?php echo $row['al_metakeywords']; ?>" />
<meta name="description" content="<?php echo $row['al_metadescription']; ?>" />

Can anybody point me in the right direction? What am I supposed to place inside these tags to echo the custom Meta tags I create in the admin panel?

<meta name="keywords" content="" />
<meta name="description" content="" />
I'll appreciate your time and help. And thank you in advance.

tomws
10-21-2009, 08:19 PM
You're probably going to need to show more than those two lines. Not many people have the skill to extrapolate the preceding lines based upon those two.

mexabet
10-22-2009, 12:15 PM
Hi Tomws,

Thanks for your reply. The pages, "list-image" and "image-detail" are included into index.php after the header is loaded. Is it possible to use the correct "if" and "else" statement to echo the Meta tags in the index.php Header?


<?php
#if no page set, or page specified is in white list, echo Meta tags.
if(false === isset($_GET['page']) || true === in_array($_GET['page'], array('list-image')))
{
echo $row['al_metakeywords, al_metadescription'];

}

elseif(false === isset($_GET['page']) || true === in_array($_GET['page'], array('image-detail')))
{
echo $row['im_metakeywords, im_metadescription'];
}
?>

I used a similar argument to include a template in some of the pages and not in the rest. And I hope to do so in this matter.

Like I said before, the script uses $_GET['page'] to extract information from other pages. The pages I want the Meta tags to appear in are 'list-image' and 'image-detail'. How do I approach this correctly?


You're probably going to need to show more than those two lines. Not many people have the skill to extrapolate the preceding lines based upon those two.

tomws
10-22-2009, 03:10 PM
Well, if that's the top of the code, it's not going to work. You haven't assigned a value to $row['anything'] yet. The $row[''] syntax is typically seen in extracting results from a database query along with other related functions. You have none of that up there either.

If the data is in the $_GET['page'] array, you need to pull from there with something like echo $_GET['al_metakeywords'] or whatever it is. However, if you're going to echo contents of the GET string, it's trivially easy for anyone to make your page display anything they want. Ideally, you should pull this data from a common location (include file, database, text file, etc.) rather than passing it along to each page. It adds unnecessary risk, complication, and overhead.

mexabet
10-22-2009, 04:51 PM
Thanks Tomws for your guidance. I want to pull the data from a database, but don't know how to do so.

This is the code for index.php:

<?php
require_once 'library/config.php';
require_once 'library/functions.php';

// which page should be shown now
$page = (isset($_GET['page']) && $_GET['page'] != '') ? $_GET['page'] : 'list-album';

// only the pages listed here can be accessed
// any other pages will result in error
$allowedPages = array('list-album', 'list-image', 'image-detail');

if (!in_array($page, $allowedPages)) {
$page = 'notfound';
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $GLOBALS['page_title']; ?></title>
<?php print_r( $row ); ?>
<meta name="keywords" content="<?php echo $row['al_metakeywords']; ?>" />
<meta name="description" content="<?php echo $row['al_metadescription']; ?>" />
<link rel="stylesheet" type="text/css" href="media/cute.css">
</head>

<body>
<table width="960" border="0" align="center" class="outerwrapper">
<tr>
<td><?php
// Display the page header
include('templates/header.tpl');
?>

<?php
// Display the intro message
include('templates/intro.tpl');
?></td>
</tr>
<tr>
<td><script type="text/javascript">
function startGallery() {
var myGallery = new gallery($('myGallery'), {
timed: true
});
}
window.addEvent('domready',startGallery);
</script></td>
</tr>
<tr>
<td><?php
#if no page set, or page specified is in white list, include template.
if(false === isset($_GET['page']) || true === in_array($_GET['page'], array('list-album', 'list-image')))
{
include('templates/slideshow.tpl');
}
?>
<table width="710" border="0" align="right" cellpadding="2" cellspacing="4" class="table_main">
<tr>
<td>
<?php
include $page. '.php';
?> </td>
</tr>
</table></td>
</tr>
<tr>
<td>
<?php
// Display the page footer
include('templates/footer.tpl');
?>
</td>
</tr>
</table>

</body>
</html>


This is the code for "list-image" (that's list-image.php):


<?php



$albumId = $_GET['album'];



$query = "SELECT im_id, im_title, im_thumbnail

FROM tbl_image

WHERE im_album_id = $albumId

ORDER BY im_title";

$result = mysql_query($query) or die('Error, list image failed. ' . mysql_error());

if (mysql_num_rows($result) == 0) {

echo "No image in this album yet";

} else {



echo '<table width="700" border="0" cellspacing="1" cellpadding="2" align="center">';



// the image is listed in a table

// here we specify how many columns

// we want to show on each row

$colsPerRow = 4;



// width of each column in percent

$colWidth = (int)(100/$colsPerRow);

$i = 0;

while ($row = mysql_fetch_assoc($result)) {

if ($i % $colsPerRow == 0) {

// start a new row

echo '<tr>';

}



echo '<td width="' . $colWidth . '%">' .

'<a href="?page=image-detail&album=' . $albumId . '&image=' . $row['im_id'] . '">' .

'<img src="viewImage.php?type=glthumbnail&name=' . $row['im_thumbnail'] . '" border="0">' .

'<br>' . $row['im_title'] . '</a></td>';



if ($i % $colsPerRow == $colsPerRow - 1) {

// start a new row

echo '</tr>';

}



$i += 1;

}



// print blank columns

if ($i % $colsPerRow != 0) {

while ($i++ % $colsPerRow != 0) {

echo '<td width="' . $colWidth . '%">&nbsp;</td>';

}

echo '</tr>';

}



echo '</table>';



}

?>



This is the code for "image-detail" (image-detail.php):


<?php



// make sure the image id is present

if (!isset($_GET['image'])) {

echo "Image id is not defined";

} else {



// get the image id

$imageId = $_GET['image'];



$sql = "SELECT im_id, im_album_id, im_title, im_description, im_image,

al_name

FROM tbl_image im, tbl_album al

WHERE im_id = $imageId AND im.im_album_id = al.al_id";



$result = mysql_query($sql) or die('Error, get image info failed. ' . mysql_error());



if (mysql_num_rows($result) == 0) {

// can't find an image with that id

?>

<p align="center">Image not found. Click <a href="index.php?page=list-image">here</a>

to go to the image list</p>

<?php

} else {

$image = mysql_fetch_assoc($result);



// find the previous and next image in this album



// set the initial value for previous and next image id

$prev = $next = 0;



// get the previous image

$sql = "SELECT im_id

FROM tbl_image

WHERE im_id < $imageId AND im_album_id = {$image['im_album_id']}

ORDER BY im_id DESC

LIMIT 0, 1";



$result = mysql_query($sql) or die('Error, get image info failed. ' . mysql_error());



if (mysql_num_rows($result) > 0) {

$row = mysql_fetch_assoc($result);

$prev = $row['im_id'];

}



// get the next image

$sql = "SELECT im_id

FROM tbl_image

WHERE im_id > $imageId AND im_album_id = {$image['im_album_id']}

ORDER BY im_id ASC

LIMIT 0, 1";



$result = mysql_query($sql) or die('Error, get image info failed. ' . mysql_error());



if (mysql_num_rows($result) > 0) {

$row = mysql_fetch_assoc($result);

$next = $row['im_id'];

}



?>



<table width="100%" border="0" cellpadding="2" cellspacing="1">

<tr>

<td align="center"><img src="viewImage.php?type=glimage&name=<?php echo $image['im_image']; ?>"></td>

</tr>

<tr>

<td style="text-align:left"><div class="describe"><?php echo $image['im_description']; ?></div></td>

</tr>

<tr>

<td>

<?php

if ($prev > 0) {

?>

<a href="index.php?page=image-detail&album=<?php echo $image['im_album_id']; ?>&image=<?php echo $prev; ?>">&lt;

Previous</a>

<?php

}

?>

|

<?php

if ($next > 0) {

?>

<a href="index.php?page=image-detail&album=<?php echo $image['im_album_id']; ?>&image=<?php echo $next; ?>">Next

&gt;</a>

<?php

}

?>

</td>

</tr>

</table>



<?php

}

}

?>



A piece of config.php, so you can see what I have in there:

DROP TABLE IF EXISTS `tbl_album`;

CREATE TABLE `tbl_album` (

`al_id` INT NOT NULL AUTO_INCREMENT,

`al_name` VARCHAR(64) NOT NULL,

`al_metakeywords` TEXT NOT NULL

`al_metadescription` TEXT NOT NULL

`al_description` TEXT NOT NULL,

`al_image` VARCHAR(64) NOT NULL,

`al_date` DATETIME NOT NULL,

PRIMARY KEY(al_id)

) TYPE=MyISAM ;

);



DROP TABLE IF EXISTS `tbl_image`;

CREATE TABLE `tbl_image` (

`im_id` INT NOT NULL AUTO_INCREMENT,

`im_album_id` INT NOT NULL,

`im_title` VARCHAR(64) NOT NULL,

`im_metakeywords` TEXT NOT NULL,

`im_metadescription` TEXT NOT NULL,

`im_description` TEXT NOT NULL,

`im_type` VARCHAR(30) NOT NULL,

`im_image` VARCHAR(60) NOT NULL,

`im_thumbnail` VARCHAR(60) NOT NULL,

`im_date` DATETIME NOT NULL,

PRIMARY KEY(`im_id`)

) TYPE=MyISAM ;

);


Now how do I assign al_metakeywords and al_metadescription when "list-image" page (list-image.php) loads, and im_metakeywords and im_metadescription when "image-detail" page (image-detail.php) loads?


Well, if that's the top of the code, it's not going to work. You haven't assigned a value to $row['anything'] yet. The $row[''] syntax is typically seen in extracting results from a database query along with other related functions. You have none of that up there either.

If the data is in the $_GET['page'] array, you need to pull from there with something like echo $_GET['al_metakeywords'] or whatever it is. However, if you're going to echo contents of the GET string, it's trivially easy for anyone to make your page display anything they want. Ideally, you should pull this data from a common location (include file, database, text file, etc.) rather than passing it along to each page. It adds unnecessary risk, complication, and overhead.

tomws
10-22-2009, 05:41 PM
image-detail.php isn't a complete, standalone page, so I'm assuming you're including that in another page, or using AJAX to pull it up. In either of those cases, you can't place the keywords and description into the head tag because the head is already written by the time you're including/AJAXing the image details contents.

To do what you want, you'd need to change your logic quite a bit.

mexabet
10-22-2009, 05:56 PM
Yes, Tomws, image-detail and list-image are not standalone pages. I include them in index.php. Do you think the best way is to make them complete standalone pages - each with its own header?


image-detail.php isn't a complete, standalone page, so I'm assuming you're including that in another page, or using AJAX to pull it up. In either of those cases, you can't place the keywords and description into the head tag because the head is already written by the time you're including/AJAXing the image details contents.

To do what you want, you'd need to change your logic quite a bit.

tomws
10-22-2009, 07:59 PM
You could make that work, but no, I don't think it's necessarily the best way to handle it. The best way would be to restructure the logic and make the "CMS" more robust.

mexabet
10-22-2009, 08:16 PM
Thanks for your effort.


You could make that work, but no, I don't think it's necessarily the best way to handle it. The best way would be to restructure the logic and make the "CMS" more robust.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum