CodeIgniter Forums

Full Version: Best way of packing and unpacking an array at form submit.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
I'm new to CodeIgniter, and need to build a custom feildtype for ExpressionEngine. I have a list of checkboxes that need to be saved into a 3D array. However, I've tried finding documentation on the save() and display_feild() method, but am having a difficult time packaging up the array.

Any input would be greatly appreciated.

Code:
function display_field($data)
{
  //error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
  
  // Load Language File
  $this->EE->lang->loadfile('three_variable_array');
  
  
  //Open form called check_form
  $check_form = form_open('field_id_'.$this->field_id, 'toggle').'<table>';
  
  
  //Toggle Showing all check boxes
  $field =  form_checkbox('newsletter', 'accept', FALSE);
  $feildlabel = form_label('Toggle Rod Selection', 'newsletter');
  $check_form .= '<tr><td>'.$feildlabel.'</td><td>'.$field.'</td></tr>';
  
  //Populate Button
  $js = "onClick=alert($data)";
  $field =  form_button('mybutton', 'Populate', $js);
  $check_form .= '<tr><td>'.$field.'</td></tr>';
  
  
  
  //See if data failed validation
  if ( ! empty($_POST)) {
   $array1d = explode("|", $data);
   $this->create_master_array();
   $count = 0;
  
   for($i = 0; $i < $this->var1_length; $i++)
    for($j = 0; $j < $this->var2_length; $j++)
     for($k = 0; $k < $this->var3_length; $k++) {
      $this->master_array[$i][$j][$k] = $array1d[$count];
      $count++;
     }
  
  
   for($i = 0; $i < $this->var1_length; $i++)
    for($j = 0; $j < $this->var2_length; $j++)
     for($k = 0; $k < $this->var3_length; $k++) {
      $key = $this->master_array[$i][$j][$k];
      
      if($key == 0)
       $checked = FALSE;
      else
       $checked = TRUE;
      
      $field =  form_checkbox($key.'_field_id_'.$this->field_id, $key, $checked);
      $feildlabel = form_label($this->var1[$i].':'.$this->var2[$j].':'.$this->var3[$k], $key);
      $check_form .= '<tr><td>'.$feildlabel.'</td><td>'.$field.'</td></tr>';
     }
  }
  elseif ( ! empty($data))
  {
   $array1d = explode("|", $data);
   $this->create_master_array();
   $count = 0;
  
   for($i = 0; $i < $this->var1_length; $i++)
    for($j = 0; $j < $this->var2_length; $j++)
     for($k = 0; $k < $this->var3_length; $k++) {
      $this->master_array[$i][$j][$k] = $array1d[$count];
      $count++;
     }
    
   for($i = 0; $i < $this->var1_length; $i++)
    for($j = 0; $j < $this->var2_length; $j++)
     for($k = 0; $k < $this->var3_length; $k++) {
      $key = $this->master_array[$i][$j][$k];
      
      if($key == 0)
       $checked = FALSE;
      else
       $checked = TRUE;
      
      $field =  form_checkbox($key.'_field_id_'.$this->field_id, $key, $checked);
      $feildlabel = form_label($this->var1[$i].':'.$this->var2[$j].':'.$this->var3[$k], $key);
      $check_form .= '<tr><td>'.$feildlabel.'</td><td>'.$field.'</td></tr>';
     }
  
  }
  else {
  
   // Start a new master array
   $this->create_master_array();
  
   for($i = 0; $i < $this->var1_length; $i++)
    for($j = 0; $j < $this->var2_length; $j++)
     for($k = 0; $k < $this->var3_length; $k++) {
       $key = $this->master_array[$i][$j][$k];
      
      $field =  form_checkbox($this->var1[$i].'_'.$this->var2[$j].'_'.$this->var3[$k], $key, FALSE);
      $feildlabel = form_label('Rod Length: '.$this->var1[$i].'. Line Weight: '.$this->var2[$j].'. Reel: '.$this->var3[$k].'.', $key);
      $check_form .= '<tr><td>'.$feildlabel.'</td><td>'.$field.'</td></tr>';
     }
  }
  
  $check_form .= form_close('</table>');

  return $check_form;

}



The save method
Code:
function save($data)
{
  $content = FALSE;
  $compilation = array();
  
  foreach($this->master_array as $key)
  {
   $val = $this->EE->input->post($key.'_field_id_'.$this->field_id);
   $compilation[] = $val;
  
   if ( ! empty($val))
    $content = TRUE;
  }
  
  if ($content)
   return implode('|', $compilation);
  else
   return '';
}