General PHP Program Flow Control

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mazdaboi
Forum Newbie
Posts: 3
Joined: Mon Aug 11, 2003 3:23 pm

General PHP Program Flow Control

Post 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
Last edited by mazdaboi on Mon Aug 11, 2003 4:05 pm, edited 1 time in total.
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Well I can't tell based on your snippet, but you should definitely INDENT your code :)
mazdaboi
Forum Newbie
Posts: 3
Joined: Mon Aug 11, 2003 3:23 pm

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
mazdaboi
Forum Newbie
Posts: 3
Joined: Mon Aug 11, 2003 3:23 pm

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