Jump to content

Recommended Posts

Hi. I have some weird problem which i have not been able to understand for some hours now. I consider myself fairly knowledgeable in php, but this problem must be caused by information i missed somewhere.

 

I have an array with object values assigned to each of its elements:

<?php
#######################
# fields info
#######################

//field
$field_name = "name";
$form_fields_info[$field_name] -> title = "naam";
$form_fields_info[$field_name] -> input_type = "text";
$form_fields_info[$field_name] -> required = true;
$form_fields_info[$field_name] -> fieldset = "persoonsgegevens";
$form_fields_info[$field_name] -> maxlength = 200;

//field
$field_name = "address";
$form_fields_info[$field_name] -> title = "adres";
$form_fields_info[$field_name] -> input_type = "text";
$form_fields_info[$field_name] -> required = true;
$form_fields_info[$field_name] -> fieldset = "persoonsgegevens";
$form_fields_info[$field_name] -> maxlength = 200;
?>

 

this array is initiated inside a function. after initiating the array i perform a check on submitted form values through a different function:

<?php
if($_POST['submitted'] == "yes")
{
$submitted_form_fields_info = $form_fields_info;

list($submitted_form_fields_info, $warnings_string) = verify_submitted_info($submitted_form_fields_info, $form_hidden_fields_info, $prefix);

if(!empty($submitted_form_fields_info))
{
	$form_fields_info = $submitted_form_fields_info;
}
}
?>

 

The problem is that when the verify_submitted_info function has been performed, somehow, even when the returning array $submitted_form_fields_info is empty, $form_fields_info will get the same (new) values which were added to $submitted_form_fields_info in the verify_submitted_info function. I always thought variables are only valid within the function they are used in (except for global variables of course). Yet adding values to elements of a DIFFERENT array in a another function is having its effect on a "lookalike" array in the calling function... Can anyone please help me understand what is causing this behaviour?

 

beneath you will find teh complete code. Ty in advance.

 

 

<?php

if (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__)) !== false)
{
header("location: " . $_SERVER['host_name'] . "/index.php");
exit;
}

function get_concerts_info()
{
global $db;

$sql =
"
	SELECT *
	FROM `events`
	WHERE	`name` = 'concert'
	ORDER BY `date` ASC, `time` ASC, `city` ASC;
";

$concerts_info = $db -> getAssoc($sql);

return $concerts_info;
}

function book($_max_width)
{
$concerts_info = get_concerts_info();

$prefix = "bk_";

$_measures[1] = $_max_width; //volle breedte hoofddiv
$_measures[2] = 25; //required / inrequired div
$_measures[3] = 20; //line height
$_measures[4] = 165; //title_div width
$_measures[8] = 20; //input spacing
$_measures[7] = $_measures[1] - 20; //row div width
$_measures[5] = $_measures[7] - $_measures[2] - $_measures[4] - 2; //input_div width (-2 for ie)
$_measures[6] = $_measures[5] - $_measures[8]; //input_div width

#######################
# fields info
#######################

//field
$field_name = "name";
$form_fields_info[$field_name] -> title = "naam";
$form_fields_info[$field_name] -> input_type = "text";
$form_fields_info[$field_name] -> required = true;
$form_fields_info[$field_name] -> fieldset = "persoonsgegevens";
$form_fields_info[$field_name] -> maxlength = 200;

//field
$field_name = "address";
$form_fields_info[$field_name] -> title = "adres";
$form_fields_info[$field_name] -> input_type = "text";
$form_fields_info[$field_name] -> required = true;
$form_fields_info[$field_name] -> fieldset = "persoonsgegevens";
$form_fields_info[$field_name] -> maxlength = 200;

//field
$field_name = "postalcode_city";
$form_fields_info[$field_name] -> title = "postcode & woonplaats";
$form_fields_info[$field_name] -> input_type = "text";
$form_fields_info[$field_name] -> required = true;
$form_fields_info[$field_name] -> fieldset = "persoonsgegevens";
$form_fields_info[$field_name] -> maxlength = 200;

//field
$field_name = "country";
$form_fields_info[$field_name] -> title = "land";
$form_fields_info[$field_name] -> input_type = "text";
$form_fields_info[$field_name] -> required = true;
$form_fields_info[$field_name] -> fieldset = "persoonsgegevens";
$form_fields_info[$field_name] -> maxlength = 200;
$form_fields_info[$field_name] -> value = "Nederland";

//field
$field_name = "email";
$form_fields_info[$field_name] -> title = "email";
$form_fields_info[$field_name] -> input_type = "text";
$form_fields_info[$field_name] -> required = true;
$form_fields_info[$field_name] -> fieldset = "contactgegevens";
$form_fields_info[$field_name] -> maxlength = 200;

//field
$field_name = "phone_nr";
$form_fields_info[$field_name] -> title = "telefoonnummer";
$form_fields_info[$field_name] -> input_type = "text";
$form_fields_info[$field_name] -> required = true;
$form_fields_info[$field_name] -> fieldset = "contactgegevens";
$form_fields_info[$field_name] -> maxlength = 200;


//field
$field_name = "nr_bookings";
$form_fields_info[$field_name] -> title = "aantal reserveringen";
$form_fields_info[$field_name] -> input_type = "text";
$form_fields_info[$field_name] -> required = true;
$form_fields_info[$field_name] -> fieldset = "bestelgegevens";
$form_fields_info[$field_name] -> maxlength = 3;
$form_fields_info[$field_name] -> value = 1;

//field
$field_name = "discount";
$form_fields_info[$field_name] -> title = "korting";
$form_fields_info[$field_name] -> discribtion = "korting geldt alleen voor studenten & genodigden";
$form_fields_info[$field_name] -> input_type = "radio";
$form_fields_info[$field_name] -> required = true;
$form_fields_info[$field_name] -> fieldset = "bestelgegevens";
$form_fields_info[$field_name] -> value[1] = "ja";
$form_fields_info[$field_name] -> value[2] = "nee";
$form_fields_info[$field_name] -> checked = 2;

//field
$field_name = "event";
$form_fields_info[$field_name] -> title = "concert";
$form_fields_info[$field_name] -> input_type = "checkbox";
$form_fields_info[$field_name] -> required = true;
$form_fields_info[$field_name] -> fieldset = "bestelgegevens";

foreach($concerts_info as $concertID => $concert_info)
{
	if(!empty($concert_info -> day_name))
	{
		$concert_title = $concert_info -> day_name;
	}

	if(!empty($concert_info -> date))
	{
		$concert_date = sqlDate2screenDate_2($concert_info -> date);

		if(!empty($concert_title))
		{
			$concert_title .= " ";
		}

		$concert_title .= $concert_date;
	}

	if(!empty($concert_info -> city))
	{
		if(!empty($concert_title))
		{
			$concert_title .= ", ";
		}

		$concert_title .= $concert_info -> city;
	}

	if(!empty($concert_info -> facility_name))
	{
		if(!empty($concert_title))
		{
			$concert_title .= ", ";
		}

		if(!empty($concert_info -> facility_link))
		{
			$concert_title .= "<a href=\"{$concert_info -> facility_link}\" target=\"_blank\">" . $concert_info -> facility_name . "</a>";
		}
		else
		{
			$concert_title .= $concert_info -> facility_name;
		}
	}

	if(!empty($concert_info -> time))
	{
		$concert_time = substr($concert_info -> time, 0, 5);

		if(!empty($concert_title))
		{
			if(empty($concert_info -> facility_name))
			{
				$concert_title .= ", ";
			}
			else
			{
				$concert_title .= " ";
			}
		}

		$concert_title .= $concert_time;
	}

	if(!empty($concert_title))
	{
		$form_fields_info[$field_name] -> value[$concertID] = $concert_title;
	}
}
$form_fields_info[$field_name] -> checked = array();

//field
$field_name = "submit";
$form_fields_info[$field_name] -> value = "verzend";
$form_fields_info[$field_name] -> input_type = "submit";
$form_fields_info[$field_name] -> required = false;
$form_fields_info[$field_name] -> fieldset = "plaats reservering";

//hidden fields

#######################

if($_POST['submitted'] == "yes")
{
	$submitted_form_fields_info = $form_fields_info;

	list($submitted_form_fields_info, $warnings_string) = verify_submitted_info($submitted_form_fields_info, $form_hidden_fields_info, $prefix);

	if(!empty($submitted_form_fields_info))
	{
		$form_fields_info = $submitted_form_fields_info;
	}
}

$fields = create_fields($form_fields_info, $form_hidden_fields_info, $prefix, $_measures);

$parent_div = <<<HERE
<div class="floatLeft" style="width: {$_measures[1]}px; clear: both; margin-top: 5px;">
	<div class="floatLeft warning" style="width: {$_measures[1]}px; clear: both;">
			$warnings_string
	</div>
	<div class="floatLeft" style="width: {$_measures[1]}px; clear: both;">
		<form name="book_form" method="POST">
			$fields
		</form>	
	</div>
</div>
HERE;

return $parent_div;
}

function verify_submitted_info($submitted_form_fields_info, $form_hidden_fields_info, $prefix)
{
$noSucces = false;

foreach($submitted_form_fields_info as $field_name => $field_attributes)
{
	unset($checked_values);

	switch($field_attributes -> input_type)
	{
		case 'text':
			if(empty($_POST[$prefix . $field_name]))
			{
				if($field_attributes -> required)
				{
					$noSucces = true;
					$warnings_string .= "het volgende veld is niet ingevuld: " . $field_attributes -> title . "<br />";
					$submitted_form_fields_info[$field_name] -> classes = "warning";
				}

				$submitted_form_fields_info[$field_name] -> value = "";
			}
			else
			{
				$submitted_form_fields_info[$field_name] -> value = $_POST[$prefix . $field_name];
			}
		break;

		case 'radio':
			if(empty($_POST[$prefix . $field_name]))
			{
				if($field_attributes -> required)
				{
					$noSucces = true;
					$warnings_string .= "het volgende veld is niet ingevuld: " . $field_attributes -> title . "<br />";
					$submitted_form_fields_info[$field_name] -> classes = "warning";
				}

				$submitted_form_fields_info[$field_name] -> checked = "";
			}
			else
			{
				$submitted_form_fields_info[$field_name] -> checked = $_POST[$prefix . $field_name];
			}
		break;

		case 'checkbox':
			foreach($field_attributes -> value as $value => $option_title)
			{
				if($_POST[$prefix . $field_name . "_" . $value] == "on")
				{
					 $checked_values[] = $value;
				}
			}

			if(empty($checked_values))
			{
				if($field_attributes -> required)
				{
					$noSucces = true;
					$warnings_string .= "het volgende veld is niet ingevuld: " . $field_attributes -> title . "<br />";
					$submitted_form_fields_info[$field_name] -> classes = "warning";
				}

				$submitted_form_fields_info[$field_name] -> checked = array();
			}
			else
			{
				$submitted_form_fields_info[$field_name] -> checked = $checked_values;
			}
		break;
	}
}

if($noSucces)
{
	//return array($submitted_form_fields_info, $warnings_string);
}
else
{
	return NULL;
}

}

function create_fields($form_fields_info, $form_hidden_fields_info, $prefix, $_measures)
{

$required_div = <<<HERE
<div class="floatLeft required" style="width: {$_measures[2]}px; height: {$_measures[3]}px; padding: 0px;" title="verplicht"><!-- --></div>
HERE;

$inrequired_div = <<<HERE
<div class="floatLeft" style="width: {$_measures[2]}px; height: {$_measures[3]}px; padding: 0px;"><!-- --></div>
HERE;

foreach($form_fields_info as $field_name => $field_attributes)
{
	$fieldset_string = "";

	if(empty($last_fieldset))
	{
		$last_fieldset = $field_attributes -> fieldset;
		$fieldset_string = "<fieldset><legend>{$field_attributes -> fieldset}</legend>";
	}
	else
	{
		if(!($last_fieldset == $field_attributes -> fieldset))
		{
			$fieldset_string = "</fieldset><fieldset><legend>{$field_attributes -> fieldset}</legend>";
		}

		$last_fieldset = $field_attributes -> fieldset;
	}

	if($field_attributes -> required)
	{
		$requirency = $required_div;
	}
	else
	{
		$requirency = $inrequired_div;
	}

	unset($input_field);
	unset($ckecked_options);

	switch($field_attributes -> input_type)
	{
		case 'text':
			$input_field = <<<HERE
			<input id="input_id_{$field_name}" type="text" name="{$prefix}{$field_name}" value="{$field_attributes -> value}" maxlength="{$field_attributes -> maxlength}" class="floatRight" style="width: {$_measures[6]}px;">
HERE;
		break;

		case 'radio':

			$ckecked_options[$field_attributes -> checked] = "CHECKED";

			foreach($field_attributes -> value as $value => $option_title)
			{
				$input_field .= <<<HERE
				<label><input type="radio" name="{$prefix}{$field_name}" value="$value" $ckecked_options[$value]>
				$option_title</label><br />
HERE;
			}

			$input_field = <<<HERE
			<div class="floatLeft" style="margin-left: {$_measures[8]}px;">$input_field </div>
HERE;
		break;

		case 'checkbox':
			foreach($field_attributes -> checked as $checked_value)
			{
				$ckecked_options[$checked_value] = "CHECKED";
			}

			foreach($field_attributes -> value as $value => $option_title)
			{
				$input_field .= <<<HERE
				<label><input type="checkbox" name="{$prefix}{$field_name}_{$value}" $ckecked_options[$value]>
				$option_title</label><br />
HERE;
			}

			$input_field = <<<HERE
			<div class="floatLeft" style="margin-left: {$_measures[8]}px;">$input_field </div>
HERE;
		break;

		case 'submit':
			$input_field = <<<HERE
			<input class="floatRight" type="submit" value="{$field_attributes -> value}">
HERE;
		break;
	}

	$fields .= <<<HERE
	$fieldset_string
	<div class="floatLeft" style="width: {$_measures[7]}px; clear: both;">
		$requirency
		<div class="floatLeft {$form_fields_info[$field_name] -> classes}" style="width: {$_measures[4]}px; line-height: {$_measures[3]}px;">
			<label for="input_id_{$field_name}">
				{$field_attributes -> title}<br /><span class="attention_form">{$field_attributes -> discribtion}</span>
			</label>
		</div>
		<div class="floatLeft" style="width: {$_measures[5]}px; line-height: {$_measures[3]}px;">
			$input_field
		</div>
	</div>
HERE;
}

if(!empty($form_hidden_fields_info))
{
	foreach($form_hidden_fields_info as $field_name => $value)
	{
		$fields .= <<<HERE
		<input type="hidden" name="$field_name" value="$value">
HERE;
	}
}

$fields .= <<<HERE
		<input type="hidden" name="submitted" value="yes">
HERE;

$fields .= "</fieldset>";

return $fields;
}

?>

Since there was no answer for a day i may assume that none knows the explanation for this phenomenon? Or is my description of the problem so crappy that none bothered to even check it out :/

 

For now i solved the problem in a not-so-gentle way, so im not hasty for an answer. However, just out of personal interest i still would like to understand how the storage of object variables works ($variable[array_element_key] -> object = value). I have had trouble with it before, also concerning the values being changed across different functions. If my explanation was too crappy i will try to write a simple and short script sometime soon in which i can point out the problem i came across.

 

any wisdom shared with me on this subject still greatly appreciated.

See this script:

 

<?php
$a->val = 1;
f($a);

function f($a) {
  $a->val = 2;
}

var_dump($a);
?>

 

So it just passes an object and alters it inside.  Run it under php 4 and you'll find the output is 1, but run it under php 5 and the output is 2.  The point is that php 5 passes objects by reference, but php 4 passes them copy-on-write.

 

Since you seem to be using objects to store data only, you might want to use an array instead of an object.  Arrays will still be passed copy-on-write even in php 5.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.