www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Meta and title info for dynamic site

  1. #1
    Join Date
    Mar 2013
    Posts
    16

    Question Meta and title info for dynamic site

    I have gone through the numerous threads regarding this subject but cannot translate any of the solutions over to my basic project. I have followed a tutorial on a basic dynamic site, the index page calls in home.php or whichever is selected from the menu. I have connected to a Mysql DB, this is working fine. I have my SELECT statement and variables in place with help from various sources however I only get the first record meta and title information showing no matter which page I view.

    I have tried as with a static site having a $page_id on ever page, this worked fine with a static site but when it came to transferring that over to the dynamic site I am getting no where fast.

    I would be grateful for some help and assistance. Thanks in advance.

    My code so far is

    PHP Code:

             
    //fetchmeta.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 
    "<meta name=\"$desc\" content=\"$content\" />\n" ;    
            }
            echo 
    "<title>";
            echo 
    $row[$title];
            echo 
    "</title>\n"
    My site code is

    PHP Code:

             // index.php

    <?php require_once 'inc/header.php'?>

    <div id="content">
    <?php
        $pages_dir 
    'inc';
        if(!empty(
    $_GET['page'])){    
            
    $pages scandir($pages_dir0);
            unset(
    $pages[0], $pages[1]);    
            
    $page $_GET['page'];
            if (
    in_array($page.'.php'$pages)){
                include 
    $pages_dir.'/'.$page.'.php';
            } else {
                echo 
    "<br><b>Sorry that page does not exist.</b><br><br>You will be redirected in 5 seconds to the homepage.";        
                
    header("refresh:5; index.php");
            }
        } else {
            include (
    $pages_dir.'/home.php');
        }                
    ?>
    </div> <!-- end of wrapper -->
    <?php require_once 'inc/footer.php'?>
    The header.php file is

    PHP Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <?php include_once 'fetchmeta.php'?>
    <link href="inc/styles.css" rel="stylesheet" type="text/css" media="screen">
    </head>

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

    <?php include 'menu.php'?>
    As I say I have tried including a $page_id variable on the content pages but this does not do anything.

    PHP Code:
    <?php $page_id 1?>

    Welcome to the home page
    Could someone shed some light on where I am going wrong. Thanks again.

  2. #2
    Join Date
    Mar 2013
    Posts
    16

    Question Output

    <!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="introduction to our site" />
    <meta name="keywords" content="products, jelly, ice, cream, milk" />
    <meta name="author" content="Author" />
    <meta name="copyright" content="Organisations Name, 2013" />
    <meta name="email" content="email address" />
    <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>

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    Try $query->fetchAll() instead of $query->fetch().
    "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."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Mar 2013
    Posts
    16
    Unfortunately that returns

    Notice: Array to string conversion in C:\xampp\htdocs\dynamicmeta\dynamic3\inc\fetchmeta.php on line 28

    28 being

    echo "<meta name=\"$desc\" content=\"$content\" />\n" ;

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    ok, here's how I'd do it, keeping to a fairly minimal amount of code:
    PHP Code:
    $sql "SELECT `desc`, `content` FROM `meta_data` WHERE `meta_id` = {$page_id}";
    $query $db->query($sql);
    while(
    $row $query->fetch(PDO::FETCH_ASSOC)) {
        echo 
    "<meta name=\"{$row['desc']}\" content=\"{$row['content']}\" />\n" ;    

    "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."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Mar 2013
    Posts
    16
    I tried your solution, thanks for the reply.

    Unfortunately I get

    Fatal error: Call to a member function fetch() on a non-object in C:\xampp\htdocs\dynamicmeta\dynamic3\inc\fetchmeta.php on line 21

    And if I try fetchAll I get

    Fatal error: Call to a member function fetchAll() on a non-object in C:\xampp\htdocs\dynamicmeta\dynamic3\inc\fetchmeta.php on line 21

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    Try some debugging:
    PHP Code:
    $sql "SELECT `desc`, `content` FROM `meta_data` WHERE `meta_id` = {$page_id}";
    $query $db->query($sql);
    if(
    $query == false) {
        die(
    "<pre>".$db->errorInfo().PHP_EOL.$query."</pre>");
    }
    while(
    $row $query->fetch(PDO::FETCH_ASSOC)) {
        echo 
    "<meta name=\"{$row['desc']}\" content=\"{$row['content']}\" />\n" ;    

    "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."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Mar 2013
    Posts
    16
    I am working my way through but initial tests give me

    Notice: Array to string conversion in C:\xampp\htdocs\dynamicmeta\dynamic3\inc\fetchmeta.php on line 22
    Array

    I will reply when I have researched a little.

    Thanks again

  9. #9
    Join Date
    Mar 2013
    Posts
    16
    I forgot to ask is implode(); the way to go?

  10. #10
    Join Date
    Mar 2013
    Posts
    16
    I still have not managed to get anywhere with this, I would be grateful if someone could give me a little more direction to resolve this. Thanks

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    Sorry, I forgot that errorInfo() returns an array, so...
    PHP Code:
    die("<pre>".print_r($db->errorInfo(), 1).PHP_EOL.$query."</pre>"); 
    "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."
    ~ Terry Pratchett in Nation

    eBookworm.us

  12. #12
    Join Date
    Mar 2013
    Posts
    16
    The output gives me

    Array
    (
    [0] => 42S22
    [1] => 1054
    [2] => Unknown column 'desc' in 'field list'
    )

  13. #13
    Join Date
    Mar 2013
    Posts
    16
    Amended desc to description, now get

    Array
    (
    [0] => 42S22
    [1] => 1054
    [2] => Unknown column 'content' in 'field list'
    )

  14. #14
    Join Date
    Mar 2013
    Posts
    16
    The first problem is that there is not a field called content in the DB. I see that 1, I have had several suggestions and think I have mixed them up at some point so re-entered the code you gave me. Now I need to enter all the fields in the SELECT query and see where that leaves me.

  15. #15
    Join Date
    Mar 2013
    Posts
    16

    This is what I have now, starting to make a little sense to me, I think

    With the code NogDog has given me and my small amendments to my SELECT statement I now have the following code, the echo statement is not what I need to work on as the meta info is out of sync now so need to get the right fields called in.

    Thanks for your help so far NogDog.

    PHP Code:
    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 
            `description`, `keywords`, `author`, `copyright`, `email`, `rating`, `robots`, `revisit`, `expires`, `distribution` 
            FROM `meta_data` WHERE `meta_id` = 
    {$page_id}";
            
    $query $db->query($sql);
            if(
    $query == false) {
                
                die(
    "<pre>".print_r($db->errorInfo(), 1).PHP_EOL.$query."</pre>");
            }
            while(
    $row $query->fetch(PDO::FETCH_ASSOC)) {
                echo 
    "<meta name=\"{$row['keywords']}\" content=\"{$row['keywords']}\" />\n" ;    
            }  
            
            echo 
    "<title>"
            echo 
    $row[$title];
            echo 
    "</title>\n"
    At the moment the output is

    Code:
    <meta name="keywords" content="description" />
    I have lost the title info again though.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

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



Recent Articles