Help - Search - Members - Calendar
Full Version: Got myself in a mess
Invision Power Services > Community Forums > Community Web Design and Coding
Kenny Pollock
Well, I got overly excited about a new project... got to coding and got a lot done. I didn't plan anything, and now I have a mess of code.

I just went in and cleaned it up a bit, but it's not even working properly because it's so inefficient. I don't even remember how to do what I want to do.

I have pages that the user can view if $logged_in = '1' and if $_GET['action'] == 'pagetheywant'. I figured I could probably do
CODE
if ( $logged_in == '1' )
{
switch ( $_GET['action']

and do it that way, but then something else comes into play...

A lot of these pages are forms you fill out; register, update profile, etc.
I'm current doing:
CODE
elseif ( $_GET['action'] == 'register' )
{
    include './templates/register.html';    
}
elseif ( $_POST['register'] )
{
    $first_name = addslashes( $_POST['first_name'] );

and can't think of a better way.

Anyone?!?
Brendon Koz
I believe you have two options:
1.) ...use the $_REQUEST variable which will take information from $_GET, $_POST, and I think even $_COOKIE (though I'm not entirely sure about cookies). (i.e.: $_GET['request'], $_POST['test'] == $_REQUEST['request'], $_REQUEST['test']) -- if you have two variables of the same name, it will take one over the other and I do not know which takes precedence.
2.) ...use extract() or import_request_variables(). Your order of precedence problem will still occur.

Since you're taking input from $_GET and $_POST you'll want to make sure you validate that the data you're receiving is definately what you're expecting in these variables.
Stephen
QUOTE(malikyte @ Mar 24 2006, 06:19 AM) *
if you have two variables of the same name, it will take one over the other and I do not know which takes precedence.


Depends on the "variables_order" setting in PHP.ini, but the default in $_ENV, $_GET, $_POST, $_COOKIE, $_SESSION (EGPCS), registration is done from left to right so post takes precedence over get, but cookie takes precedence over post.
Kenny Pollock
QUOTE(malikyte @ Mar 24 2006, 01:19 AM) *
I believe you have two options:
1.) ...use the $_REQUEST variable which will take information from $_GET, $_POST, and I think even $_COOKIE (though I'm not entirely sure about cookies). (i.e.: $_GET['request'], $_POST['test'] == $_REQUEST['request'], $_REQUEST['test']) -- if you have two variables of the same name, it will take one over the other and I do not know which takes precedence.
2.) ...use extract() or import_request_variables(). Your order of precedence problem will still occur.

Since you're taking input from $_GET and $_POST you'll want to make sure you validate that the data you're receiving is definately what you're expecting in these variables.

Thanks you! I'll try using request and let you know how it goes.
Kenny Pollock
Hmm, wait... confused myself. I thought I had it but when I went to code I realized I didn't.

switch( $_REQUEST)? What do I use for cases? Or were you thinking of another method of sorting the pages besides switch()?
Kenny Pollock
Figured everything out how I wanted it.

Now I have a question...

I want to show a user's school that's in a database, in a drop down.
In profiles table, there is school_id field that has the id # of their school.
In schools table, there is the same school_id along with a school name.

I came up with the following, with no luck. I want to get $school_r['school_id'] and $school_r['name'] out of $value.

CODE
$school_query = mysql_query( "SELECT profiles.school_id, schools.name FROM profiles LEFT JOIN schools ON profiles.school_id = schools.id WHERE profiles.id = '" . $user_id . "'" );

while ( $school_r = mysql_fetch_array( $school_query ) )
{
    echo "\t" . '<option value="' . $school_r['school_id'] . '">' . $school_r['name'] . '</option>' . "\n";

    foreach ( $school_r as $value )
    {
        if ( $value != $school_r['school_id'] )
        {
            echo "\t" . '<option value="' . $value . '">' . $value . '</option>' . "\n";
        }
    }
}


Any advice?
Grant
Remember, when you use the foreach system, the $value is still an array. So you would need to compare it as such
CODE
if( $value['school_id'] != $school_r['school_id'] )


when you use foreach( $whatever as $value ) remember that its identiity is never broken... Its always consistent with the last variable passed to it ( $value ) so if $whatever is a float number then $value will be a float number... if $whatever is an array, $value is also an array.
Kenny Pollock
NEVERMIND: FIXED.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.