include(); Navigation

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

rabatitat
Forum Newbie
Posts: 11
Joined: Wed Oct 08, 2008 7:16 am

include(); Navigation

Post by rabatitat »

I'm not really well-versed in PHP since my coding background isn't web based.

I have this new project that has to be platform-independent hence the shift to web application development.

Now the I want the feel of the site to be more like your usual binary app... The problem is with PHP/HTML being mostly stateless.

I am using include(); navigation on my forms and pages primarily for input validation (it's a hassle if you have to return to the previous page every time you make an input error) and my forms have 5 - 10 text input fields which have to be validated.

After doing the first 5 forms I began to feel uncomfortable with the code I was putting out:

Here's a snippet of the main page that handles all the actions on the buttons of the forms:

Code: Select all

 
switch (true) { 
    /*Users*/ 
    case isset($_POST['cmdAddNewUser']): 
        $_SESSION['success'] = false; 
        include("adduser.php"); 
        break; 
    case ($_POST['cmdValidateUserAdd']): 
        include("adduser.php"); 
        break; 
    case isset($_POST['cmdAddUserSave']): 
        $qrySaveNewUser = sprintf("INSERT INTO tblUsers VALUES ". 
            "(NULL, '%s', AES_ENCRYPT('%s', MD5('%s')), ". 
            "'%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');", 
            mysql_real_escape_string($_SESSION['user_name']),  
            mysql_real_escape_string($_SESSION['user_pass']), 
            mysql_real_escape_string($_SESSION['key']), 
            mysql_real_escape_string($_SESSION['access_level']), 
            mysql_real_escape_string($_SESSION['salutation']), 
            mysql_real_escape_string($_SESSION['first']), 
            mysql_real_escape_string($_SESSION['mi']),
            mysql_real_escape_string($_SESSION['last']),
            mysql_real_escape_string($_SESSION['birthdate']), 
            mysql_real_escape_string($_SESSION['street']),
            mysql_real_escape_string($_POST['city_id']));
        if (!$_SESSION['success']) {
            if (mysql_query($qrySaveNewUser)) {
                $_SESSION['success'] = false;
            }
        }
        include("showusers.php");
        break;
    case isset($_POST['cmdAddUserCancel']):
        include("showusers.php");
        break;
    case isset($_POST['cmdEditUser']): 
        switch (true) {
            case isset($_POST['id_no']):
                $_SESSION['id_no'] = $_POST['id_no'];
                $_SESSION['success'] = false;
                include("edituser.php");
                break;
            default:
                include("showusers.php");
                break;
        }
        break;
    case isset($_POST['cmdValidateUserEdit']):
        include("edituser.php");
        break;
 
Now what I get uncomfortable with is the repetitive

Code: Select all

 
case isset($_POST['foo']):
    include("bar.php"); 
    break;
 
It's just one big GoTo *cringe* system now... :banghead:

The code works fine but it's a bitch to maintain, not to mention the problems that would crop up with CSS... It's gone up to 800 lines now and I'm not even halfway through the forms yet. 8O

I've decided not to use javascript for anything more than layout solutions.

Your thoughts and comments would be greatly appreciated.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: include(); Navigation

Post by aceconcepts »

In my experience I think it's just one of the lengthy parts of validation.

Obviously it's very useful to make use of functions where you can.

You can get the forms method from the server using $_SERVER['REQUEST_METHOD'] but this doesn't indicate the form element i.e. $_POST['cmdAddNewUser'].

My advice would be to use functions.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: include(); Navigation

Post by Christopher »

rabatitat wrote:It's just one big GoTo *cringe* system now... :banghead:

The code works fine but it's a bitch to maintain, not to mention the problems that would crop up with CSS... It's gone up to 800 lines now and I'm not even halfway through the forms yet. 8O

I've decided not to use javascript for anything more than layout solutions.

Your thoughts and comments would be greatly appreciated.
Front Controller.
(#10850)
rabatitat
Forum Newbie
Posts: 11
Joined: Wed Oct 08, 2008 7:16 am

Re: include(); Navigation

Post by rabatitat »

aceconcepts wrote:In my experience I think it's just one of the lengthy parts of validation.

Obviously it's very useful to make use of functions where you can.

You can get the forms method from the server using $_SERVER['REQUEST_METHOD'] but this doesn't indicate the form element i.e. $_POST['cmdAddNewUser'].

My advice would be to use functions.
I can't really figure out how to do it without lengthening it further since I'd have to pass the same button names as arguments. The functions that I use for the validations are in another foo.php file which is included at the start of the main page and they are called by the form page bar.php in the

Code: Select all

include("bar.php");
statements.

I also can't homogenize the buttons names into common ones since they belong to different forms ergo have different actions.
Last edited by rabatitat on Fri Oct 10, 2008 1:55 am, edited 1 time in total.
rabatitat
Forum Newbie
Posts: 11
Joined: Wed Oct 08, 2008 7:16 am

Re: include(); Navigation

Post by rabatitat »

arborint wrote:
rabatitat wrote:It's just one big GoTo *cringe* system now... :banghead:

The code works fine but it's a bitch to maintain, not to mention the problems that would crop up with CSS... It's gone up to 800 lines now and I'm not even halfway through the forms yet. 8O

I've decided not to use javascript for anything more than layout solutions.

Your thoughts and comments would be greatly appreciated.
Front Controller.
If you're implying the code then, that is what it is, a controller. I just need to know if there's a better way of skinning this particular cat than what I currently am doing.

If not, then please be more specific...

Thank you all for you replies.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: include(); Navigation

Post by alex.barylski »

A controller and a front controller are different things. I imagine arborint is just tired of explaining the differences over and over again so for the sake of brevity he offered you some key words instead. LOL

Sorry I haven't slept yet...I'm going on 35 hours of non-stop go go...

Go read up on the front controller, look at examples of others. A switch statement although conceptually like a front in the sense you sorta centralize aciton handling...the switch is where it stop.

Controllers are the case statements so in reality I guess a switch/case is like a front controller and a controller all in one which would be nasty coupling.

I'm losing myself though I need sleep :drunk:
rabatitat
Forum Newbie
Posts: 11
Joined: Wed Oct 08, 2008 7:16 am

Re: include(); Navigation

Post by rabatitat »

Thanks for that. I'll look into it after I get some sleep...
rabatitat
Forum Newbie
Posts: 11
Joined: Wed Oct 08, 2008 7:16 am

Re: include(); Navigation

Post by rabatitat »

Seems I won't be getting any sleep after all... Just Googled Front Controller and I might be in for another long read...

Thank you very much for pointing me in the right direction. :D
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: include(); Navigation

Post by Christopher »

Did you find what you want? Do you want examples of converting the code above to a Front/Action Controller design?
(#10850)
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: include(); Navigation

Post by omniuni »

You could also try an elseif, it may at least clean it up a bit. I hate validation.
rabatitat
Forum Newbie
Posts: 11
Joined: Wed Oct 08, 2008 7:16 am

Re: include(); Navigation

Post by rabatitat »

arborint wrote:Did you find what you want? Do you want examples of converting the code above to a Front/Action Controller design?
No I haven't really found something I could apply to my problem since the server this thing is gonna live on won't allow me to use .htaccess files...

I'd really appreciate links to/or examples re: this.

Thanks in advance...
rabatitat
Forum Newbie
Posts: 11
Joined: Wed Oct 08, 2008 7:16 am

Re: include(); Navigation

Post by rabatitat »

omniuni wrote:You could also try an elseif, it may at least clean it up a bit. I hate validation.
I don't think so... It's still the same thing and actually longer.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: include(); Navigation

Post by Christopher »

rabatitat wrote:No I haven't really found something I could apply to my problem since the server this thing is gonna live on won't allow me to use .htaccess files...

I'd really appreciate links to/or examples re: this.
If I get some time I will convert your example above to Action Controllers to give you an example of one possible direction ...
(#10850)
rabatitat
Forum Newbie
Posts: 11
Joined: Wed Oct 08, 2008 7:16 am

Re: include(); Navigation

Post by rabatitat »

arborint wrote:
rabatitat wrote:No I haven't really found something I could apply to my problem since the server this thing is gonna live on won't allow me to use .htaccess files...

I'd really appreciate links to/or examples re: this.
If I get some time I will convert your example above to Action Controllers to give you an example of one possible direction ...
Thanks. :D
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: include(); Navigation

Post by josh »

Code: Select all

 
<?php
 
class userController extends MVC_Controller
{
    function cmdAddNewUserAction()
    {
        $_SESSION['success'] = false; 
        $this->_addUser(); 
    }
    
    function cmdValidateUserAdd()
    {
        $this->_addUser();
    }
    
    protected function _addUser()
    {
        # code stuff goes here
        # instead of
        #include("adduser.php"); 
    }
    
    protected function _AddUserSave()
    {
        $newValues = $form->get();
        
        // this would be a 'model' in the MVC pattern
        $user = Data_Mapper::getUser();
        
        // the following code would be using MVC proceduraly
        /*$user -> attribute1 = $newValues[0]
            ->attribute2=$newValues[1]
            ->attribute3=$newValues[2];*/
            
        // - or instead of above - 
        if($form->validate())
        {
            $user ->setValues( $form->get() );
        }
        else
        {
            // take action
        }
            
        
    }
}
 
Post Reply