tags:

views:

192

answers:

2

Hi guys. I have the following code:

if ( (isset($_GET['slAction'])) && ($_GET['slAction'] == "manage_label") )
{    
  $formData = getFormData();
  foreach ($formData as $key => $value)
    echo "(Key: $key, Value: $value )<br /> ";

}

// All form fields are identified by '[id]_[name]', where 'id' is the 
// identifier of the form type. Eg. label, store etc.
// The field identifier we want to return is just the name and not the id.  
  function getFormData()
  {
    $form_fields = array_keys($_POST); 

    for ($i = 0; $i < sizeof($form_fields); $i++) 
    {
      $thisField = $form_fields[$i];
      $thisValue = $_POST[$thisField];

      //If field is an array, put all it's values into one string 
      if (is_array($thisValue))
      {
        for ($j = 0; $j < sizeof($thisValue); $j++)
        {
          $str .= "$thisValue[$j],";
        }

        // Remove the extra ',' at the end
        $thisValue =  substr($str, 0, -1);

       //Assosiative array $variable[key] = value
       $formData[end(explode("_", $thisField))] = $thisValue;
      }
      else 
        $formData[end(explode("_", $thisField))] = $thisValue;      
    } 
    return $formData;
  }

The output from this code is:

(Key: id, Value: 7276 )
(Key: name, Value: 911 Main brand )
(Key: email, Value: )
(Key: www, Value: )
(Key: categories, Value: Menswear,Womenswear,Shoes )
(Key: targetgroup, Value: )
(Key: keywords, Value: )
(Key: description, Value: Testing )
(Key: saveForm, Value: Save )

Now this is my problem. The form field called 'label_categories' are checkboxes and is returned as an array. The output, as you see, is "Menswear,Womenswear,Shoes". If I try 'echo $formData['name']', the output is "911 Main brand". If I try 'echo $formData['categories']. the output is blank / empty.

How come I can output the string 'name' and not the string 'categories'? In the getFormData() function, I turn the array into a string....

Any help appreciated.

+1  A: 

That code can be greatly simplified:

// 1. no need for isset() check in the case where you're testing a value
// 2. use single quotes for strings where possible
if ($_GET['slAction'] == 'manage_label') {    
  $formData = getFormData();
  // 3. Good rule is to use braces even when not necessary
  foreach ($formData as $key => $value) {
    echo "(Key: $key, Value: $value )<br /> ";
  }
}

function getFormData() {
  // 4. prefer explicit initialization
  $formData = array();
  // 5. this foreach is much cleaner than a loop over keys
  foreach ($_POST as $k => $v) {
    // 6. this is a lot cleaner than end(explode(...))
    $name = preg_replace('!.*_!', '', $k);
    if (is_array($v)) {
      // 7. this implode() replaces 5 lines of code
      // and is MUCH clearer to read
      $formData[$name] = implode(',', $v);
    } else {
      $formData[$name] = $v;
    }
  }
  return $formData;
}
cletus
err, cletus, why did you remove the isset? Presumably sometimes the slAction won't be passed to the URL, so it would indeed be necessary.
Paolo Bergantino
It's not necessary in this case because $_GET['slAction'] == 'manage_label' will eval to false if its not set, which is what you want anyway.
cletus
Wow, thanks. That worked like a charm. I'm a light weight programmer - so monkey see, monkey doo :) I'm processing 3 forms on this page. I'm using slAction to check if a form is submited, and to check which form has been submited.
Steven
You're welcome. Just so I'm making myself clear: I'm not saying isset() isn't necessary at all, just in *this case* because you're checking the value anyway.
cletus
But if the slAction is not passed and you try checking it, it will raise a notice, so you do need the isset....
Paolo Bergantino
A: 

instead of posting code, ill explain.

you cannot echo $_post['catagories'] because it is not a string or number. It is an array.

you can echo $_post['catagories'][nth number'] or echo implode(', ' $_post['catagories'])

Ozzy