problem with array

#1
[eluser]mashary[/eluser]
anyone help me pelase!!!!

i have code in my Controller

Code:
foreach ($_POST as $key => $val){
            if ($key!='id'){
                if ($key!='STMIK'&&$key!='UPB'&&$key!='Sekolah'&&$key!='Kursus'){
                    $data[$key] = $this->input->post($key);
                }else{
                    $data['group'] .=  $val;
                }
            }
        }

then CI said an error like this
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: group

Filename: controllers/anggota.php

Line Number: 72

how come? and how to fix this.... please!!!


sorry poor english

#2
[eluser]John_Betong[/eluser]
Try this:
Code:
foreach ($_POST as $key => $val)
  {
    $data['group'] = isset($data['group']) ? $data['group'] : '';

    if ($key!='id')
    {
      if ($key!='STMIK'&&$key!='UPB'&&$key!='Sekolah'&&$key!='Kursus')
      {
        $data[$key] = $this->input->post($key);
      }else{
        $data['group'] .=  $val;
      }
    }
  }
 

#3
[eluser]mashary[/eluser]
cool this code work as well,
can u tell me the reason, why my code doesn't work?

#4
[eluser]John_Betong[/eluser]
[quote author="mashary" date="1274432133"]cool this code work as well,
can u tell me the reason, why my code doesn't work?[/quote]

>>> Message: Undefined index: group

The $_POST["group"] index is not set.

My code uses isset(...) to test to see if $_POST["group"] is set.
If it is set then the original value is used otherwise/else an empty string is used.




.

#5
[eluser]mashary[/eluser]
so the point is to define every variable

ok thanks so much

#6
[eluser]John_Betong[/eluser]
From memory and without checking:

If your $_POST array comes from a form and you are checking the fields then any empty field will not be shown. This is why you had your original error - which I tested with the isset(...) function.

Test this out for yourself before stepping through your foreach(#_POST as ...)
Code:
echo '<pre>';
    print_r($_POST);
echo '</pre>';
&nbsp;
&nbsp;
&nbsp;


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.