Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Hello,
I have to make a little manage script. I have a question is this a goodway of programming like this=>
switch ($page) {
case "add" :
include ('special_add.php');
break;
case "edit" :
include ('special_edit.php');
break;
default:
...
}
or is it a better to work with different files for everything not with the switch? I have to make it easy so my partner can work with the files in the future also.
Every file like special_add.php has also a form submit script to submit data.
As you can probably tell from previous threads of this nature it is really down to your own personal preference. Just use what you are more comfortable with and feel will be easier to read and maintain. If you are really fussy then do some time checks to see which method produces the fastest output.
// index.php
// configuration stuff - put in config.php
$path = './';
$ext = '.php';
$default = 'home';
$page = preg_replace('/[^a-zA-Z0-9\_]/', '', $_REQUEST['page']);
$result = false;
if ($page) {
$result = @include ($path . $page . $ext);
}
if (! $result) {
include ($path . $default . $ext);
}
From there you could call pages that may have hard coded switch logic in them. An example is a page that shows a form and then the search results like this:
// mypage.php
$mode = preg_replace('/[^a-zA-Z0-9]/', '', $_REQUEST['mode']);
// put code here to check for errors, validate form input, etc.
if ($errors) {
$mode = 'showform';
}
switch ($mode) {
case 'showresults' :
show_results();
break;
case 'showform' :
default:
show_form($errors);
}
function show_form ($errors)
{
}
function show_results ()
{
}
The top code is a general page loader and is a simple version of what the Fancy OO Pattern People call a Front Controller. The second one is a page that has several related modes. It is a simple version of what is sometimes called a Page Controller. Note that the second page can be called on its own with just:
These are very simple and rough, but if you add error handling they might be usable. You can also turn these things into classes. There are several reasons you might want to do that.
It depends on you programming style and the problem you are trying to solve whether you use none of the above, one or both. You can write a good program all three ways.
thanks guys for the great info. Just a little question i'm planning to make a simple scripts for adding data into a database but i want to work via one database for all my projects.
Do you know if their are tutorials for that kind of programming? So you have like 10 different projects(sites) with one database and a script installed on every project. But the problem i have is i want to hide the database connection so customers cannot see the db connection.
A database holds tables, and a table holds the data. So as long as you have 1 database you can create as many tables as you wish and so use it for 100's of projects. Just come up with some kind of naming convention like "projectname_table" so that you can identify which tables belong to each project.
The users cant see the database connection as long as the page is parsed, e.g. name the script .php instead of .inc so that it will always get parsed by php before the user see it, so they cant get your user/password details. Other than that the user shouldnt see any other connection to the database - as you just send them the results.
well let me explain. I have customers with own webhosting but they will rent the script. So i want they cannot see the database connection details. I only want they can use the script to add things to the database or showing the data from the script.
They have to have access to the database details in order to connect if you do it that way. Or you can create a series of middle man scripts that would insert and select the data on your host, and then the client using your service would communicate to these scripts in order to get and set data. The scripts could then return the results in xml or something.