SilverRay
SilverRay

Reputation: 331

Codeigniter Severity: Notice --> Undefined variable

I am having frustration because of this error. When I am still programming php without using any framework my code is ok but when I transferred it in codeigniter, it always says severity undefined functions. Please check my posted code:

foreach($data['employee'] as $val)
    {
        if($val['Active']==0)
        {
            $status = @strtoupper('Resigned');
        }
        else
        {
            $status = @strtoupper('Active');
        }

        $str = @substr($val['WS_Code'],0,-2);
        if($str == "PR")
        {
            $line = @substr($val['WS_Code'],0,-1);
            $str2 = @substr($val['WS_Code'],-1);
            if($str2 == 'B')
            {
                $section = "BRIEF";
            }
            else
            {
                $section = "PANTY";
            }

        }
        elseif($str == "SP")
        {
            $line = @substr($val['WS_Code'],0,-1);
            $str2 = @substr($val['WS_Code'],-1);
            if($str2 == "B")
            {
                $section = "BRIEF";
            }
            else
            {
                $section = "PANTY";
            }
        }
        elseif($str == "BL")
        {
            $section = "BRIEF";
            $str2 = @substr($str,-3,2);
            $line = $str2;
        }
        elseif($str == "PL")
        {
            $section = "PANTY";
            $str2 = @substr($str,-3,2);
            $line = $str2;
        }

And here is the rest of the code and how I use it:

<tr>
   <td><label class='control-label'>Section:</label></td>
   <td>
      <select class='form-control' name='cmbsection' id='cmbsection'>
         <option value='".$section."'>".$section."</option>
         ";
         $section_code = array();
         $section_code[] = $section;
         foreach($data['sectionlist'] as $value)
         {
         if(!in_array($value['sectionName'],$section_code))
         {
         echo "<option value='".$value['sectionName']."'>".$value['sectionName']."</option>";
         }
         }
         echo "
      </select>
   </td>
   <td><label class='control-label'>Line:</label></td>
   <td>
      <select class='form-control' name='cmbline' id='cmbline'>
         ";
         $line_code = array();
         $line_code[] = $line;
         echo "
         <option value='$line'>$line</option>
         ";
         foreach($data['linelist'] as $value)
         {
         if(!in_array($value['lineName'],$line_code))
         {
         echo "<option value='".$value['lineName']."'>".$value['lineName']."</option>";
         }
         }
         echo "
      </select>
   </td>
</tr>

Please note that I am echoing a table inside the controller and the error always points the $section and$line in the if(!in_array($value['lineName'],$line_code)) code.

enter image description here

enter image description here

Upvotes: 1

Views: 576

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94672

I would not call this a good solution but it will at least ensure that the variables $line and $section exist.

Your problem is that you are getting a code in WS_Code that you are not processing. As you only create the 2 variables if you see a code you have coded for, it must be that you have a new code that you had not expected and coded for.

The proper solution would be to work out what WS_Code you are getting in this code that you are not expecting.

In fact the proper solution would be to create a table that contains a code and a full_desc that you can query in order to expand these codes into whatever full description you want to use, and ensure that when a new code is created the table get updated accordingly

$line = 'new unexpected code recieved';
$section = 'new unexpected section recieved';

foreach($data['employee'] as $val)
    {
        if($val['Active']==0) {
            $status = strtoupper('Resigned');
        } else {
            $status = strtoupper('Active');
        }

        $str = substr($val['WS_Code'],0,-2);
        if($str == "PR") {
            $line = substr($val['WS_Code'],0,-1);
            $str2 = substr($val['WS_Code'],-1);
            if($str2 == 'B') {
                $section = "BRIEF";
            } else {
                $section = "PANTY";
            }

        }
        elseif($str == "SP") {
            $line = substr($val['WS_Code'],0,-1);
            $str2 = substr($val['WS_Code'],-1);
            if($str2 == "B") {
                $section = "BRIEF";
            } else {
                $section = "PANTY";
            }
        }
        elseif($str == "BL") {
            $section = "BRIEF";
            $str2 = substr($str,-3,2);
            $line = $str2;
        }
        elseif($str == "PL") {
            $section = "PANTY";
            $str2 = substr($str,-3,2);
            $line = $str2;
        }

Upvotes: 2

naf4me
naf4me

Reputation: 413

Please use the following code:

if(isset($line)){
   echo "<option value='$line'>$line</option>";
}

foreach($data['linelist'] as $value){
    if(isset($value['lineName']) && isset($line_code)) {
        if (!in_array($value['lineName'], $line_code)) {
            echo "<option value='" . $value['lineName'] . "'>" . $value['lineName'] . "</option>";
       }
   }
}

Upvotes: -1

Related Questions