www.webdeveloper.com
Results 1 to 11 of 11

Thread: Is there a better way to do this?

  1. #1
    Join Date
    Apr 2012
    Posts
    23

    Is there a better way to do this?

    I have been using the following method for a long time now, but someone recently said to me its ugly and there are ways to do it that will perform quicker too. He mentioned using and array in a separate file but I am not familiar with this method. Can anyone give me some guidance please?

    PHP Code:
    <?php
    $about 
    = isset($_GET['d']) ? $_GET['d'] : 'about';

    switch(
    $about) {
    /*----------------------- PAGES -----------------------------------*/
    case 'profile':
    $title 'My Title';
    $keyword 'some keywords';
    $description 'A great description.';
    break;

    default:
    $title 'My Title';
    $keyword 'some keywords';
    $description 'A great description.';
    break;
    }
    include(
    $_SERVER['DOCUMENT_ROOT']. '/include/header.php');  

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

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

    ?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    Maybe they mean something like this:

    pages.php:
    PHP Code:
    <?php
    $pages 
    = array(
        
    'profile' => array(
            
    $title 'My Title';
            
    $keyword 'some keywords';
            
    $description 'A great description.';
        ),
        
    'another_page' => array(
            
    $title 'Another Title';
            
    $keyword 'some more keywords';
            
    $description 'A better description.';
        ),
        
    'default' => array(
            
    $title 'My Title';
            
    $keyword 'some keywords';
            
    $description 'A great description.';
        )
    );
    $page 'default';
    if(isset(
    $_GET['page']) and isset($pages[$_GET['page']])) {
        
    $page $_GET['page'];
    }
    ?>
    Some page:
    PHP Code:
    <?php
    require_once 'pages.php';
    ?>
    <html>
    <head>
    <title><?php echo $pages[$page]['title'];?></title>
    <meta name='keywords' content='<?php echo $pages[$page]['keyword'];?>' />
    <meta name='description' content='<?php echo $pages[$page]['description'];?>' />
    "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

  3. #3
    Join Date
    Apr 2012
    Posts
    23
    Thanks NogDog,

    I have been doing some reading and your suggestion seems to be the most practical method. Are there any issues with performance in that example?

  4. #4
    Join Date
    Apr 2012
    Posts
    23
    Quote Originally Posted by chrisguk View Post
    Thanks NogDog,

    I have been doing some reading and your suggestion seems to be the most practical method. Are there any issues with performance in that example?
    In fact just to add to that.

    The code I supplied is contained within my index.php, which of you can see pulls the content pages in. My header and footer are includes.

    because the variable $page is declared in the pages.php how would the index.php know what the variable $page is?

    Or!!!

    Is the construction of my index.php completely wrong in that I dont need an include for the header and footer?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    There are many, many ways to organize such things, and I'm not going to claim that one is better than another. One way to approach it, though, would be to functionalize things as much as possible. You could put the code I cobbled together into a function that would just return a single, simple array based on the value provided to it. You could then put the header and footer parts of the page into a function. Then you would just call those functions as needed, e.g.:

    page_stuff.php:
    PHP Code:
    <?php
    /**
     * Get the attributes of a page
     * @return array
     * @param string $pageName Name of the page
     */
    function getPageInfo($pageName)
    {
        
    $pages = array(
            
    'profile' => array(
                
    $title 'My Title';
                
    $keyword 'some keywords';
                
    $description 'A great description.';
            ),
            
    'another_page' => array(
                
    $title 'Another Title';
                
    $keyword 'some more keywords';
                
    $description 'A better description.';
            ),
            
    'default' => array(
                
    $title 'My Title';
                
    $keyword 'some keywords';
                
    $description 'A great description.';
            )
        );
        
    $page 'default';
        if(isset(
    $_GET['page']) and isset($pages[$_GET['page']])) {
            
    $page $_GET['page'];
        }
        return 
    $pages[$page];
    }

    /**
     * Get the top-of-page stuff
     * @return string
     * @param array $pageData
     */
    function htmlHead(array $pageData)
    {
        
    /*
            might want some error-checking here to make sure $pageData is complete
        */
        
    $html "<!DOCTYPE html>
    <html>
        <head>
            <title>"
    .htmlspecialchars($pageData['title'])."</title>
            <meta name='keywords' content='"
    .htmlspecialchars($pageData['keyword'])."' />
            <meta name='description' content='"
    .htmlspecialchars($pageData['description'])."' />
        </head>
        <body>"
    ;
        return 
    $html;
    }
    index.php:
    PHP Code:
    <?php
    require_once 'page_stuff.php';
    $pageName = isset($_GET['page']) ? $_GET['page'] : 'default';
    echo 
    htmlHead(getPageInfo($pageName));
    Those functions could be in the same include file, or separate files, or you could even get all object-oriented about it and put them into class definitions.

    You could even separate the data from the code more by putting the $pages array into a separate, non-PHP file that gets read by that function. The JSON format is very handy for this sort of thing, as it can be quickly and easily parsed into a PHP array (or object) via json_decode().
    "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
    Apr 2012
    Posts
    23
    Quote Originally Posted by NogDog View Post
    There are many, many ways to organize such things, and I'm not going to claim that one is better than another. One way to approach it, though, would be to functionalize things as much as possible. You could put the code I cobbled together into a function that would just return a single, simple array based on the value provided to it. You could then put the header and footer parts of the page into a function. Then you would just call those functions as needed, e.g.:

    page_stuff.php:
    PHP Code:
    <?php
    /**
     * Get the attributes of a page
     * @return array
     * @param string $pageName Name of the page
     */
    function getPageInfo($pageName)
    {
        
    $pages = array(
            
    'profile' => array(
                
    $title 'My Title';
                
    $keyword 'some keywords';
                
    $description 'A great description.';
            ),
            
    'another_page' => array(
                
    $title 'Another Title';
                
    $keyword 'some more keywords';
                
    $description 'A better description.';
            ),
            
    'default' => array(
                
    $title 'My Title';
                
    $keyword 'some keywords';
                
    $description 'A great description.';
            )
        );
        
    $page 'default';
        if(isset(
    $_GET['page']) and isset($pages[$_GET['page']])) {
            
    $page $_GET['page'];
        }
        return 
    $pages[$page];
    }

    /**
     * Get the top-of-page stuff
     * @return string
     * @param array $pageData
     */
    function htmlHead(array $pageData)
    {
        
    /*
            might want some error-checking here to make sure $pageData is complete
        */
        
    $html "<!DOCTYPE html>
    <html>
        <head>
            <title>"
    .htmlspecialchars($pageData['title'])."</title>
            <meta name='keywords' content='"
    .htmlspecialchars($pageData['keyword'])."' />
            <meta name='description' content='"
    .htmlspecialchars($pageData['description'])."' />
        </head>
        <body>"
    ;
        return 
    $html;
    }
    index.php:
    PHP Code:
    <?php
    require_once 'page_stuff.php';
    $pageName = isset($_GET['page']) ? $_GET['page'] : 'default';
    echo 
    htmlHead(getPageInfo($pageName));
    Those functions could be in the same include file, or separate files, or you could even get all object-oriented about it and put them into class definitions.

    You could even separate the data from the code more by putting the $pages array into a separate, non-PHP file that gets read by that function. The JSON format is very handy for this sort of thing, as it can be quickly and easily parsed into a PHP array (or object) via json_decode().
    Hi,

    I have tried your code and keep getting an error regarding this line:

    Parse error: syntax error, unexpected ';', expecting ')' in /var/www/test/page_stuff.php on line 7

    PHP Code:
    $title 'My Title'

  7. #7
    Join Date
    Feb 2012
    Location
    youTUBE
    Posts
    234

    typos

    There were typographic mistakes on each set corresponding to the following correction:
    Code:
     
            'another_page' => array( 
                $title => 'Another Title'; 
                $keyword => 'some more keywords'; 
                $description => 'A better description.'; 
            ),
    and you should set beforehand:
    Code:
     
                $title = 'title'; 
                $keyword = 'key' 
                $description = 'desc';
    then try it again...

  8. #8
    Join Date
    Apr 2012
    Posts
    23
    Quote Originally Posted by WyCnet View Post
    There were typographic mistakes on each set corresponding to the following correction:
    Code:
     
            'another_page' => array( 
                $title => 'Another Title'; 
                $keyword => 'some more keywords'; 
                $description => 'A better description.'; 
            ),
    and you should set beforehand:
    Code:
     
                $title = 'title'; 
                $keyword = 'key' 
                $description = 'desc';
    then try it again...
    Hi,

    That has not fixed it. This is what I have:

    PHP Code:
    <?php
    $pages 
    = array(
        
    'another_page' => array( 
                
    $title => 'Another Title'
                
    $keyword => 'some more keywords'
                
    $description => 'A better description.'
            ),
        
    'another_page' => array( 
                
    $title => 'Another Title'
                
    $keyword => 'some more keywords'
                
    $description => 'A better description.'
            ),
        
    'another_page' => array( 
                
    $title => 'Another Title'
                
    $keyword => 'some more keywords'
                
    $description => 'A better description.'
            )
    );
    $page 'default';
    if(isset(
    $_GET['page']) and isset($pages[$_GET['page']])) {
        
    $page $_GET['page'];
    }
    ?>
    and this is the error:

    Parse error: syntax error, unexpected ';', expecting ')' in /var/www/test/pages.php on line 6

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    Commas instead of semi-colons between each array element.
    "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
    Apr 2012
    Posts
    23
    lol

    This is driving me crazy :P

    Now im at this stage and it still doesnt work

    PHP Code:
    <?php
    $pages 
    = array(
        
    'another_page' => array( 
                
    $title 'Another Title',
                
    $keyword 'some more keywords'
                
    $description 'A better description.'
            ),
        
    'another_page' => array( 
                
    $title 'Another Title'
                
    $keyword 'some more keywords'
                
    $description 'A better description.'
            ),
        
    'default' => array( 
                
    $title 'Another Title'
                
    $keyword 'some more keywords'
                
    $description 'A better description.',
            )
    );
    $page 'default';
    if(isset(
    $_GET['page']) and isset($pages[$_GET['page']])) {
        
    $page $_GET['page'];
    }
    ?>

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    No comma after the last element of an array -- it's a separator, not a terminator.
    "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

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