Script Programming

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.

Moderator: General Moderators

Post Reply
wizzard81
Forum Commoner
Posts: 66
Joined: Wed Jan 15, 2003 6:05 am
Location: Belgium
Contact:

Script Programming

Post by wizzard81 »

nigma | Help us, help you. Please use

Code: Select all

and

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

and

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]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
wizzard81
Forum Commoner
Posts: 66
Joined: Wed Jan 15, 2003 6:05 am
Location: Belgium
Contact:

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
wizzard81
Forum Commoner
Posts: 66
Joined: Wed Jan 15, 2003 6:05 am
Location: Belgium
Contact:

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
Post Reply