Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Sep 2008
    Location
    Mexabet.biz
    Posts
    89
    Thanks
    12
    Thanked 0 Times in 0 Posts

    How do I print Meta tags in a CMS?

    I'm creating a photo album and the tables have been created. In tbl_album table I have al_metakeywords and al_metadescription. And in another table, tbl_image, I have im_metakeywords and im_metadescription.

    The PHP script uses $_GET['page'] to extract information from other pages. The pages are 'list-image' and 'image-detail'.

    I want to print al_metakeywords and al_metadescription for the corresponding album when the list-image page loads on the browser. Also I want to print im_metakeywords and im_metadescription for the corresponding image when image-detail page loads.

    I tried this method, but it didn't work:
    PHP Code:
    <meta name="keywords" content="<?php echo $row['al_metakeywords']; ?>" />
    <meta name="description" content="<?php echo $row['al_metadescription']; ?>" />
    Can anybody point me in the right direction? What am I supposed to place inside these tags to echo the custom Meta tags I create in the admin panel?
    Code:
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    I'll appreciate your time and help. And thank you in advance.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You're probably going to need to show more than those two lines. Not many people have the skill to extrapolate the preceding lines based upon those two.
    Are you a Help Vampire?

  • #3
    New Coder
    Join Date
    Sep 2008
    Location
    Mexabet.biz
    Posts
    89
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Tomws,

    Thanks for your reply. The pages, "list-image" and "image-detail" are included into index.php after the header is loaded. Is it possible to use the correct "if" and "else" statement to echo the Meta tags in the index.php Header?

    PHP Code:
    <?php
    #if no page set, or page specified is in white list, echo Meta tags.
    if(false === isset($_GET['page']) || true === in_array($_GET['page'], array('list-image')))
    {
        echo 
    $row['al_metakeywords, al_metadescription'];

    }

    elseif(
    false === isset($_GET['page']) || true === in_array($_GET['page'], array('image-detail')))
    {
        echo 
    $row['im_metakeywords, im_metadescription'];
    }
    ?>
    I used a similar argument to include a template in some of the pages and not in the rest. And I hope to do so in this matter.

    Like I said before, the script uses $_GET['page'] to extract information from other pages. The pages I want the Meta tags to appear in are 'list-image' and 'image-detail'. How do I approach this correctly?

    Quote Originally Posted by tomws View Post
    You're probably going to need to show more than those two lines. Not many people have the skill to extrapolate the preceding lines based upon those two.

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Well, if that's the top of the code, it's not going to work. You haven't assigned a value to $row['anything'] yet. The $row[''] syntax is typically seen in extracting results from a database query along with other related functions. You have none of that up there either.

    If the data is in the $_GET['page'] array, you need to pull from there with something like echo $_GET['al_metakeywords'] or whatever it is. However, if you're going to echo contents of the GET string, it's trivially easy for anyone to make your page display anything they want. Ideally, you should pull this data from a common location (include file, database, text file, etc.) rather than passing it along to each page. It adds unnecessary risk, complication, and overhead.
    Are you a Help Vampire?

  • #5
    New Coder
    Join Date
    Sep 2008
    Location
    Mexabet.biz
    Posts
    89
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks Tomws for your guidance. I want to pull the data from a database, but don't know how to do so.

    This is the code for index.php:
    PHP Code:
    <?php
    require_once 'library/config.php';
    require_once 
    'library/functions.php';

    // which page should be shown now
    $page = (isset($_GET['page']) && $_GET['page'] != '') ? $_GET['page'] : 'list-album';

    // only the pages listed here can be accessed
    // any other pages will result in error
    $allowedPages = array('list-album''list-image''image-detail');

    if (!
    in_array($page$allowedPages)) {
        
    $page 'notfound';



    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?php echo $GLOBALS['page_title']; ?></title>
    <?php print_r$row ); ?>
    <meta name="keywords" content="<?php echo $row['al_metakeywords']; ?>" /> 
    <meta name="description" content="<?php echo $row['al_metadescription']; ?>" />
    <link rel="stylesheet" type="text/css" href="media/cute.css">
    </head>

    <body>
    <table width="960" border="0" align="center" class="outerwrapper">
      <tr>
        <td><?php
    // Display the page header
    include('templates/header.tpl');
    ?>

          <?php
    // Display the intro message
    include('templates/intro.tpl');
    ?></td>
      </tr>
      <tr>
        <td><script type="text/javascript">
                function startGallery() {
                    var myGallery = new gallery($('myGallery'), {
                        timed: true
                    });
                }
                window.addEvent('domready',startGallery);
            </script></td>
      </tr>
      <tr>
        <td><?php
    #if no page set, or page specified is in white list, include template.
    if(false === isset($_GET['page']) || true === in_array($_GET['page'], array('list-album''list-image')))
    {
        include(
    'templates/slideshow.tpl');
    }
    ?>
    <table width="710" border="0" align="right" cellpadding="2" cellspacing="4" class="table_main">
            <tr>
              <td>
    <?php
    include $page'.php';
    ?>          </td>
            </tr>
          </table></td>
      </tr>
      <tr>
      <td>
        <?php
    // Display the page footer
    include('templates/footer.tpl');
    ?>
    </td>
      </tr>
    </table>

    </body>
    </html>
    This is the code for "list-image" (that's list-image.php):
    PHP Code:
    <?php



    $albumId 
    $_GET['album'];



    $query  "SELECT im_id, im_title, im_thumbnail

               FROM tbl_image

               WHERE im_album_id = $albumId

               ORDER BY im_title"
    ;

    $result mysql_query($query) or die('Error, list image failed. ' mysql_error());

    if (
    mysql_num_rows($result) == 0) {

        echo 
    "No image in this album yet";

    } else {

        

        echo 
    '<table width="700" border="0" cellspacing="1" cellpadding="2" align="center">';



        
    // the image is listed in a table 

        // here we specify how many columns 

        // we want to show on each row 

        
    $colsPerRow 4;

        

        
    // width of each column in percent

        
    $colWidth   = (int)(100/$colsPerRow);

        
    $i 0;

        while (
    $row mysql_fetch_assoc($result)) {

            if (
    $i $colsPerRow == 0) {

                
    // start a new row

                
    echo '<tr>';

            }



            echo 
    '<td width="' $colWidth '%">' 

                 
    '<a href="?page=image-detail&album=' $albumId '&image=' $row['im_id'] . '">' 

                 
    '<img src="viewImage.php?type=glthumbnail&name=' $row['im_thumbnail'] . '" border="0">' .

                 
    '<br>' $row['im_title'] . '</a></td>';



            if (
    $i $colsPerRow == $colsPerRow 1) {

                
    // start a new row

                
    echo '</tr>';

            }        

            

            
    $i += 1;

        }

        

        
    // print blank columns

        
    if ($i $colsPerRow != 0) {

            while (
    $i++ % $colsPerRow != 0) {

                echo 
    '<td width="' $colWidth '%">&nbsp;</td>';

            }    

            echo 
    '</tr>';

        }    

        

        echo 
    '</table>';



    }

    ?>
    This is the code for "image-detail" (image-detail.php):
    PHP Code:
    <?php



    // make sure the image id is present

    if (!isset($_GET['image'])) {

        echo 
    "Image id is not defined";

    } else {



        
    // get the image id

        
    $imageId $_GET['image'];

        

        
    $sql  "SELECT im_id, im_album_id, im_title, im_description, im_image,

                        al_name

                 FROM tbl_image im, tbl_album al 

                 WHERE im_id = $imageId AND im.im_album_id = al.al_id"
    ;

        

        
    $result mysql_query($sql) or die('Error, get image info failed. ' mysql_error());

        

        if (
    mysql_num_rows($result) == 0) {

            
    // can't find an image with that id

        
    ?>

    <p align="center">Image not found. Click <a href="index.php?page=list-image">here</a> 

        to go to the image list</p>

    <?php    

        
    } else {    

            
    $image mysql_fetch_assoc($result);

            

            
    // find the previous and next image in this album



            // set the initial value for previous and next image id

            
    $prev $next 0;



            
    // get the previous image

            
    $sql  "SELECT im_id

                     FROM tbl_image 

                     WHERE im_id < $imageId AND im_album_id = {$image['im_album_id']}

                     ORDER BY im_id DESC

                     LIMIT 0, 1"
    ;

            

            
    $result mysql_query($sql) or die('Error, get image info failed. ' mysql_error());

            

            if (
    mysql_num_rows($result) > 0) {

                
    $row mysql_fetch_assoc($result);

                
    $prev $row['im_id'];

            }

            

            
    // get the next image

            
    $sql  "SELECT im_id

                     FROM tbl_image 

                     WHERE im_id > $imageId AND im_album_id = {$image['im_album_id']}

                     ORDER BY im_id ASC

                     LIMIT 0, 1"
    ;

            

            
    $result mysql_query($sql) or die('Error, get image info failed. ' mysql_error());

            

            if (
    mysql_num_rows($result) > 0) {

                
    $row mysql_fetch_assoc($result);

                
    $next $row['im_id'];

            }        

            

    ?>

     

    <table width="100%" border="0" cellpadding="2" cellspacing="1">

        <tr> 

            <td align="center"><img src="viewImage.php?type=glimage&name=<?php echo $image['im_image']; ?>"></td>

      </tr>

        <tr>

            <td style="text-align:left"><div class="describe"><?php echo $image['im_description']; ?></div></td>

        </tr>

        <tr> 

            <td> 

                <?php

    if ($prev 0) {

    ?>

                <a href="index.php?page=image-detail&album=<?php echo $image['im_album_id']; ?>&image=<?php echo $prev?>">&lt; 

                Previous</a> 

                <?php

    }

    ?>

                | 

                <?php

    if ($next 0) {

    ?>

                <a href="index.php?page=image-detail&album=<?php echo $image['im_album_id']; ?>&image=<?php echo $next?>">Next 

                &gt;</a> 

                <?php

    }

    ?>

            </td>

        </tr>

    </table>



    <?php

        
    }

    }

    ?>
    A piece of config.php, so you can see what I have in there:
    Code:
    DROP TABLE IF EXISTS `tbl_album`;
    
    CREATE TABLE `tbl_album` (
    
    `al_id` INT NOT NULL AUTO_INCREMENT,
    
    `al_name` VARCHAR(64) NOT NULL,
    
    `al_metakeywords` TEXT NOT NULL
    
    `al_metadescription` TEXT NOT NULL 
    
    `al_description` TEXT NOT NULL,
    
    `al_image` VARCHAR(64) NOT NULL,
    
    `al_date` DATETIME NOT NULL,
    
    PRIMARY KEY(al_id)
    
    ) TYPE=MyISAM ;
    
    );
    
    
    
    DROP TABLE IF EXISTS `tbl_image`;
    
    CREATE TABLE `tbl_image` (
    
    `im_id` INT NOT NULL AUTO_INCREMENT,
    
    `im_album_id` INT NOT NULL,
    
    `im_title` VARCHAR(64) NOT NULL,
    
    `im_metakeywords` TEXT NOT NULL,
    
    `im_metadescription` TEXT NOT NULL,
    
    `im_description` TEXT NOT NULL,
    
    `im_type` VARCHAR(30) NOT NULL,
    
    `im_image` VARCHAR(60) NOT NULL,
    
    `im_thumbnail` VARCHAR(60) NOT NULL,
    
    `im_date` DATETIME NOT NULL,
    
    PRIMARY KEY(`im_id`)
    
    ) TYPE=MyISAM ;
    
    );
    Now how do I assign al_metakeywords and al_metadescription when "list-image" page (list-image.php) loads, and im_metakeywords and im_metadescription when "image-detail" page (image-detail.php) loads?

    Quote Originally Posted by tomws View Post
    Well, if that's the top of the code, it's not going to work. You haven't assigned a value to $row['anything'] yet. The $row[''] syntax is typically seen in extracting results from a database query along with other related functions. You have none of that up there either.

    If the data is in the $_GET['page'] array, you need to pull from there with something like echo $_GET['al_metakeywords'] or whatever it is. However, if you're going to echo contents of the GET string, it's trivially easy for anyone to make your page display anything they want. Ideally, you should pull this data from a common location (include file, database, text file, etc.) rather than passing it along to each page. It adds unnecessary risk, complication, and overhead.

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    image-detail.php isn't a complete, standalone page, so I'm assuming you're including that in another page, or using AJAX to pull it up. In either of those cases, you can't place the keywords and description into the head tag because the head is already written by the time you're including/AJAXing the image details contents.

    To do what you want, you'd need to change your logic quite a bit.
    Are you a Help Vampire?

  • #7
    New Coder
    Join Date
    Sep 2008
    Location
    Mexabet.biz
    Posts
    89
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Yes, Tomws, image-detail and list-image are not standalone pages. I include them in index.php. Do you think the best way is to make them complete standalone pages - each with its own header?

    Quote Originally Posted by tomws View Post
    image-detail.php isn't a complete, standalone page, so I'm assuming you're including that in another page, or using AJAX to pull it up. In either of those cases, you can't place the keywords and description into the head tag because the head is already written by the time you're including/AJAXing the image details contents.

    To do what you want, you'd need to change your logic quite a bit.

  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You could make that work, but no, I don't think it's necessarily the best way to handle it. The best way would be to restructure the logic and make the "CMS" more robust.
    Are you a Help Vampire?

  • #9
    New Coder
    Join Date
    Sep 2008
    Location
    Mexabet.biz
    Posts
    89
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks for your effort.

    Quote Originally Posted by tomws View Post
    You could make that work, but no, I don't think it's necessarily the best way to handle it. The best way would be to restructure the logic and make the "CMS" more robust.


  •  

    Posting Permissions

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