shiv223
shiv223

Reputation: 45

why Cannot use object of type stdClass as array in

I am trying do pagination,which is working but dont know why my edit and delte functionality stopped working. This is My Controller

function view($page=0){
                $config = array();
                $config["base_url"] = base_url() . "index.php/employee/view";
                $config["total_rows"] = $this->employee_model->getTotalEmployeeCount();
                $config["per_page"] =5;
                $this->pagination->initialize($config);
                $this->data["results"] = $this->employee_model->getEmployee($config["per_page"], $page);
                $this->data["links"] = $this->pagination->create_links();
                $this->data['title'] = 'Payroll System';
                $this->data['message'] = $this->session->flashdata('message');
                $this->load->view('employee_view', $this->data);

    }

This is My Model

function getTotalEmployeeCount() {
            return $this->db->count_all('user');
        }
      function getEmployee($limit, $start) {
            $this->db->limit($limit, $start);
            $this->db->select('id,firstname,lastname,presentadress,contactno,dateofjoining,email');
            $qry= $this->db->get('user');
        return $qry->result_array();
         }

This is my view

 <?php
    foreach ($results as $m){
    ?>
      <tr style="text-align:center;">
                  <td><?php echo $m['id'] ?></td>
                  <td><?php echo $m['firstname'] ?></td>
                  <td><?php echo $m['lastname'] ?></td>
                  <td><?php echo $m['dateofjoining'] ?></td>
                  <td><?php echo $m['presentadress'] ?></td>
                  <td><?php echo $m['contactno'] ?></td>
                  <td><?php echo $m['email'] ?></td>
   <td><a href="<?php echo site_url('employee/edit_employee/'.$m->id) ?>" class="btn btn-primary btn-mini">Edit</a></td>
          <td>
          <?php 
          echo anchor('employee/delete_employee/'.$m->id, 'Delete', array('onClick' => "return confirm('Are you sure you want to delete?')"));
          ?>
          </td>
    <?php
    }
    ?>
    <?php echo $this->pagination->create_links()?>

In my model if change result_array to result then it gives error of "Cannot use object of type stdClass as array in".Thats why i used result_array,But now delete and edit functionality not working.I am getting the error of "Trying to get property of non object". Help me to end this error

Upvotes: 1

Views: 9384

Answers (3)

Sumit Nair
Sumit Nair

Reputation: 327

Just try like this

<td><?php echo $m->id; ?></td>
<td><?php echo $m->firstname; ?></td>
<td><?php echo $m->lastname; ?></td>

Upvotes: 1

Rajeev Ranjan
Rajeev Ranjan

Reputation: 4142

change single line in your view :

<?php
    foreach ($results as $m){
    ?>
      <tr style="text-align:center;">
                  <td><?php echo $m['id'] ?></td>
                  <td><?php echo $m['firstname'] ?></td>
                  <td><?php echo $m['lastname'] ?></td>
                  <td><?php echo $m['dateofjoining'] ?></td>
                  <td><?php echo $m['presentadress'] ?></td>
                  <td><?php echo $m['contactno'] ?></td>
                  <td><?php echo $m['email'] ?></td>
   <td><a href="<?php echo site_url('employee/edit_employee/'.$m->id) ?>" class="btn btn-primary btn-mini">Edit</a></td>
          <td>
          <?php 
          //change  here ///
          echo anchor('employee/delete_employee/'.$m['id'], 'Delete', array('onClick' => "return confirm('Are you sure you want to delete?')"));
          ?>
          </td>
    <?php
    }
    ?>
    <?php echo $this->pagination->create_links()?>

Upvotes: 0

Eswara Reddy
Eswara Reddy

Reputation: 1647

If you change result from result_array in model, in view you should access those elements like this.

<td><?php echo $m->id; ?></td>
<td><?php echo $m->firstname; ?></td>
<td><?php echo $m->lastname; ?></td>

Upvotes: 3

Related Questions