Jump to content

Recommended Posts

Hi there..

 

Ineed help about dynamic meta. like title,meta description,meta tags

 

 

please first visit the link its a demo site

 

link

 

and visit pages apple.banans etc..

 

all the pages loads from databses from getting value from url

 

look the  the page title. meta tags are not changed...

 

because i dont know hoe to change them dynamically..

 

can anyone help this..

 

1.How to change meta tags dynamically? any tutorial?

 

2.is it possible without php.mysql? by xml?

 

 

please help me

Link to comment
https://forums.phpfreaks.com/topic/239413-how-to-change-meta/
Share on other sites

As every page will need to have it's own title and meta tags ("description" I guess), your best bet is adding them to the article rows. In example:

 

Table "articles"

--------------------------------------------

id | title | content | meta_description

--------------------------------------------

1 | Apples | Apples are healthy to eat everyday | Not all apples are red

 

<?php
$id = $_GET['id'];
$results = mysql_query("SELECT title, content, meta_description FROM articles WHERE id=$id");
$values = mysql_fetch_assoc($results);

$title = $values['title'];
$content = $values['content'];
$meta_description = $values['meta_description'];
?>
<html>
<head>
<meta name="description" content="<?php echo $meta_description; ?>" />
<title>MySite.com - <?php echo $title; ?></title>
</head>

<body>
<h1><?php echo $title; ?></h1>
<?php echo $content; ?>
</body>
</html>

 

The code is just to give you a basic idea of how to handle it, but you'll need to validate input, check if the article exists, stripslashes, etc.

Link to comment
https://forums.phpfreaks.com/topic/239413-how-to-change-meta/#findComment-1229941
Share on other sites

Thanks mate. i got idea from you,reaaly happy...

 

can you please chelc out if iam right or wrong? as i am very new in php please corection me if did anything wrong..

 

Table name: page

 

----------------------------------------------------------------------

id  |  title  | meta_keywords  | meta_description | dtl(content)

----------------------------------------------------------------------

 

1 | apples page |apple,red,sweet,green |apples are green | appleas are green,red,blue bla bla bla bla,this iscontent

 

 

and whole structure of my page is now

 

 

<?php 
mysql_connect("localhost", "ash", "lash") or die("Connection Failed");
mysql_select_db("lash")or die("Connection Failed");
$id = isset($_GET['id']) ? filter_var($_GET['id'], 
FILTER_VALIDATE_INT) : NULL;
if (is_int($id)) {    
$result = mysql_query("SELECT title, dtl, meta_description,meta_keywords FROM page WHERE id='$id'") or die(mysql_error());    $row = mysql_fetch_array($result);    

?>

<html>
<head>
<meta name="description" content="<?php echo $meta_description; ?>" />
<meta name="keywords" content="<?php echo $meta_keywords; ?>" />
<title>MySite.com - <?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<?php echo $dtl; ?></body>
</html>

 

 

Please please correct me if anything wrong

Link to comment
https://forums.phpfreaks.com/topic/239413-how-to-change-meta/#findComment-1229990
Share on other sites

<?php 
mysql_connect('localhost', 'ash', 'lash') or die('Connection Failed');
mysql_select_db('lash') or die('Database not found');

$id = (int) $_GET['id'];
  
$result = mysql_query("SELECT title, dtl, meta_description, meta_keywords FROM page WHERE id=$id");

if (!mysql_num_rows($result)) {
     header('Location: 404.php', true, 404);
     die();
}
$row = mysql_fetch_array($result);

$title = stripslashes($row['title']);
$dtl = stripslashes($row['dtl']);
$meta_desc = stripslashes($row['meta_description']);
$meta_keys = stripslashes($row['meta_keywords']);
?>

<html>
<head>
<meta name="description" content="<?php echo $meta_description; ?>" />
<meta name="keywords" content="<?php echo $meta_keywords; ?>" />
<title>MySite.com - <?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<?php echo $dtl; ?></body>
</html>

 

As the script is intended for production, I added some control if the row exists. You can't show an article when it doesn't exist, right? In that case it does a redirect (with a 404 status code) to a "404.php" page. The best way to do that would be just sending a 404 header and creating an .htaccess file with an ErrorDocument for 404. Anyway, it will work the same.

 

I added the variables too (ie. $title = stripslashes($row['title'])) :)

Link to comment
https://forums.phpfreaks.com/topic/239413-how-to-change-meta/#findComment-1230003
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.