Page 1 of 2

include(); Navigation

Posted: Wed Oct 08, 2008 7:54 am
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.

Re: include(); Navigation

Posted: Thu Oct 09, 2008 10:26 am
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.

Re: include(); Navigation

Posted: Thu Oct 09, 2008 12:39 pm
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.

Re: include(); Navigation

Posted: Fri Oct 10, 2008 1:38 am
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.

Re: include(); Navigation

Posted: Fri Oct 10, 2008 1:50 am
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.

Re: include(); Navigation

Posted: Fri Oct 10, 2008 10:15 am
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:

Re: include(); Navigation

Posted: Fri Oct 10, 2008 10:31 am
by rabatitat
Thanks for that. I'll look into it after I get some sleep...

Re: include(); Navigation

Posted: Fri Oct 10, 2008 10:44 am
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

Re: include(); Navigation

Posted: Mon Oct 13, 2008 6:42 pm
by Christopher
Did you find what you want? Do you want examples of converting the code above to a Front/Action Controller design?

Re: include(); Navigation

Posted: Mon Oct 13, 2008 10:30 pm
by omniuni
You could also try an elseif, it may at least clean it up a bit. I hate validation.

Re: include(); Navigation

Posted: Tue Oct 14, 2008 5:09 pm
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...

Re: include(); Navigation

Posted: Tue Oct 14, 2008 5:13 pm
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.

Re: include(); Navigation

Posted: Tue Oct 14, 2008 5:30 pm
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 ...

Re: include(); Navigation

Posted: Wed Oct 15, 2008 3:37 am
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

Re: include(); Navigation

Posted: Sat Oct 25, 2008 12:06 am
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
        }
            
        
    }
}