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

Thread: Easy title/keyword tags

  1. #1
    Join Date
    Jul 2006
    Posts
    373

    Easy title/keyword tags

    Hi,

    im making a php site where every page has a title, its keywords and description. The thing is that i would like to have a separate file, call it tags.php and have all the tags defined there so i can change them anytime without having to edit every single page. I have a vague idea on how to do it but im pretty much stuck.

    Lets say i have for the moment 2 pages, index.php and schedule.php (or index.php?s=schedule) so i want to define the following tags for each one of them. The first being title, the second the keywords and the 3rd the description.

    PHP Code:

    $index
    = array("title"=>"Clinic Name""keywords"=>"clinic,  medical""desc"=>"A good clinic for you");

    $schedule= array("title"=>"Consultation""keywords"=>"hours, time""desc"=>"some description"); 
    whereas the html code would look like this

    HTML Code:
    <!DOCTYPE html>
    <html lang="en">
     <head>
      <title>$page[$title]</title>
      <meta charset=utf-8>
      <meta name="description" content="$description">
      <meta name="keywords" content="$keywords">
    the template is the same for all pages so the idea is to have dynamically replaced the tags with the ones corresponding to that page. $page is the name of the page which i automatically get with $page = $_GET['s'];

    So this is the basic idea but i really dont know pretty much where to start. Any ideas please?

    Thank you!

  2. #2
    Join Date
    Jul 2006
    Posts
    373
    No answer? okay, i figured out this so far:

    PHP Code:
    $tags = array("index" => array("title" => "Clinic Name""keywords" => "clinic,  medical""desc" => "A good clinic for you"), "schedule" => array("title" => "Consultation""keywords" => "hours, time""desc" => "some description"));

     
    $page $_GET['s']; 
    and in the html

    HTML Code:
    $tags[$page][title]
    it works, but i just feel like it could be done in a much practical way. Otherwise i have to use the words "title", "keywords" and "desc" so many times in the code which is redundant. I would like to compact it into something like this:

    PHP Code:
    $index = array("Clinic Name",  "clinic,  medical""A good clinic for you");

    $schedule = array("Consultation""hours, time""some description"); 
    so i dont have to repeat the same words. Anybody got any ideas?

    Thank you.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    I might do something like this (or I might not ):
    PHP Code:
    <?php
    /**
     * Get HTML text for title element plus keywords and description meta tags
     * @return string
     * @param string $pageName Top-level key in $data array
     */
    function getHtmlHead($pageName="default")
    {
       
    $data = array(
          
    "default" => array(
             
    "title" => "Default Title",
             
    "keywords" => array(
                
    "word1",
                
    "word2"
             
    ),
             
    "description" => "This is a sample description."
          
    ),
          
    "index" => array(
             
    "title" => "Home",
             
    "keywords" => array(
                
    "foo",
                
    "bar"
             
    ),
             
    "description" => "Welcome to the home page."
          
    ),
          
    "schedule" => array(
             
    "title" => "Schedule",
             
    "keywords" => array(
                
    "Larry",
                
    "Moe",
                
    "Curly"
             
    ),
             
    "description" => "This is the schedule page."
          
    )
       );
       if( ! 
    array_key_exists($pageName$data)) {
          
    $pageName "default";
          
    error_log("Invalid pageName '$pageName' in ".__FUNCTION__."()");
       }
       
    $template = <<<EOD
    <html>
    <head>
    <title>%s</title>
    <meta name='keywords' content='%s' />
    <meta name='description' content='%s' />
    </head>
    <body>

    EOD;

       return 
    sprintf(
          
    $template,
          
    htmlspecialchars($data[$pageName]['title']),
          
    htmlspecialcahrs(implode(', '$data[$pageName]['keywords'])),
          
    htmlspecialchars($data[$pageName]['description'])
       );
    }
    Usage:
    PHP Code:
    <?php
    require_once 'path/to/the/above/file.php';
    echo 
    getMetaTags('schedule');
    ?>
    <div>Main content here...</div>
    "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
    Jul 2006
    Posts
    373
    Thanks. But then, there is no way to abbreviate and avoid the repeated usage of the words "title", "keywords" and "description" like the example i gave in my second post? i believe we can get a much shorter code and that would be more likeable.

  5. #5
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by supercain View Post
    No answer? okay, i figured out this so far:

    PHP Code:
    $tags = array("index" => array("title" => "Clinic Name""keywords" => "clinic,  medical""desc" => "A good clinic for you"), "schedule" => array("title" => "Consultation""keywords" => "hours, time""desc" => "some description"));

     
    $page $_GET['s']; 
    and in the html

    HTML Code:
    $tags[$page][title]
    it works, but i just feel like it could be done in a much practical way. Otherwise i have to use the words "title", "keywords" and "desc" so many times in the code which is redundant. I would like to compact it into something like this:

    PHP Code:
    $index = array("Clinic Name",  "clinic,  medical""A good clinic for you");

    $schedule = array("Consultation""hours, time""some description"); 
    so i dont have to repeat the same words. Anybody got any ideas?

    Thank you.
    While it may not be practical for this project...this is a good candidate for storing the information in a database. Then you can remove all the array building code and put in a quick SQL statement for the page you are on.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    I guess I didn't see that part about repeating words, but generally speaking I tend to err on the side of verbosity in my source code, as that makes it much easier to maintain. A little copy-and-pasting can make it easy to duplicate as needed.

    If you don't want to go with a DB solution (a perfectly valid suggestion, mind you), you could look into setting up a parse_ini_file() solution, perhaps, where the ini file might be something like:
    Code:
    [index]
    title = "index"
    description = "This is the home page"
    keywords[] = "keyword 1"
    keywords[] = "keyword 2"
    
    [schedule]
    title = "Schedule Page"
    description = "This is the scheduling page"
    keywords[] = "foo"
    keywords[] = "bar"
    PS: Another popular option these days would be to set up a JSON file and use json_decode() to populate an array.
    "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

  7. #7
    Join Date
    Jul 2006
    Posts
    373
    Thanks. I didnt know about that. Sounds interesting but seems like you have to repeat again the words "title", "keywords" and "description" for each array just like in the other codes, which is i would like to avoid just for the sake of having a much shorter code.

    I cant help to think that something in this format wuld look great:

    PHP Code:
    $index = array("Clinic Name",  "clinic,  medical""A good clinic for you");

    $schedule = array("Consultation""hours, time""some description"); 
    but i fail to figure out how to have the code understand that the first value of the array is for title, the second for keywords and the 3rd for description without having to write those every time.

  8. #8
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by supercain View Post
    Thanks. I didnt know about that. Sounds interesting but seems like you have to repeat again the words "title", "keywords" and "description" for each array just like in the other codes, which is i would like to avoid just for the sake of having a much shorter code.

    I cant help to think that something in this format wuld look great:

    PHP Code:
    $index = array("Clinic Name",  "clinic,  medical""A good clinic for you");

    $schedule = array("Consultation""hours, time""some description"); 
    but i fail to figure out how to have the code understand that the first value of the array is for title, the second for keywords and the 3rd for description without having to write those every time.
    If you stored them in a database you'd have something like:
    PHP Code:
    $title "";//get this however you like...
    //connect to db
    //select database
    $sql "SELECT title, keyword, description FROM table_name LEFT JOIN tag_table on table_name.foreign_key = tag_table.foreign_key WHERE title='$title'"//don't forget to sanitize if you are using GET or POST
    $result mysql_query($sql);
    if(!
    $result) die mysql_error();
    while(
    $row mysql_fetch_array($result)){
    //this is where you do whatever with your tags for the specified title


  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    Quote Originally Posted by supercain View Post
    Thanks. I didnt know about that. Sounds interesting but seems like you have to repeat again the words "title", "keywords" and "description" for each array just like in the other codes, which is i would like to avoid just for the sake of having a much shorter code.

    I cant help to think that something in this format wuld look great:

    PHP Code:
    $index = array("Clinic Name",  "clinic,  medical""A good clinic for you");

    $schedule = array("Consultation""hours, time""some description"); 
    but i fail to figure out how to have the code understand that the first value of the array is for title, the second for keywords and the 3rd for description without having to write those every time.
    At the risk of repeating myself, doing something simply to make the code shorter is seldom much of a reason for doing so. But, if you feel it is important, then you would just have to know that, for example, $array[0] is one type of thing, $array[1] another, and so forth. That then means you have to be careful about staying 100% consistent with your ordering, never leaving out an element, and you probably should add a fairly verbose comment to your code explaining what you're doing -- which makes simply using the more verbose code in the first place seem, perhaps, less verbose?
    "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
    Jul 2006
    Posts
    373
    Thanks again. The usage of a database seems like a good a idea but the site doesnt have one so unfortunately i cant use that option.

    One question for Nogdog, according to your example can we say that $index [0] equals Clinic Name while $schedule[0] = Consultation and so on? if so, then i may have figured out a way.

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    Quote Originally Posted by supercain View Post
    ...One question for Nogdog, according to your example can we say that $index [0] equals Clinic Name while $schedule[0] = Consultation and so on?...
    Yes, as long as you are totally consistent on how you define each such enumerated array.
    "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
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Just my 2c, but you could also define constants to give your array members a better description:

    PHP Code:
    <?php

        
    /* DEFINITION */

        
    define('title'0);
        
    define('keywords'1);
        
    define('desc'2);
        
    $tags = array(
            
    'index'    => array('Clinic Name''clinic, medical''A good clinic for you'),
            
    'schedule' => array('Consultation''hours, time''some description')
        );

        
    /* USAGE */

        
    echo $tags['index'][title] . '<br />';
        echo 
    $tags['index'][keywords] . '<br />';
        echo 
    $tags['index'][desc] . '<br />';

    ?>
    Then at least if the order changes or you add more fields, you can simply reindex the constant values.

    - or as a different option, have a function build your array for you -

    PHP Code:
    <?php

        
    /* DEFINITION */

        
    function createTags($data) {$tags = array(); foreach ($data as $key => $value) {$tags[$key] = array('title' => $value[0], 'keywords' => $value[1], 'desc' => $value[2]);} return $tags;}
        
    $tags createTags(array(
            
    'index'    => array('Clinic Name''clinic, medical''A good clinic for you'),
            
    'schedule' => array('Consultation''hours, time''some description')
        ));

        
    /* USAGE */

        
    echo $tags['index']['title'] . '<br />';
        echo 
    $tags['index']['keywords'] . '<br />';
        echo 
    $tags['index']['desc'] . '<br />';

    ?>
    Last edited by bionoid; 12-31-2011 at 11:45 AM.
    JavaScript: Learn | Validate | Compact | bionoid

  13. #13
    Join Date
    Dec 2011
    Posts
    34
    Code:
    <?php
    $pages=array(
      'headers'=>array('title','keywords','desc'),
      'index'=>array('Clinic Name','clinic, medical','A Good Clinic for you.'),
      'schedule'=>array('Consulation','hours, time','Hours of operation')
    );
    
    $page=NULL;
    
    function SelectPage($name=NULL)
    {
      global $pages,$page;
      $page=NULL;
      if(isset($pages[$name]))
      {
        foreach($pages['headers'] as $idx=>$val)
          $page[$val]=$pages[$name][$idx];
      }
    }
    
    
    SelectPage('index');
    $sample=<<<EOF
    <h1>{$page['title']}</h1><br />
    <em>keywords: </em>{$page['keywords']}<br />
    <em>desc: </em>{$page['desc']}<br />
    EOF;
    echo $sample;
    SelectPage('schedule');
    $sample=<<<EOF
    <h1>{$page['title']}</h1><br />
    <em>keywords: </em>{$page['keywords']}<br />
    <em>desc: </em>{$page['desc']}<br />
    EOF;
    echo $sample;
    There ya Goes

  14. #14
    Join Date
    Jul 2006
    Posts
    373
    Great! now i finally have what i was looking for. Thank you all for your suggestions and support.

  15. #15
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    A website I designed with CMS, was all database driven. Pages could be customized by pulling page specific title, description, keywords from the database as well as the content.

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
  •  
HTML5 Development Center



Recent Articles