Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

header get variables (object oriented)

I have this in a header file which I include in every file, because I use get variables in mysql queries
also I add new variables

$id=mysqli_real_escape_string ($dbc, $_GET['id']);
$key=mysqli_real_escape_string($dbc, $_GET['key']);
$level=mysqli_real_escape_string ($dbc, $_GET['level']);
$sort =mysqli_real_escape_string ($dbc,$_GET['sort']);
$response=mysqli_real_escape_string ($dbc,$_GET['response']);
$tablename=mysqli_real_escape_string ($dbc,$_GET['tablename']);
$getinsert=mysqli_real_escape_string ($dbc,$_GET['insert']);
$gettablenameid=mysqli_real_escape_string($dbc,$_GET[$tablename.'_id']);
$getemailtemplateid=mysqli_real_escape_string($dbc,$_GET['email_template_id']);

Open in new window


so I am looking for an object oriented solution
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);


// DEMONSTRATE AN OBJECT-ORIENTED APPROACH TO HANDLING GET-METHOD REQUEST VARIABLES


Class GetVars
{
    // OUR CLASS VARS MATCH $_GET KEYS
    public $u = NULL, $q = NULL;

    // THIS IS AN EXCEPTION TRIGGER, PERHAPS?
    public $err  = 'Missing Data';

    public function __construct()
    {
        // THIS IS WHERE WE WILL FILTER THE $_GET DATA
        $this->u = !empty($_GET['u']) ? $_GET['u'] : NULL;
        $this->q = !empty($_GET['q']) ? $_GET['q'] : NULL;
        $this->id=!empty($_GET['id']) ? $_GET['id'] : NULL;
        $this->key=!empty($_GET['key']) ? $_GET['key'] : NULL;
        $this->level=!empty($_GET['level']) ? $_GET['level'] : NULL; 
        $this->sort =!empty($_GET['sort']) ? $_GET['sort'] : NULL;
        $this->response=!empty($_GET['response']) ? $_GET['response'] : NULL;
        $this->tablename=!empty($_GET['tablename']) ? $_GET['tablename'] : NULL;
        $this->getinsert=!empty($_GET['getinsert']) ? $_GET['getinsert'] : NULL;
        $this->gettablenameid=!empty($_GET['gettablenameid']) ? $_GET['gettablenameid'] : NULL;
        $this->getemailtemplateid=!empty($_GET['getemailtemplateid']) ? $_GET['getemailtemplateid'] : NULL;

        // THIS IS WHERE WE WILL TEST FOR ACCEPTABLE COMBINATIONS IN THE REQUEST
        if ($this->u && $this->q) $this->err = NULL;
    }
}

// INSTANTIATE THE CLASS
$reqdata = new Getvars();

// TEST FOR ACCEPTABLE DATA
if ($reqdata->err) echo "SOMETHING IS WRONG: $reqdata->err";

// IF THERE ARE NO ERRORS, USE THE EXTERNAL DATA
if (!$reqdata->err)
{
    echo "THE 'u' VALUE IS: $reqdata->u";
    echo "<br>";
    echo "THE 'q' VALUE IS: $reqdata->q";
}

Open in new window



I dont fully understand this modified code sample and how to add new get header variables
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I don't even know where to begin.  Maybe get this book and learn about PHP object oriented design patterns?
http://www.amazon.com/Objects-Patterns-Practice-Experts-Source/dp/143022925X/

Or follow this search:
https://www.google.com/search?q=PHP+object-oriented+design
SOLUTION
Avatar of Ahmed Hussein
Ahmed Hussein
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

these code samples offer insight and explanation

thanks