为什么不能使用stdClass类型的对象作为数组

时间:2013-08-19 12:04:47

标签: php codeigniter codeigniter-2

我正在尝试分页,这是有效但不知道为什么我的编辑和删除功能停止工作。 这是我的控制器

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);

    }

这是我的模特

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();
         }

这是我的观点

 <?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()?>

在我的模型中,如果将result_array更改为result,则会出现错误“不能将stdClass类型的对象用作”中的数组。这就是为什么我使用了result_array,但是现在删除和编辑功能不起作用。我收到“试图获取非对象属性”的错误。 帮我结束此错误

3 个答案:

答案 0 :(得分:3)

如果您在模型中从result更改result_array,则应在视图中访问这些元素。

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

答案 1 :(得分:1)

在视图中更改单行:

<?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()?>

答案 2 :(得分:1)

试试这个

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