1

I store the field names within an array, in hopes to dynamically create the variables.

I receive a illegal offset type error for the if and else, these two lines:

$data[$tmp_field] = $tmp_field[$id];

$data[$tmp_field] = 0;

I checked the post data and it is posting with the appropriate data, but I am not sure what the problem is.

$student_id stores all the students ids., for example: $student_id = array(8,9,11,23,30,42,55);

function updateStudentInfo() {
  $student_id = $this->input->post('student_id');
  $internet_student = $this->input->post('internet_student');
  $dismissed = $this->input->post('dismissed');
  $non_matriculated_student = $this->input->post('non_matriculated_student');
  $felony = $this->input->post('felony');
  $probation = $this->input->post('probation');
  $h_number = $this->input->post('h_number');
  $office_direct_to = $this->input->post('office_direct_to');
  $holds = $this->input->post('holds');

  $fields = array('internet_student', 'non_matriculated_student', 'h_number', 'felony', 'probation', 'dismissed');

  foreach($student_id as $id):
    $data = array();

    foreach($fields as $field_name):
      $tmp_field = ${$field_name};

      if(empty($tmp_field[$id])) {
        $data[$tmp_field] = 0;
      } else { 
        $data[$tmp_field] = $tmp_field[$id];
      }
    endforeach;

    print '<pre style="color:#fff;">';
    print_r($data);
    print '</pre>';

  endforeach;
}

This is the array format I desire:

Array
(
    [internet_student] => 1
    [non_matriculated_student] => 1
    [h_number] => 0
    [felony] => 0
    [probation] => 1
    [dismissed] => 0
)

Added screenshot to give you a visual of the form the data is being posted from

enter image description here

3
  • What happens when you use $data[$field_name] instead of $data[$tmp_field]? You are trying to set key as a array, which could give an error. Commented Aug 2, 2012 at 15:02
  • I'd go down the route of $student=array_merge(array('internet_student'=>0, 'non_matriculated_student'=>0, 'h_number'=>0, 'felony'=>0, 'probation'=>0, 'dismissed'=>0), $student); this will create an array of 0 values and over-right any existing values over the top of it Commented Aug 2, 2012 at 15:04
  • @Waygood could you submit your answer with that example? Commented Aug 2, 2012 at 15:17

2 Answers 2

1
foreach($student_id as $id):
    $data = array();

    foreach($fields as $field_name):
      $tmp_field = ${$field_name};

      if(empty($tmp_field[$id])) {
        $data[$field_name] = 0;
      } else { 
        $data[$field_name] = $tmp_field[$id];
      }
    endforeach;

    print '<pre style="color:#fff;">';
    print_r($data);
    print '</pre>';

  endforeach;
Sign up to request clarification or add additional context in comments.

1 Comment

That looks like it did the trick. I had $data[$tmp_field] = $tmp_field[$id]; wrong.
0

I am assuming that all these fields are arrays, as otherwise you wouldn't need any loops.

function updateStudentInfo()
{
    $student_id                 = $this->input->post('student_id');
    $internet_student           = $this->input->post('internet_student');
    $dismissed              = $this->input->post('dismissed');
    $non_matriculated_student   = $this->input->post('non_matriculated_student');
    $felony                     = $this->input->post('felony');
    $probation              = $this->input->post('probation');
    $h_number                   = $this->input->post('h_number');
    $office_direct_to           = $this->input->post('office_direct_to');
    $holds                  = $this->input->post('holds');

    $fields = array('internet_student', 'non_matriculated_student', 'h_number', 'felony', 'probation', 'dismissed');

    $student_count  = count($student_id);
    foreach($student_id as $id)
    {
        $data   = array();
        foreach($fields as $field)
        {
            if(array_key_exists($id, $$field))
                $data[$field]   = ${$field}[$id];
        }
    }
}

You are trying to use the student id as an array key for the other fields but the HTML form is just a standard indexed array, not keyed to any student data.

4 Comments

The $student_id stores an array of student's ID's, so I use the id's in there to pull the appropriate data from each array. For example, $student_id = array(8, 9, 11, 21, 30, 43);
@Brad are the other fields (h_number, holds, etc) coming from a HTML form? If so then they aren't keyed to the student ids
yes, they are keyed into the $student_id id's. In the view, I have field names like this: h_number[$s->id], same for holds[$s->id][], dismissed[$s->id]
@Brad oh, I thought this was direct from the submitted form. I've tweaked the loop to use the student id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.