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

This is my controller code

/*************************** Begin Add state ****************************/
function state() {
  $data['states'] = $this->master_model->getState();

function addNewState() {

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

My model code is

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');
  $query = $this->db->get();
  $queryResult = $query->result();
  return $queryResult;

And this is my view code

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

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

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