Dynamic Web pages?

Discussion in 'PHP' started by BenSeagrave, Feb 19, 2009.

  1. #1
    Hello,

    I am making a php template, I heard about dynamic web pages, I want it so that i can have a main file (index.php) then files just with content so i can include them into my index.php, But i want the includes to change when someone presses a button, so say if i was on the homepage, the content on that page would be for example home-content.php then if someone pressed "About" button on the navigation bar, I want the content to change to say about-content.php, How would i do this?

    Thanks
     
    BenSeagrave, Feb 19, 2009 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    This solution requires part .htaccess and part php. Google 'mod rewrite' for help with the specifics of using mod rewrite to reassign your 'about-content.php' urls to the index.php file.

    Here's the overview, you get your .htaccess file to catch those requests and forward them on to your index.php file with a query string (i.e. index.php?topic=about). So while 'about-content.php' is shown as the url, its really passing info to the index.php page to do the work. In the index.php file you have some code to catch what content was requested ($_GET['topic']) and spit out the correct content based on that variable.
     
    plog, Feb 19, 2009 IP
  3. my gene

    my gene Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Why .htaccess ? :confused:

    Goes like this

    
    <body>
    
    <?php 
    
    // check for _GET['page'] -> index.php?page= ...
    if (isset($_GET['page']))
    {
    	$page = trim($_GET['page']); // delete whitespace
    	
    	// check what is requested
    	switch($page)
    	{
    		case 'home': // if index.php?page=home then
    			include('home.php');
    		break; // jump out of switch()
    		
    		case 'about': // if index.php?page=about then ...
    			include('about.php');
    		break;
    		
    		default: // if something like index.php?page=blabla then
    			include('home.php');
    		break;
    	}
    }
    
    // if nothing is set, default page
    else include('home.php');
    
    ?>
    <!-- navi -->
    
    <a href="index.php?page=home">Home</a>
    <a href="index.php?page=about">About</a>
    
    </body>
    
    PHP:
     
    my gene, Feb 19, 2009 IP
  4. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Because he just said

    not index.php?page=about.
     
    djzmo, Feb 20, 2009 IP
  5. my gene

    my gene Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    How could he ask for that if he doesnt know about that -.-

    The content should change to the content which is written in the about-content.php.

    However, i guess this is what he wanted.
     
    my gene, Feb 20, 2009 IP
  6. NadeemJaz

    NadeemJaz Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    well you could use ajax to change the content on your index.php.

    Each time they click on 'about', 'contact' the ajax call request the relevant content from about-content.php for example and displays it in the innerHTML of your main content DIV.

    I think that is what he wants. To keep it index.php all the time and just have content loaded from another file.
     
    NadeemJaz, Feb 20, 2009 IP
  7. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This unnecessarily complicates a small web site. If you only have a few pages, just make your index template and copy it, rename it to aboutus.php and put the data directly into the new page.

    Unless you're anticipating a large website, just just static pages. You will find it is much easier than creating the database and putting in 10 entries just to have a dynamic site.
     
    SHOwnsYou, Feb 20, 2009 IP
  8. my gene

    my gene Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thats right but i think its good for him that he knows how to do it. The thread starter could answer if this is what he wanted ...
     
    my gene, Feb 20, 2009 IP
  9. parkerproject

    parkerproject Active Member

    Messages:
    114
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    98
    #9
    hey my gene ,i like your code. keep it up dude!
     
    parkerproject, Feb 21, 2009 IP
  10. e_hippie

    e_hippie Guest

    Messages:
    32
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    But now you're adding to the complexity of maintaining the site. Change one thing with index.php and you may likely not change it on aboutus.php.

    mod-rewrite is better.
     
    e_hippie, Feb 21, 2009 IP