使用codeigniter编辑记录

时间:2013-01-26 16:03:24

标签: php codeigniter

我在编辑数据库中的记录时遇到问题。我有一个表格,显示视图页面中的所有cliends记录,并在它们旁边我有一个锚点链接来编辑记录。

我将id发送到名为site / update的控制器函数,然后使用

获取id
 $id = $this->uri->segment(3);

然后我将id发送到findRecord($ id)模型以获取与数据库中的id相对应的记录。

如果我打印我的查询

 Array ( [records] => Array ( [0] => stdClass Object ( [id] => 2 [name] =>
 sdf [surname] => sdf [contact] => 35353 [email] => blabla@gmail.com ) ) ) 

我可以在控制器中打印,而不是在视图中打印

我该怎么做?问题出在哪里?

控制器代码:

    <?php

    class Site extends CI_Controller 
    {
        function __construct()
        {
            parent::__Construct();
            $this->is_logged_in();
        }

        function members_area()
        {
            $data=array();

            if($query=$this->site_model->getrecords()){
                $data['records']=$query;
            }
            $this->load->view('logged_in_area',$data);
        }

        function add() // just for sample
        {
            $this->load->view('add_clients');
        }

        function is_logged_in()
        {
            $is_logged_in = $this->session->userdata('is_logged_in');
            if(!isset($is_logged_in) || $is_logged_in != true)
            {
                echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';    
                die();      
                //$this->load->view('login_form');
            }       
        }
        function create(){
                $this->load->library('form_validation');///// form validation to add clients

            // field name, error message, validation rules
            $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
            $this->form_validation->set_rules('surname', 'Surname', 'trim|required|xss_clean');
            $this->form_validation->set_rules('contact', 'Contact Number', 'trim|required|min_length[4]');
            $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
            if($this->form_validation->run() == FALSE)
            {
                $this->load->view('add_clients');
            }else{
                $data=array('name'=>$this->input->post('name'),
                'surname'=>$this->input->post('surname'),
                'contact'=>$this->input->post('contact'),
                'email'=>$this->input->post('email'));
                $this->site_model->addRecords($data);
                $this->members_area();  
                }
        }
        function delete($id){
                $this->site_model->deleteRow($id);
                $this->members_area();
        }
        function update(){
            $data2=array();
            $id = $this->uri->segment(3);
        if($query=$this->site_model->findRecord($id)){
            $data2['records']=$query;
            //print_r($data2);
            $this->load->view('logged_in_area',$data2);
                }else{echo' nothing';}
            //$this->load->model('employesModel');
            //if($t=$this->site_model->findRecord($id)){
            //$this->load->view('update_clients',$data);
            //$this->site_model->updateRecord($data,$id);
        }
    }

视图模型

    <body>
        <h2 style="text-align:left">Welcome Back, <?php echo $this->session->userdata('username'); ?>!</h2>
         <p>This section represents the area that only logged in members can access.</p><br/>
         <h2>Clients List</h2>
         <?php
         if(isset($records)):
         echo'<table align="left"  width="40%" cellpadding="6" cellspacing="3" border="0">
         <tr><td align="left"><b>Name</b></td><td align="left"><b>Surname</b></td><td align="left"><b>Phone number</b></td><td align="left"><b>Email address</b></td><td>
         </td></tr>';
          foreach ($records as $row): ?>
        <tr>
            <td><?php echo $row->name;?></td>
            <td><?php echo $row->surname;?></td>
            <td><?php echo $row->contact;?></td>
            <td><?php echo $row->email;?></td>
            <td align="center"><?php echo anchor("site/delete/$row->id",'Delete');?>&nbsp;<?php echo anchor("site/members_area/$row->id",'Update')?></td>
        </tr>
          <?php endforeach; echo '</table>'?>
          <?php else: ?>
          <h2>No records were returned</h2>
          <?php endif; ?><br/><br/><br/><br/><?php if(isset($data2))var_dump($data2);?><br/><br/>
    <p>
         <?php echo anchor('site/add','Add clients');?>&nbsp;
         <?php echo anchor('login/logout', 'Logout'); ?>
    </p>
    </body>
    </html> 

最后是模型

function findRecord($id){
    $query = $this->db->get_where('clients', array('id' => $id));
    return  $query->result();
   }
 }

2 个答案:

答案 0 :(得分:0)

您没有获取任何数据来查看文件,因为您没有向其发送数据。看看你的代码,你有一个错误的拼写:

if($query**=**$this->site_model->getrecords()){
            $data['records']=$query;}

尝试将=更改为==。

答案 1 :(得分:0)

看起来你在数组中接收一个数组,尝试使用数组$ records ['0']的索引而不是$ records。

E.g: $records['0'] as $row