Jump to content
xisto Community
Sign in to follow this  
minnieadkins

Getvariables Class

Recommended Posts

I'm sorta new to php/mysql and I have been learning some over the past month. I was taking a class and one of my professors helped me with this class for PHP 5. Just was seeing what you thought, and how many people use classes like this? Simple get variables class.

<?phpclass GetVars{    public $vars = array();      	public function __construct($my_get_vars,$my_post_vars,$request_method)  	{       if($request_method=="GET")      	$my_web_vars = $my_get_vars;      else      	$my_web_vars = $my_post_vars;    	foreach($my_web_vars as $key=>$value){                $this->vars[$key] = $value;        }//!End of foreach loop  	}//!End of constructor  	public function build_form($formName,$method, $script){      echo "<form name=\"$formName\" method=\" $script\" action=\"$script\">";           $this->build_form_elements();            echo "</form>";    	  	}//!End of function build_form()  	public function build_form_element($element){            if($this->vars[$element]){             if(is_array($this->vars[$element])){                foreach($this->vars[$element] as $value){                 echo "<input type='hidden' name='" . $element . "[]'	value='" . $value. "' />";            	}//!End of foreach             }else{	                 echo "<input type=\"hidden\" name=\"$element\" value=\"$this->vars[$element]\" />";          }//!End of if(is_array)           }//!End of if()  	}//!End of function build_form()  	public function build_form_elements(){       foreach($this->vars as $key=>$value){           if(is_array($value)){             foreach($value as $arrValue){              echo "<input type='hidden' name=" . $key . "[] value=" . $arrValue . " />";            }//!End of foreach($value as $arrValue)          }else{	          echo "<input type=\"hidden\" name=\"$key\" value=\"$value\" />";       }//!End of if()      }//!End of foreach($this->vars as $key=>$value)   	}//!End of function build_form()  	public function set_default($var, $value){      if(!isset($this->vars[$var])){         $this->vars[$var] = $value;      }      	}//!End of function set_default  	public function __get($varname){      if(array_key_exists($varname, $this->vars) )      {   return $this->vars[$varname]; }  	}//!End of function __get  	public function __set($varname, $value){      $this->vars[$varname] = $value;       return true;  	}//!End of function __set}//!end of class?>

It's a simple class that you can use to deal with form variables, whether POST or GET. For instance if you pass variables a lot using hidden form fields, it's kind of handy to build those fields for you.

Example:
<html><head  title  body blah balh><?phpinclude("thisclass");$vars = new GetVars($HTTP_GET_VARS, $HTTP_POST_VARS, $_SERVER['REQUESTMETHOD']);//you could use build_form or build_form_elements here?><form name="formname" action="somescript" method="POST"><?php build_form_elements(); ?></form></body></html>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.