Meta tags from a database
Results 1 to 6 of 6

Thread: Meta tags from a database

  1. #1
    Somewhere on Mars chrisguk's Avatar
    Join Date
    Jan 2010
    Location
    Someone tell me?
    Posts
    208

    Meta tags from a database

    In order to make sure I pull the correct values from the database so they match the correct page loaded could I do the following:

    PHP Code:
    //This line is contained within my index.php file - $page = isset($_GET['page']) ? $_GET['page'] : 'home';

    <?php

    //The id on each row matches the name of the page name in the variable $page for example; $page = 'about';

    // SQL
    $sql mysql_query("SELECT * FROM database WHERE id = '" $page "' LIMIT 1") or die (mysql_error());

    while(
    $row mysql_fetch_array($sql)){
    $title $row['title']; // get title from database
    $description $row['description']; // get description from database
    $keywords substr($row['keywords'], 0250); // get keywords (250 characters) from database
    }

    // Close database here...
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title><?php echo $title?></title>
    <meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
    <meta http-equiv="Content-Language" content="en">
    <meta name="description" content="<?php echo $description?>">
    <meta name="keywords" content="<?php echo $keywords?>">
    </head>
    <body>

    <!-- HTML content goes here... -->

    </body>
    </html>
    Try not to become a man of success but a man of value

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,886
    You could get all normalized about it and make a keyword table with a foreign key to the page table, so that you would have one entry in the keyword table per keyword, creating a many-to-one relationship to the page table.
    PHP Code:
    $sql "
      SELECT
        page.description,
        CONCAT_WS(',', keyword.keyword) AS keywords
      FROM page
      LEFT JOIN keyword ON keyword.page_id = page.page_id
      WHERE page_id = 
    $pageId
    "

    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  3. #3
    Somewhere on Mars chrisguk's Avatar
    Join Date
    Jan 2010
    Location
    Someone tell me?
    Posts
    208
    @Nogdog,

    Sorry my bad the actual question wasnt visible in my last post:

    This line is contained within my index.php file -

    PHP Code:
    $page = isset($_GET['page']) ? $_GET['page'] : 'home'
    The id on each row matches the name of the page name in the variable $page for example; $page = 'about';

    Could I use that method to make sure the keywords and description match the correct page that is loaded?
    Try not to become a man of success but a man of value

  4. #4
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,886
    Sure, though you'll need a "rainy day" path in your logic to handle situations where the value in $_GET['page'] does not match anything in the DB. Also don't forget that you'll need to sanitize whatever value is received before using it in a query.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  5. #5
    Somewhere on Mars chrisguk's Avatar
    Join Date
    Jan 2010
    Location
    Someone tell me?
    Posts
    208
    Quote Originally Posted by NogDog View Post
    Sure, though you'll need a "rainy day" path in your logic to handle situations where the value in $_GET['page'] does not match anything in the DB. Also don't forget that you'll need to sanitize whatever value is received before using it in a query.
    Okay this is what I have so far, will this work or not.

    At the moment I get an error message saying "no database selected".

    I have called the db - "mydb" and the table "url"

    PHP Code:
    <?php 
    $localhost 
    'localhost';
    $dbuser 'root';
    $dbpass 'password';
    $dbname 'mydb';

    $connect mysql_connect($localhost$dbuser$dbpass);
    mysql_select_db('dbname'$connect);

    $page = array('about','profiles','another_page');

    $page = isset($_GET['page']) ? $_GET['page'] : 'home';

    $sql mysql_query("SELECT * FROM mydb WHERE id = '" $page "' LIMIT 1") or die (mysql_error()); 

    if( 
    mysql_num_rows($sql) ) {
    $row mysql_fetch_array($sql);
    $title $row['title']; // get title from database
    $description $row['description']; // get description from database
    $keywords substr($row['keywords'], 0250); // get keywords (250 characters) from database
    } else {
    $title 'Set Default Title';
    $description 'Set Default Description';
    $keywords 'Set Default Keywords';



    include(
    $_SERVER['DOCUMENT_ROOT']. '/include/header.php');  

    include(
    'content/'.$page.'.php'); 

    include(
    $_SERVER['DOCUMENT_ROOT']. "/include/footer.php");
    ?>
    contents of my header.php

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title><?php echo $title?></title>
    <meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
    <meta http-equiv="Content-Language" content="en">
    <meta name="description" content="<?php echo $description?>">
    <meta name="keywords" content="<?php echo $keywords?>">
    </head>
    <body>

    <!-- HTML content goes here... -->

    </body>
    </html>
    Last edited by chrisguk; 04-22-2012 at 01:29 PM.
    Try not to become a man of success but a man of value

  6. #6
    Somewhere on Mars chrisguk's Avatar
    Join Date
    Jan 2010
    Location
    Someone tell me?
    Posts
    208
    Cancel that, I fixed it and it works perfect:

    changed mysql_select_db('dbname', $connect); to:

    mysql_select_db($dbname, $connect);
    Try not to become a man of success but a man of value

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •