Associative array PHP

stoicunknown

I am doing a back-end validation and I wonder if there is a better way to do this instead of typing all the variables of each data, here's my code

  public function status_check()       
    {
        $radioStatus = $this->input->post('radioStatus');

        $JuniorHS = array(    
            $SeniorHSGradeYear = $this->input->post('SeniorHSGradeYear'),
            $radioHSTracks = $this->input->post('radioHSTracks'),
            $radioCollegeCourses = $this->input->post('radioCollegeCourses'),
            $radioGraduateCourses = $this->input->post('$radioGraduateCourses'),
            $hsSchoolName = $this->input->post('$hsSchoolName'),
            $hsSchoolLocation = $this->input->post('$hsSchoolLocation'),
            $hsYearsAttended = $this->input->post('$hsYearsAttended'),
            $hsGradDate = $this->input->post('$hsGradDate'),
            $collegeSchoolName = $this->input->post('$collegeSchoolName'),
            $collegeSchoolLocation = $this->input->post('$collegeSchoolLocation'),
            $collegeYearsAttended = $this->input->post('$collegeYearsAttended'),
            $formerCollegeDegree = $this->input->post('$formerCollegeDegree'),
            $collegeGradDate = $this->input->post('$collegeGradDate'),
        );

        if  ($radioStatus == "JuniorHS")
        {
            if (!empty($JuniorHS))
            {
                $this->form_validation->set_message('status_check', 'Please check the information you provided');
                return false;
            }
        }
        return true;
    }

I put all the variables inside an array then i want to check if any of it has a value, if the array is not empty then it will return as false, hence if its empty then it will return true, but the code always return as false even if its empty, am i even doing it right?

José Lopez Coronado

I was reading that:

Since CI 2.1.0, you can now do the following easy way to get all post data from the form.

$data = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
$data = $this->input->post(); // returns all POST items without XSS filter 

So, I think this could work for you:

public function status_check()       
    {
        $data = $this->input->post();

        foreach ($data as $key => $value) {
            if ($key == 'radioStatus') {
                $radioStatus = $value;
            }else{
                $JuniorHS[] = $value;
            }
        }

        if  ($radioStatus == "JuniorHS")
        {
            foreach ($JuniorHS as $key => $value) {
                if (!empty($value))
                {
                    $this->form_validation->set_message('status_check', 'Please check the information you provided');
                    return false;
                }
            }
        }
        return true;
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related