double0201 2013-02-13 10:07
浏览 15
已采纳

基于动态密钥=>值的PhP关联错误数组

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.

  • 写回答

1条回答 默认 最新

  • dsf11t5u1651 2013-02-13 10:13
    关注
    $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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比