Solved

header get variables (object oriented)

Posted on 2013-05-28
4
291 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 108

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 250 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 33

Accepted Solution

by:
Slick812 earned 250 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now