Suggestions on how to separate my HTML from PHP "machinery"
Posted: Tue Jun 22, 2010 11:46 am
Well, I know this is a common problem, but I've searched through the forums and haven't gotten any clear guidance, so I'm posting. However, because I'm sure this has been addressed before, all I'm asking for are some pointers to some good articles on how to solve this particular issue. If you want to elaborate *more* in this thread, I'm all ears, but I'm not expecting anybody to do into much detail since, as I said, I'm sure others have presented this issue in the past.
I'm working on a PHP-driven site for a client. It's a fairly standard DB-driven site for letting teachers give quizzes to students. So, the DB has schools, and the schools contain teachers, teachers contain "classes", and the classes have students and assignments, etc. Nothing new, there.
The way the back-end is put together is pretty much that each "role" (ie, student, teacher, administrator) has their own php file. So, for example, the administrator's stuff would be in "admin.php". The particular tasks that an administrator does are invoked with an "action" value passed in the querystring. Something like "admin.php?action=delete_teacher&teacherid=82". Then, in admin.php, there's just this huge switch clause that looks like:
Nothing new here, right? Seen this a million times. Here's what's different from what I've done in the past. The original author fashioned a quazi-templating solution by putting most of the non-dynamic content into included files which end in ".inc". Those are basically HTML files which toss in certain PHP variables at certain places. For example:
Get it? Preload some variables, include the template, and have the template fly the variables into the appropriate places. It's a little different from how I've done some PHP stuff in the past, where the URL's pointed to HTML pages and the pages made calls to the PHP logic. Here, it's the other way around. The main entry point is at the PHP files and the PHP calls upon the HTML. But... whatevah. I guess this is a little more "MVC"... but not much.
So, what's my big problem?
First off, because the dude decided to name these files ".inc", Dreamweaver doesn't interpret them as HTML. I'd love for my client to be able to manage all of her images, layout, style-sheets (everything except the business logic, I guess) herself, but Dreamweaver just looks at these things like straight text files. It also means that we can't preview what the page is going to look like until we just upload it to the testing server and reload the page in the browser. I guess I could just rename them to .html files but... now that I think about it, I guess I don't know why I don't. I guess I'm just asking here to see if anybody can see of any pitfalls of doing that (or if they have any better ideas to make it easy to design/manage the templates in DW).
It would also be nice if I could employ a less-insane method of page flow, or site navigation, or whatever the preferred term is for it. Right now, I have to put URL's in explicitly, like:
....You've just deleted a teacher. <a href="admin.php?action=main">Click here</a> to return to the main admin screen
So, there are jillions of these things all over the place. If we renamed admin.php or renamed the "main" action to something else, it would break a bunch of stuff and we wouldn't know if we tracked down all of the places where we had to change it. Of course, I could start rewriting the code to use a bunch of global URL/action constants, like
....You've just deleted a teacher. <a href=\"" . getURL("ADMIN_MAIN") . "\">Click here</a> to return to the main admin screen
But, that's not much of an improvement. I'm thinking that the solution is some combination of a smarter IDE (kinda on the order of IDEA for Java) and a more-sensible approach to warehousing names of the php files and their actions, but I want to avoid re-inventing the wheel, here.
What have y'all used to make sense of things when a site starts getting unruly like this?
- Joe
I'm working on a PHP-driven site for a client. It's a fairly standard DB-driven site for letting teachers give quizzes to students. So, the DB has schools, and the schools contain teachers, teachers contain "classes", and the classes have students and assignments, etc. Nothing new, there.
The way the back-end is put together is pretty much that each "role" (ie, student, teacher, administrator) has their own php file. So, for example, the administrator's stuff would be in "admin.php". The particular tasks that an administrator does are invoked with an "action" value passed in the querystring. Something like "admin.php?action=delete_teacher&teacherid=82". Then, in admin.php, there's just this huge switch clause that looks like:
Code: Select all
switch($_GET['action']) {
case "delete_teacher":
blah blah blah...
break;
...Right? So, it's just a static HTML file that throws in some variables from PHP. So, the actual case statement for the "delete_teacher" action in admin.php would look something like:delete_teacher.inc<html><head><title><%php echo $pagetitle %></title></head>
<body><font size="+2" color="#00FF00"><%php echo $maincontent %></font></body>
</html>
Code: Select all
switch($_GET['action']) {
case "delete_teacher":
blah blah blah...
$pagetitle="Deleting teacher number $teacherid";
$maincontent="... blah blah blah... ";
include("templates/delete_teacher.inc");
exit();
break;
...So, what's my big problem?
First off, because the dude decided to name these files ".inc", Dreamweaver doesn't interpret them as HTML. I'd love for my client to be able to manage all of her images, layout, style-sheets (everything except the business logic, I guess) herself, but Dreamweaver just looks at these things like straight text files. It also means that we can't preview what the page is going to look like until we just upload it to the testing server and reload the page in the browser. I guess I could just rename them to .html files but... now that I think about it, I guess I don't know why I don't. I guess I'm just asking here to see if anybody can see of any pitfalls of doing that (or if they have any better ideas to make it easy to design/manage the templates in DW).
It would also be nice if I could employ a less-insane method of page flow, or site navigation, or whatever the preferred term is for it. Right now, I have to put URL's in explicitly, like:
....You've just deleted a teacher. <a href="admin.php?action=main">Click here</a> to return to the main admin screen
So, there are jillions of these things all over the place. If we renamed admin.php or renamed the "main" action to something else, it would break a bunch of stuff and we wouldn't know if we tracked down all of the places where we had to change it. Of course, I could start rewriting the code to use a bunch of global URL/action constants, like
....You've just deleted a teacher. <a href=\"" . getURL("ADMIN_MAIN") . "\">Click here</a> to return to the main admin screen
But, that's not much of an improvement. I'm thinking that the solution is some combination of a smarter IDE (kinda on the order of IDEA for Java) and a more-sensible approach to warehousing names of the php files and their actions, but I want to avoid re-inventing the wheel, here.
What have y'all used to make sense of things when a site starts getting unruly like this?
- Joe