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

Thread: Meta and title info for dynamic site

Hybrid View

  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
    Mar 2013
    Posts
    16

    Question Dsiplay dynamic meta/title info on requested page in dynamic site : back to basics

    I have still not managed to get the query right to display the meta info and title for the page requested. I have stripped the code back to its very basic to ensure I was getting an output. As you see in the query I have selected a particular id and I get the correct output depending on the number entered. Can someone advise me as to how I ensure that query collects the information depending on the page requested by the user.

    PHP Code:
    include 'connect.php';                        $sql "SELECT         `description`, `keywords`, `author`, `copyright`, `email`, `rating`, `robots`, `revisit`, `expires`, `distribution`, `title`         FROM `meta_data` WHERE meta_id = 3";        $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=\"keywords\" content=\"{$row['keywords']}\" />\n" ;             echo "<meta name=\"copyright\" content=\"{$row['copyright']}\" />\n" ;             echo "<meta name=\"author\" content=\"{$row['author']}\" />\n" ;             echo "<meta name=\"email\" content=\"{$row['email']}\" />\n" ;              echo "<meta name=\"Distribution\" content=\"{$row['distribution']}\" />\n" ;            echo "<meta name=\"Rating\" content=\"{$row['rating']}\" />\n" ;            echo "<meta name=\"Robots\" content=\"{$row['robots']}\" />\n" ;             echo "<meta name=\"Revisit-after\" content=\"{$row['revisit']}\" />\n" ;             echo "<meta name=\"expires\" content=\"{$row['expires']}\" />\n" ;             echo "<title>" $row['title'] . "</title>\n";             } 
    Thanks

  4. #4
    Join Date
    Mar 2013
    Posts
    16

    Question Some help would be appreciated

    I have tried to alter the query to call in the $page_id of each page but cannot seem to get the sql statement right. So far I have

    PHP Code:
    $sql "SELECT                `description`, `keywords`, `author`, `copyright`, `email`, `rating`, `robots`, `revisit`, `expires`, `distribution`, `title`                FROM `meta_data` WHERE meta_id = '/page.php?page_id=$page_id' LIMIT 1";                $query $db->query($sql);                       if($query == false) {                                         die("<pre>".print_r($db->errorInfo(), 1).PHP_EOL.$query."</pre>");                }                               if(isset($page_id) == $meta_id){                    while($row $query->fetch(PDO::FETCH_ASSOC)) {                    echo "<meta name=\"keywords\" content=\"{$row['keywords']}\" />\n" ;                    echo "<meta name=\"copyright\" content=\"{$row['copyright']}\" />\n" ;                    echo "<meta name=\"author\" content=\"{$row['author']}\" />\n" ;                    echo "<meta name=\"email\" content=\"{$row['email']}\" />\n" ;                      echo "<meta name=\"Distribution\" content=\"{$row['distribution']}\" />\n" ;                    echo "<meta name=\"Rating\" content=\"{$row['rating']}\" />\n" ;                    echo "<meta name=\"Robots\" content=\"{$row['robots']}\" />\n" ;                    echo "<meta name=\"Revisit-after\" content=\"{$row['revisit']}\" />\n" ;                    echo "<meta name=\"expires\" content=\"{$row['expires']}\" />\n" ;                    echo "<title>" $row['title'] . "</title>\n";                    }                return $meta_id;             } 
    I am not getting anything returned due to a notice at the start regarding undefined variable $meta_id but the query is also incorrect.

    Can someone advise how I need to write this query to echo out the correct meta/title info please? Thanks in advance

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    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

  6. #6
    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" ;

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    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

  8. #8
    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

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    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

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

  11. #11
    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

  12. #12
    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

  13. #13
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    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

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

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

  15. #15
    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.

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