Pregunta

I am working on a dynamic blog/portfolio:

Ref Link: http://www.andrewryan.me/

I have been working on my personal website for more than a month now. I keep modifying things because I feel like I am setting up this entire thing wrong. I know I am in some way or another and I am finally asking for help.

I have been working on a dynamic site structure and I would like to know a few things:

  1. Best practices' for dynamic content.

    • I have the frame of the site in the root folder. This includes the index and styles.
      (along with the scripts folder, images folder and other misc file types..)

    • The newest blog entries are pulled up on the index page. If they wanted to see all my professional posts/topics they'd just go to any given sub section.
      (e.g: index.php?p=professional)

    • From there; If a user wanted to view a blog post in it's entirety, they'd just click on the post and then that's further-dynamically pulled up.
      (e.g: index.php?p=professional/thispost (idfk))

  2. How to create dynamic content inside of dynamic content.

    • Pages and SubPages then continue on down the chain within the personal folder. (e.g: index.php > index.php?p=personal)
  3. How to make updatable content that will file in once a page is uploaded to it's respective folder.

    • Should I be looking into using a database or XML for this? And if so, can anyone point me to any good tutorials on making this type of website? No need to explain too much on this.

Here's my file structure:

root/   
  > images/  
    ||--> x.png   
  > pages/  
    ||--> personal.php  
    ||--> professional.php  
    ||--> contact.php   
    ||--> 404.php  
    ||--> contact.php 
    > personal/  
        |||--> personalposts.php  
    > professional
        |||--> professionalposts.php  
    > gallery/  
        |||--> galleryposts.php   
  > scripts  
    ||--> scripts.php/js/etc
  |--> .htaccess
  |--> index.php  
  |--> style.css    
  |--> robots.txt  
  |--> humans.txt

...and the code for dynamic pages:

<?php
    $pages_dir = 'pages'; //Scans the pages directory

    if (!empty($_GET['p'])) { //If not empty it will get the file
        $pages = scandir ($pages_dir, 0); //Sets up files as array
        unset ($pages[0], $pages[1]); //Unsets the first two. These are just dots.
        $p = $_GET['p'];

        if (in_array($p.'.php',$pages)) {
            include ($pages_dir.'/'.$p.'.php');
        } else {
        include($pages_dir.'/404.php'); //DENIED!
        }
    } else {
        include($pages_dir.'/blog.php'); //if it's the index page
    }
?>  

I more or less want to know if I got the structure down right or if there's a better way to do this. I'd like to be able to make this website once and just auto-update it content-wise when needed.

¿Fue útil?

Solución

Aha, the question I have been waiting for. I'm working on a site that does just this. First, you got to separate your site into pages (each page has a different layout). For example, to logout, you would have something like this: sitename.com/?action=logout

To go home, you could go to sitename.com, if you try /?p=somethingThatDoesntExist then you still get to the homepage (or an error page)

If you have access to something underneath your web root, then it's much better. Then, you should keep all .php files in /root/content/ and only one index.php in /root/www/

This code is what I use to give the player a page if he is logged in (and optionally if he is an admin)

// Check if alternate page requested
if (!empty($_GET["p"]) and !empty($_SESSION['id']))
{
    $page = $_GET["p"];

    if (!empty($pages[$page]))
    {
        $page_array = $pages[$page];
        if ( (!empty($page_array['auth'])) && ($user['Auth'] < $page_array['auth']) )
        {
            $page_array = $pages['index'];
        }
    }
    else
    {
        $page = '';
    }
}

Then, you do your basic stuff:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang='en'>  
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
        <title><? echo $page_array['title'] ?></title>

        <link rel="stylesheet" href="css/3p/reset.css" type="text/css"/>
        <link rel="stylesheet" href="css/3p/formalize.css" type="text/css"/>

        <link href="css/bootstrap.css" rel="stylesheet">
        <link href="css/main.css" rel="stylesheet">
        <link href="css/bootstrap-responsive.css" rel="stylesheet">
    </head>  
    <body>  
            <?php
                    require("../content/header.php");
                    require($page_array['require']);
                    require("../content/footer.php");
            ?>
    </body>
</html>

Here are the contents of page_array:

$pages = Array();
$pages['admin'] = Array(
    'auth' => 3,
    'require' => "../admin/index.php",
    'title' => "Administration - NPG CP"
);

$pages['index'] = Array(

    'require' => "../content/index.php",
    'title' => "Home - NPG CP"
);

$pages['friends'] = Array(

    'require' => "../content/index.php",
    'title' => "Friends - NPG CP"
);

$pages['pm'] = Array(

    'require' => "../content/index.php",
    'title' => "Messages - NPG CP"
);

$pages['stats'] = Array(

    'require' => "../content/index.php",
    'title' => "Statistics - NPG CP"
);

On my site, I only use one viewable file that handles everything else. I hope I helped you. I can give you the source code to my site, just email me at [my_username]@gmail.com for it :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top