Making sawdust
Join Date: May 2004
Location: Minnesota
|
Built myself a website this weekend (www.anykeysupport.com) and decided to try running everything through the index page and have the script determine what content is displayed on the page based on the url (example: index.php?page_id=about).
Heres what I came up with: [PHP] $page_id = $_GET['page_id']; switch($page_id) { case "services": $page = "Services"; $page_content = "<h1>Services</h1>"; break; case "contact": $page = "Contact Us"; $page_content = "<h1>Contact Form</h1>\n" . "<p>Need computer service? Fill out the form below and we'll call you with an estimate ASAP!</p>\n" . "<form action=\"index.php?page_id=form_sent\" method=\"post\">\n" . "<p><label for=\"name\">Name</label><br/>\n" . "<input type=\"text\" id=\"name\" name=\"name\"/></p>\n" . "<p><label for=\"phone\">Phone Number</label><br/>\n" . "<input type=\"text\" id=\"phone\" name=\"phone\"/></p>\n" . "<p><label for=\"email\">Email Address</label><br/>\n" . "<input type=\"text\" id=\"email\" name=\"email\"/></p>\n" . "<p><label for=\"problem\">Problem Description</label><br/>\n" . "<textarea rows=\"5\" cols=\"60\" id=\"problem\" name=\"problem\"></textarea></p>\n" . "<p><input type=\"submit\" id=\"submit\" name=\"submit\" value=\"Send Email\"/></p>\n" . "</form>\n"; break; case "form_sent": include("includes/form_handler.php"); $page = "Email Sent"; $page_content = "<h1>Email Sent!</h1>"; break; case "about": $page = "About Any Key Support"; $page_content = "<h1>About Us</h1>"; break; default: $page = "Home"; $page_content = "<h1>Homepage</h1>"; } [/PHP] This seems to work fine for now, and ill probably move the content to their own includes files as i add more, but my real question is whether or not there is a better way to accomplish this? I've never done a site this way before and am about a 3 or 4 on a scale of 1-10 for php skills. |
quote |
Senior Member
|
Quote:
What you've got there is a reasonable way of doing things... If you're feeling lazy/don't like switch statements, something to consider would be along the lines of: [php]<?php if ( isset ( $_GET['page_id'] ) && file_exists( "./pages/" . $_GET['page_id'] . ".php" ) ) { require_once ( "./pages/" . $_GET['page_id'] . ".php" ); } else { $page = "Home"; $page_content = "<h1>Homepage</h1>"; } ?>[/php] Then you'd simply make a "pages" directory that contains all of the pages saved as "[page_id].php" (Ex. "about.php"). This can be a security risk if not implemented safely... But yeah, I'm guessing there's no security concern here. Oh, one last thing to note... You would have to change any include paths to refer to the root directory, if you were to use these sub-pages. (Ex. 'include("../includes/form_handler.php");' ) I really have nothing to put here, but I feel it's rather strange to not have one. |
|
quote |
http://ga.rgoyle.com
Join Date: May 2004
Location: In your dock hiding behind your finder icon!
|
First:
My advice would be to stop putting large chunks HTML inside PHP code. Second: What is your reasoning behind running everything through an index.php page? |
quote |
Making sawdust
Join Date: May 2004
Location: Minnesota
|
1st: completely agree, that html chunk currently resides in its own include file ATM.
2nd: no real reasoning, just experimenting with ideas. I figured since all my pages had an identical layout, i figured it would be easier to maintain |
quote |
25 chars of wasted space.
|
Switch to a framework. If you are a good programmer, use Zend Framework, if you are just ok, try something like CodeIgnitor.
Like others have said, stop stop stop putting HTML in PHP, you will regret it later for sure. If you aren't going to use a framework, use a url rewrite (if you can use mod_rewrite) and just do something like… Quote:
|
|
quote |
Member
Join Date: Jun 2004
|
Quote:
ast3r3x: Good idea, although I would modify the last line to something line RewriteRule ^(.*)$ /index.php?q=$1 [NC,L]. That way you know which page was requested and you can handle everything via PHP. rollercoaster: Smarty might have some initial setup but it's definitely worth it. Some kind of templating is crucial here. You basically put the Smarty install in a folder, create some folders for your template and then the Smarty class in your PHP file. It's not that bad. Thunderpoit: It'll blow your mind how much nicer it is to write templates without having to escape every quotation mark . I'd definitely give Smarty a shot. |
|
quote |
25 chars of wasted space.
|
Quote:
Thunderpoit, I will say, and I can't stress this enough, if you aren't using a framework for your php, you are not utilizing php completely. Using a framework (and there are a bunch available) will turn your makeshift php scripting into real programming with a structure and capabilities you can't compete with on your own. |
|
quote |
Posting Rules | Navigation |
|
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Leopard Newbie w/ question on large song files transfer | dicky49 | Genius Bar | 4 | 2008-05-18 21:48 |
Organizing iTunes | bts2710 | Genius Bar | 2 | 2007-11-21 13:29 |
Why Haven't we seen this?(menu/list organizing) | Wrao | General Discussion | 3 | 2006-09-30 19:52 |
.Band files -> AIFF files without losing quality | World Leader Pretend | Apple Products | 20 | 2006-03-18 02:05 |
Understanding and organizing downloaded apps | AWR | Apple Products | 31 | 2005-11-24 12:18 |