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
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.
Why .htaccess ? 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:
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.
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.
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.
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 ...
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.