Page 1 of 1
Script Programming
Posted: Tue Oct 26, 2004 9:48 am
by wizzard81
nigma | Help us, help you. Please use Code: Select all
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=>
Code: Select all
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.
nigma | Help us, help you. Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Tue Oct 26, 2004 10:48 am
by timvw
this thread will end in a "frontpage controller is good/bad" discussion.
you might want to lookup the many threads before in this forum on that subject.
Posted: Tue Oct 26, 2004 12:04 pm
by BDKR
timvw wrote:this thread will end in a "frontpage controller is good/bad" discussion.
you might want to lookup the many threads before in this forum on that subject.
LOL!!! You are right.
Posted: Tue Oct 26, 2004 12:38 pm
by kettle_drum
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.
Posted: Tue Oct 26, 2004 5:47 pm
by Christopher
You can use hard coded switch or something like this
http://www.mysite.com?page=mypage
Code: Select all
// 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:
http://www.mysite.com?page=mypage
http://www.mysite.com?page=mypage&mode=showform
http://www.mysite.com?page=mypage&mode=showresults
Code: Select all
// 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:
http://www.mysite.com?mode=showform
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.
Posted: Wed Oct 27, 2004 6:33 am
by wizzard81
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.
Posted: Wed Oct 27, 2004 8:16 am
by kettle_drum
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.
Posted: Wed Oct 27, 2004 1:20 pm
by wizzard81
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.
Posted: Wed Oct 27, 2004 1:55 pm
by kettle_drum
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.