Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

header get variables (object oriented)

Posted on 2013-05-28
4
Medium Priority
?
326 Views
Last Modified: 2013-06-12
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
0
Comment
Question by:rgb192
4 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39204181
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
0
 
LVL 4

Assisted Solution

by:Ahmed Hussein
Ahmed Hussein earned 1000 total points
ID: 39204195
<?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


It's fairly simple, here's the explination of the above code:

first if all he defines new class variable with the same names as the $_GET paramaters:

public $u = NULL, $q = NULL;

Open in new window


And inside the __construct method, which get executed once the class is initiated, he assigns values to those class variables from the actual $_GET parameters:

 $this->u = !empty($_GET['u']) ? $_GET['u'] : NULL;

Open in new window


first he make suer that the $_GET['u'] isn't empty, and gets its value and assign it to $this->u which is in this case the class variable. but if the $_GET['u'] is empty, he basically give the class variable a value of NULL.

So if you want to add for example new header paramater called "poo" you gotta add it first as a class variable like this:

public $u = NULL, $q = NULL; $poo = NULL;

Open in new window


then inside the __construct method you have to give it the value like this:
 $this->poo = !empty($_GET['poo']) ? $_GET['poo'] : NULL;

Open in new window


That's it all.

~AG
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 1000 total points
ID: 39205956
OK  rgb192, you seem determined to do Class and Object PHP code.  I see your "Idea" for the function-purpose of your class, the first thing I will suggest is that a php Class is usually a Container for at least three or four related functions, for a couple of functions and short code, it is usually not worth the time to write a Class.

Next, you Can do all of that PUBLIC property definitions like -
 public $u = NULL, $q = NULL, $id = null, $key = null;// on and on and on

BUT with that you LOSE ALL FLEXIBILITY, for your Class, it will be Stuck in only ONE Use.
What I would do is to use a Standard PHP Array (or if you need to stay in OO then a standard Object) as a Property, and then you could use this in Many different Gets, , as you define the "GET" names with an input array.

Maybe something like this -
Class GetVars{
    public $aryGets = array(), $error  = 'Errors: ';

public function __construct($getNames){
    if(is_array($getNames)) {
    foreach($getNames as $name) {
		if(empty($_GET[$name])) $this->error .= ', NOT SET-'.$name;
            else $this->aryGets[$name] = $_GET[$name];
        }
    } else {
    $this->error .= ', $getNames is NOT array';
//You could Exception out here but that's more complex
    }
}

public function escapeGets(){
	$link = mysqli_connect('localhost', 'user', 'passw', 'dbTable');
    foreach($this->aryGets as &$value) {
        $value = mysqli_real_escape_string($link, $value);
        }
    mysqli_close($link);
    }
} // GetVars end Class


// INSTANTIATE THE CLASS
$gets = array('id','key','level','response');
$reqdata = new GetVars($gets);

// TEST FOR ACCEPTABLE DATA
if (strlen($reqdata->error) > 7) echo "Constuct ".$reqdata->error;

// IF THERE values in the array, list THE get DATA
if (count($reqdata->aryGets) > 0){
    foreach($this->aryGets as $name => $value) 
        echo "THE Gets '".$name."' VALUE IS: ".$value."<br />";
} else echo 'No GETS';

if (count($reqdata->aryGets) > 0){
    $reqdata->escapeGets(); // do mysqli escapes
}

Open in new window


This is untested code and may have typing errors, but I hope it gives you an idea about the "Process" of code that you are trying.
0
 

Author Closing Comment

by:rgb192
ID: 39241573
these code samples offer insight and explanation

thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question