Page 1 of 1

General PHP Program Flow Control

Posted: Mon Aug 11, 2003 3:23 pm
by mazdaboi
Hello all,

I have been programming with PHP for a while now, but nothing too outrageously advanced... However, I am working on a medium sized database front-end now, and thought I'd ask other developers a question or two...

Right now I use hidden form tags to control the flow of my programs... But it's getting sort of messy... Basically I set hidden fields with certain values, and use a structure like this:

Code: Select all

if (empty($get_majorfunction)) {
    // No function chosen
} elseif ($get_majorfunction == "view") {
    if (empty($get_minorfunction)) {
        // View majorfunction, but no minor function
    } elseif ($get_minorfunction == "select") {
        // Display a form to select account to view
    } elseif ($get_minorfunction == "submit") {
        // User supposedly selected acct, submit the info
    }
}
This works fine on small projects... And I'm sure there are developers here that will say that it works fine for large projects too...

But I just cant seem to keep track of it mentally once it goes more than a majorfunction/minorfunction in... I'm to the point where I am using major/middle/minor functions...

I have considered several other alternatives:

1) Continue to use the hidden form fields to control flow
2) Use cookies, which I view as sort of a messy solution
3) Use PHP sessions and keep the major/minor function variables
on the server side which still leaves me with basically the same
structure, but just storing the variable values on the server side.

Does anyone have an elegant solution that they would recommend to a programmer who hasnt done many major projects?

Thanks,
Brad

Posted: Mon Aug 11, 2003 4:00 pm
by qartis
Well I can't tell based on your snippet, but you should definitely INDENT your code :)

Posted: Mon Aug 11, 2003 4:06 pm
by mazdaboi
Oops... I forgot to add the bbCode tags... Been a while since Ive posted on a forum. lol... In any case, I have corrected my oversight. Sorry bout that.

Posted: Mon Aug 11, 2003 6:40 pm
by McGruff
When you say page flow I assume you mean navigation from page to page?

The standard way is to set hyperlinks with [..relative or absolute path to..]/filename.php?var1=value1&var2=value2..etc. That selects a php file and sets some GET vars, so you can call any script and feed it the vars it requires (urlencode any text values..).

A variation I started using when I took my first steps with php is:

[site root]/index.php?value1/value2

..which creates cleaner urls and just needs a bit of exploding to separate out the values.

Some search engines can choke on the ? in a query string, and this is just a step away from the mod_rewrite trick - http://www.alistapart.com/stories/urls/. I don't think ? is a big issue with SEs nowadays - but it might be worth doing if you prefer shorter, easy to remember urls.

I hate to think how SEs would deal with site nav based on hidden form fields - but maybe I misunderstood your question.

Posted: Mon Aug 11, 2003 10:03 pm
by nielsene
I would say stop sticking all the functions on a single page. For instance if you make one page for each "majorfunction", then you only need to have one level of if elses to handle the minorfunctions, etc.

Posted: Wed Aug 13, 2003 2:07 pm
by mazdaboi
Thanks for the tips. Both ideas are good. I'll go ahead and give them both a shot and see which works better for me.