Storing PHP Code in an Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
neojawbreaker
Forum Newbie
Posts: 3
Joined: Mon Jul 19, 2010 7:25 pm

Storing PHP Code in an Array

Post by neojawbreaker »

I want to store PHP code in a 2 dimensional array to be recalled later but I am having trouble. Basically my array looks like this. . .

Code: Select all

//  var_name = name of variable used in form
//  var_type = type of variable (integer, string, bool, etc.)
//  form_code = html code used in search form
//  php_code = php code used in checking the variable 

//  $var_list = array( array( "name" => "var_name", "type" => "var_type_int_str_bool", "form_code" = "html_code" , "php_code" => "php code") );

$name = "zip";
$type = "INT";
$form_code = $name.': <input type="text" name="'.$name.'" class="search" />';
$php_code = '$row[\'name\'] : $_SESSION[$name]';  //HAVING PROBLEMS HERE
$zip_code = array( "name" => $name, "type" => $type, "form_code" => $form_code, "php_code" => $php_code );

$var_list = array($zip_code);
main_search( $var_list );
I want to use the PHP code in $php_code to format how the value will be returned so, using age as an example, I can check if the user is searching with just a min age, just a max age, or with both a min age and max age. The function main_search() is provided below. . .

Code: Select all

//  This is the code for the searchbox.  Basically, it checks to see what criteria are being searched by
//  and gives a list current criteria with the option to eliminate any existing criteria.  Below, that is
//  criteria that can be searched by but is not currently being used in the search.  Each click of the 
//  submission button or the delete button will add or remove criteria.  Note that the buyer and seller 
//  will have somewhat different search criteria.

function main_search($var_list)
{
//  var_name = name of variable used in form
//  var_type = type of variable (integer, string, bool, etc)
//  form_code = html code used in search form
//  php_code = php code used in checking the variable 

//  $var_list = array( array( "name" => "var_name", "type" => "var_type_int_str_bool", "form_code" = "html_code" , "php_code" => "php code") );

//  Next we check if any new criteria have been submitted.  If so, then we will store the criteria in 
//  the $_SESSION array so that it is temporarly avaaible whilst the session is active.  Note that we 
//  cycle through all the variables in out search list.
foreach ($var_list as $row)
	{
	if ( isset( $_POST[$row['name']] ) === TRUE )
		{
		$_SESSION[$row['name']] = $_POST[$row['name']];
		}
	}

//  If the user resets the form at the bottom, then we delete all the current criteria.  
//  To do that, we must cycle through it by checking if the criteria is active and, if so, deleting it.
if ( $_POST['update'] == "reset" )
	{
	foreach ($var_list as $row)
		{
		if ( isset( $_SESSION[$row['name']] ) === TRUE )
			{
			unset( $_SESSION[$row['name']] );
			}
		}
	}

//  If the X button is pressed next to an active criteria, it will be deleted.  The buttons should have
//  the name "delete" and their var_name as their value.
if ( isset( $_POST['X'] ) === TRUE )
	{
	unset( $_SESSION[$_POST['X']] );
	}

//  Now we begin the actual form code which contains both the active criteria and the unused criteria.
$currentpage = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
echo '<form name="search" action="'.$currentpage.'" method="post">';

//  We want to search through all the criteria to see which ones have active $_SESSION variables.
//  Those will be displayed with their current value and a way to delete them.  
foreach ($var_list as $row)
	{
	if ( isset( $_SESSION[$row['name']] ) === TRUE )
		{
		$php_code = eval($row['php_code']);
		echo '<span class="search_criteria">'.$php_code.'</span>';
		echo '<input type="submit" name="'.$row['name'].'" value="X" class="delete_criteria">';
		$vars_exist = FALSE;  // unused criteria are set to FALSE here anytime at least 1 criteria is beign searched
		}
	}

//  Now, we look for all the criteria that do NOT have active $_SESSION variables.  Those are ones
//  that can be searched through.
foreach ($var_list as $row)
	{
	if ( isset( $_SESSION[$row['name']] ) === FALSE )
		{
		echo $row["form_code"];
		$vars_exist = TRUE; //  there exists an unused criteria so we know that there is still free vars
		}
	}

//  here we add the search button but only when it is still possible to search using criteria
if ( $vars_exist === TRUE )
	{
	echo '<input type="submit" name="update" value="submit">';
	}

//  users should always have the option to reset the form even if they have not searched  
//  this way they can reset a form that may be filled in but incorrectly so
echo '<input type="submit" name="update" value="reset">';
echo '</form>';	
}
I want to take the PHP code stored in the 2 dimensional array $var_list from the first code box at the top and evaluate it within the main_search() function to produce HTML. I think but I am not sure that the part of main_search() that I am explicitly having trouble with is. . .

Code: Select all

//  We want to search through all the criteria to see which ones have active $_SESSION variables.
//  Those will be displayed with their current value and a way to delete them.  
foreach ($var_list as $row)
	{
	if ( isset( $_SESSION[$row['name']] ) === TRUE )
		{
		$php_code = eval($row['php_code']);
		echo '<span class="search_criteria">'.$php_code.'</span>';
		echo '<input type="submit" name="'.$row['name'].'" value="X" class="delete_criteria">';
		$vars_exist = FALSE;  // unused criteria are set to FALSE here anytime at least 1 criteria is beign searched
		}
	}
If I can clarify anything, then please let me know. I am not sure exactly what to provide for your consumption. Thank you very much!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Storing PHP Code in an Array

Post by Jonah Bron »

If you echo PHP code in a string, it will output just like that. You can't put PHP code in a string and have it execute.

For those about to mention eval(), don't.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Storing PHP Code in an Array

Post by twinedev »

I didn't have time to really go through your code, but at first glance I would say this:

When you are doing your eval, you are doing this (from the exmple given):

Code: Select all

eval('$row[\'name\'] : $_SESSION[$name]');
Which at that point is like having the following line of code there:

Code: Select all

$row['name'] : $_SESSION[$name];
Which as you can see, isn't valid PHP code.

check out http://www.php.net/eval for more help on how eval works.

-Greg
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Storing PHP Code in an Array

Post by DigitalMind »

Jonah Bron wrote:You can't put PHP code in a string and have it execute.
eval('echo 1; echo 2; echo 3;');
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Storing PHP Code in an Array

Post by Jonah Bron »

Jonah Bron wrote:For those about to mention eval(), don't.
Without looking at the code, I think there's probably a 90% chance this can be done without eval.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Storing PHP Code in an Array

Post by John Cartwright »

Best advise you'll ever get: Do. Not. Use. Eval.
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Storing PHP Code in an Array

Post by DigitalMind »

Jonah Bron wrote:Without looking at the code, I think there's probably a 90% chance this can be done without eval.
I would say 99,9% chance. eval == evil :)
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Storing PHP Code in an Array

Post by twinedev »

John Cartwright wrote:Best advise you'll ever get: Do. Not. Use. Eval.
I'll second that!

To quote someone at work "But Drupal uses it"... Then I showed them the analysis of drupal running with on a system with Zend Server which pinpointed the majority of the processing time for a drupal page was a single eval statement. (let alone how how much clensing you better have in place to anything going into it)
Post Reply