我们如何使用mongodb在codeigniter中通过id编辑数据

时间:2016-01-20 06:20:34

标签: mongodb codeigniter foreach

控制器:edit_details

 public function index()
 {
      $this->load->model('edit_details_model'


    $this->load->view('edit_details_view',$data);
 }

public function editdetails($id)
{

    $this->load->model('edit_details_model');
    $data['value']=$this->edit_details_model->edit_data($id);
    $this->load->view('edit_details_view',$data);   
}

public function update_details()
{
    $this->edit_details_model->update_data();
    $this->getall();
}
    }

model:edit_details_model

public function _construct()
{
    parent::construct();
    $this->load->database();
    $this->load->helper('form');
}

public function edit_data($id)
{
    $collection = $this->mongo_db->db->selectCollection('employee');
    $query=$collection->findOne( array( '_id' => ($id)) );
    return $query;
}

}

视图:edit_details_view

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
 //collection = $this->mongo_db->db->selectCollection('employee');
//		$cursor=$collection->findOne( array( '_id' => new MongoId($id)) );

if (is_array($value) || is_object($value))
{
foreach($value as $document)
{
	 $id=$document["_id"];

	$name=$document['name'];

	$email=$document['email'];
	$phone=$document['phone'];
	
	
	
}

}
?>
<form role="form" name="frm1" method="post" ><?php echo form_open('edit_details/update_details')?>

	NAME<input type="text" name="ntxt" value="<?php echo $name;?>" /><br />
    EMAIL<input type="etxt" name="etxt"  value="<?php echo $email?>"/><br />
    PHONE<input type="ptxt" name="ptxt"  value="<?php echo $phone?>"/><br />
    
    <input type="submit" name="submit" value="submit" />
    
</form>

</body>

它显示来自

的未定义名称,电子邮件和电话

1 个答案:

答案 0 :(得分:0)

试试这个

foreach($value as $document)
{
   $id=$document->_id;

   $name=$document->name;

   $email=$document->email;
   $phone=$document->phone; 
}