Jump to content

storing results of a function - previous result overwritten with new result


DLR

Recommended Posts

I have created a function to validate input.

 

function validate_dimension($value,$name) {

 

global $errors;

$errors = array();

 

......validation actions here - this all works fine - if I get an error it is captured like this  . . .

 

$errors[] = "Dimension value missing";

 

This is all fine and the $error is returned correctly.

 

So that I can keep the error, I save it as a Session variable

 

$_SESSION['errors'] = $errors;

 

HOWEVER the next time I run the function (with the next dimension to be checked) , $_SESSION['errors'] is overwritten and the first error is lost.

 

I have solved the problem by creating a new array - $all_errors - and adding $errors to $all_errors each time the function is run - but this seems clumsy - I do not need the function as I could just as easily wite a separate line of code for each dimension to be checked.

 

Can anyone suggest a better way of doing this?

Please?

 

 

Link to comment
Share on other sites

Okay so first off,, $_SESSION is already global scope, so there's no need to be creating this extra global $errors array (and you really shouldn't be side-stepping scope like that to begin with).  Also, you mentioned returning $errors... well the point of returning something is to keep within scope, so I fear you may be again doing something iffy...  And 3rd, you are saying that you are using a session variable to "keep" the errors...are you trying to pass the errors to another page? If this is all within one script page being executed, then there's no need for even that.

 

What is the overall goal here?  Are you simply looping through data, validating and returning all relevant errors? Your code should more or less look like this (just using $_POST in a foreach as an example...):

 

foreach($_POST as $name => $value) {
  $errors[] = validate_dimension($value,$name);
}

function validate_dimension($value,$name) {
  // validate info. If there is an error...
  return "error message";
}

 

You will then have a full array of all relevant errors.  If you are passing it to another page, then just use $_SESSION['errors'][] instead of $errors[] in the foreach

Link to comment
Share on other sites

Thank you. I really appreciate your assistance. I have learnt a lot.

 

Perhaps you could assist me further?

 

Some of the $_POST variables are permitted to be nul. But the validate function will mark any nul input as an error.

So the fixed error message becomes inappropriate.

 

I have tried to add a "flag" to the variable so I can permit a nul input, but cannot see a way to do this.

Is there perhaps another elegant way to solve this?

 

Many thanks for your time.

Link to comment
Share on other sites

Thank you. I really appreciate your assistance. I have learnt a lot.

 

Perhaps you could assist me further?

 

Some of the $_POST variables are permitted to be nul. But the validate function will mark any nul input as an error.

So the fixed error message becomes inappropriate.

 

I have tried to add a "flag" to the variable so I can permit a nul input, but cannot see a way to do this.

Is there perhaps another elegant way to solve this?

 

Many thanks for your time.

 

well i have no idea what the validation part of your function looks like...

Link to comment
Share on other sites

Thanks,

 

herewith the basic structure.

<input type="text" name="width">  // this variable MUST have a value
<input type="text" name="depth">  // this variable may be null

//now validate inputs
foreach($_POST as $name => $value) {
     $errors[]  = validate_dimension($value,$name);	
}

function validate_dimension($value,$name) {

if(! isset($_POST[$name]) OR $_POST[$name] == "" OR $_POST[$name] == 0) {
return "<span style='color:red;'>Dimension for " .$name . " required</span>";

} else {
		  			  
$_SESSION[$name] = htmlentities($value);
}

Is there a way to "flag" or "switch" the validation so that some variables may have a nul value and not show an error message?

Link to comment
Share on other sites

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.