0

Here is a class I've began writing to add a new user to my database via a registration form. Everything has worked fine up until I try to do back-end checking to see if required values are not provided. Here is the code...

class User
{
//Basic Login variables
private $uName;
private $pWord;
private $cpWord;
private $shaPass;

//Account info vars
private $fName;
private $lName;
private $email;
private $confEmail;
private $mPhone;
private $hPhone;

//Trading info vars
private $trName;
private $phone; //Defaults to home phone, or cell, if no home number specified.
private $fax;
private $web;

//Home Address vars
private $h_addr1;
private $h_addr2;
private $h_city;
private $h_state;
private $h_zip;

//Postal Address vars
private $p_addr1;
private $p_addr2;
private $p_city;
private $p_state;
private $p_zip;

private $not_required_vars;
//Post name collection
private $post_coll;
//Error collection
private $errors;

public function User()
{       
    $this->not_required_vars = array('fax', 'web');
    $this->post_coll = array('uName', 'pWord', 'cpWord', 'fName', 'lName', 'email', 'confEmail', 'dob', 'mPhone', 'hPhone', 'trName', 'phone', 'fax', 'web', 'h_addr1', 'h_addr2', 'h_city', 'h_state', 'h_zip', 'p_addr1', 'p_addr2', 'p_city', 'p_state', 'p_zip');
    //print_r($_POST);

    foreach($this->post_coll as $index)
    {
        if(isset($_POST[$index]) && (!empty($_POST[$index])))
        {
            //echo $index . '<br />';
            $this->{$index} = $_POST[$index];
        }else
        {
            foreach($this->not_required_vars as $not_req)
            {
                if($index != $not_req)
                {
                    //echo $index . '<br />';
                    $this->errors = array();

                    $this->errors[] = array($index=>'This field is required');
                    break;
                    //header('location: '.$_SERVER['PHP_SELF'].'?page='. $_GET['page'] . '&error=1');
                }
            }
        }

    }
    print_r($this->errors);
}

Let me try to save you time by explaining what the above does... I have the array of $_POST index strings called 'post_coll'. I then have my foreach loop through each string checking to see if that string isset in the $_POST array, if it's not, I check to see if it is a required value. If it is indeed a required value I want to create an associative array with the string (such as 'uName') as a key in the array with the value being something like "This field is required" that way when I redirect it back to my form I can getErrors from my class and display them where appropriate on the form.

Thanks in advance for any help.

Highspeed
  • 442
  • 3
  • 18

1 Answers1

2
$this->errors[] = array($index=>'This field is required');

This line looks like its creating and array of arrays

Is it not something like this you are looking for?

$this->errors[$index] = 'This field is required';
cosmicsafari
  • 3,949
  • 11
  • 37
  • 56
  • Ok... yeah, I had that at one point, but when that's there I get this for a print out... `Array ( [cpWord] => This field is required ) ` That is sort of what I want, but... cpWord wasn't the only value not filled out, I also left uName and pWord unfilled as well. It's like it's doing what it's supposed to, but replacing the values inside the array instead of appending the errors. Edit: I was trying to use something like array_push, but that doesn't work either, or at least the way I did it didn't. – Highspeed Feb 13 '13 at 10:20
  • How are you printing the values in the array to screen? – cosmicsafari Feb 13 '13 at 10:23
  • The User class will redirect the user back to the form and above each element I'll have a div tag with a condition stating if $errors['uName'] etc... (obtained from a getErrors function) is set, display it's message above the element. – Highspeed Feb 13 '13 at 10:29
  • Here's an example of how I display the errors... `
    ` **getErrors(); echo ''.$errors['cpWord'].''; ?>** `

    `
    – Highspeed Feb 13 '13 at 10:38
  • May i suggest print_r($theArrayInQuestion) on the page, just to double check that the array is being set correctly. Could very well be that the array is fine and its a mistake elsewhere – cosmicsafari Feb 13 '13 at 10:39
  • If you look at the end of the constructor I do that... and actually I put this condition outside of my foreach loop... `if($index == 'uName') { break; }` That stopped the checking of the remaining unset variables and then when I output the array it said... `Array ( [uName] => This field is required )` So what it's doing is basically replacing one with the other, the next index would be pWord, which I left empty, and finally cpWord which is the last one. – Highspeed Feb 13 '13 at 10:44
  • You are resetting the array each time with the *$this->errors = array()* line in the nested foreach(). If you remove it, it should append to the array rather than overwrite it on each iteration. – cosmicsafari Feb 13 '13 at 10:58
  • Cheers man, probably should have noticed that earlier but we got there in the end ^_^ – cosmicsafari Feb 13 '13 at 11:03