Solved

header get variables (object oriented)

Posted on 2013-05-28
4
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 110

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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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