User Name
Password
AppleNova Forums » Programmer's Nook »

Question on organizing files in PHP


Register Members List Calendar Search FAQ Posting Guidelines
Question on organizing files in PHP
Thread Tools
ThunderPoit
Making sawdust
 
Join Date: May 2004
Location: Minnesota
 
2009-03-09, 09:46

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
noleli2
Senior Member
 
Join Date: May 2004
Location: Chicago
 
2009-03-09, 10:19

I'm no expert either, but I would look into the Smarty template engine. It allows you to abstract your content out of the PHP logic. I know it's the technology behind some simple content management systems.
  quote
rollercoaster375
Senior Member
 
Join Date: Mar 2005
Location: UIllinois (Champaign, IL)
Send a message via AIM to rollercoaster375 Send a message via MSN to rollercoaster375 Send a message via Yahoo to rollercoaster375 Send a message via Skype™ to rollercoaster375 
2009-03-09, 15:26

Quote:
Originally Posted by noleli2 View Post
I'm no expert either, but I would look into the Smarty template engine. It allows you to abstract your content out of the PHP logic. I know it's the technology behind some simple content management systems.
"Smarty" and "Simple" are two terms that never need to [or legitimately can] be used in the same sentence.


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
Gargoyle
http://ga.rgoyle.com
 
Join Date: May 2004
Location: In your dock hiding behind your finder icon!
 
2009-03-09, 15:39

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
ThunderPoit
Making sawdust
 
Join Date: May 2004
Location: Minnesota
 
2009-03-09, 19:49

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
ast3r3x
25 chars of wasted space.
 
Join Date: May 2004
Send a message via AIM to ast3r3x  
2009-03-13, 14:30

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:
# file or folder - serve that shit
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# forward to index.php
RewriteRule ^.*$ /index.php [NC,L]
This will forward all urls that aren't a symbolic link, file or directory to index.php. Now you have clean URLs and and handle them however you want—like say with a cool framework like zend
  quote
synotic
Member
 
Join Date: Jun 2004
 
2009-03-16, 02:11

Quote:
Originally Posted by Gargoyle View Post
What is your reasoning behind running everything through an index.php page?
It's actually a pretty common practice.

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
ast3r3x
25 chars of wasted space.
 
Join Date: May 2004
Send a message via AIM to ast3r3x  
2009-03-16, 12:11

Quote:
Originally Posted by synotic View Post
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.
You are right that you want access to the original URI, but rewriting it to a get variable is unnecessary. In PHP you can just use $_SERVER['REQUEST_URI'] to get what you care about.

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
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
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


« Previous Thread | Next Thread »

All times are GMT -5. The time now is 10:26.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2024, AppleNova