Welcome Guest, Not a member yet? Register   Sign In
Undefined variable: data
#1

[eluser]Unknown[/eluser]
This is my controller code

Code:
/*************************** Begin Add state ****************************/
function state() {
  $this->load->view('admin/state');
  $data['states'] = $this->master_model->getState();
}

function addNewState() {
  $this->load->view('admin/addstate');
}

function addState() {
  $this->load->library('form_validation');
  $this->form_validation->set_rules('st_StateName','State Name','required');
  if($this->form_validation->run() == false) {
   $this->load->view('admin/addstate');
  }
  else {
   $this->master_model->createNewState();
   echo "State Name added Successfully";
  }
}
/*************************** End Add state ****************************/

My model code is

Code:
function createNewState() {
        $data = array(
                'st_StateName' => $this->input->post('st_StateName'),
                'st_ActiveStatus' => $this->input->post('st_ActiveStatus')
  );
     return  $result= $this->db->insert('crm_state_master', $data);
}

function getState() {
  $this->db->select('st_StateName, st_ActiveStatus');
  $this->db->from('crm_state_master');
  $query = $this->db->get();
  $queryResult = $query->result();
  return $queryResult;
}

And this is my view code


Code:
<table align='center' width='100%'  id="gradient-style">
<tr>
  <td colspan="7">State Information |&lt;?php echo anchor('master/addNewstate', 'ADD NEW STATE'); ?&gt;</td>
</tr>
    
<tr>
  <th>State Name</th>
        <th>Access Level</th>
        <th>Edit</th>
    </tr>
&lt;?php
if(is_array($data)) {
foreach($data as $value): ?&gt;
  <tr>
  <td>&lt;?php echo $value->st_StateName;?&gt;</td>
  <td>&lt;?php echo $value->st_ActiveStatus;?&gt;</td>
  <td><a href="edit/&lt;?php echo $value-&gt;sr_Id; ?&gt;">Edit</a></td>
  </tr>
&lt;?php endforeach;
}
?&gt;
</table>

I get an error called "Undefined variable: data"
Please help me on this...
#2

[eluser]Aken[/eluser]
You're retrieving your $data array in the view improperly. Check the user guide, it explains what variables are available in your views.




Theme © iAndrew 2016 - Forum software by © MyBB