codeigniter视图,添加,更新和删除 [英] codeigniter view, add, update and delete

查看:170
本文介绍了codeigniter视图,添加,更新和删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是codeigniter的新手,还在学习。任何人都可以帮助在基本视图中的示例,添加,更新,删除操作和查询在codeigniter将非常​​感激。



只是一个简单的例如为新手创建地址簿。 p>

感谢,



最好的问候

解决方案

Codeigniter中的一些示例查询

 类名扩展模型{
function addRecord($ yourname ){
$ this-> db-> set(name,$ yourname);
$ this-> db-> insert(names);
return $ this-> db-> _error_number(); //返回最后一个查询中出现的错误
}
function updateRecord($ yourname){
$ this-> db-> set(name,$ yourname);
$ this-> db-> update(names);
}
function deleteRecord($ yourname){
$ this-> db-> where(name,$ yourname);
$ this-> db-> delete(names);
}
function selectRecord($ yourname){
$ this-> db-> select(name,name_id);
$ this-> db-> from(names);
$ this-> db-> where(name,$ yourname);
$ query = $ this-> db-> get();
return $ this-> db-> result();
}
function selectAll(){
$ this-> db-> select(name);
$ this-> db-> from(names);
return $ this-> db-> get();
}
}

更多信息和更多CRUD在< =http://codeigniter.com/user_guide/database/active_record.html =nofollow noreferrer> codeigniter活动记录文档
更多关于此处



示例控制器



class names_controller extends Controller {
function addPerson(){
$ this-> load-> Model(Names);
$ name = $ this-> input-> post(name); //从表单提交数据
$ name = $ this-> xss-> clean();
$ error = $ this-> Names-> addRecord($ name);
if(!$ error){
$ results = $ this-> Names-> selectAll();
$ data ['names'] = $ results-> result();
$ this-> load-> view(show_names,$ data);
} else {
$ this-> load-> view(error);
}
}
}

href =http://codeigniter.com/user_guide/general/controllers.html =nofollow noreferrer>此处



示例视图 - show_names.php

 < table> 
< tr>
< td>名称< / td>
< / tr>
<?php foreach($ names as $ row):?>
< tr>< td><?ph echo $ row-> name; ?>< / td>< / tr>
<?php endforeach; >
< / table>

有关codeigniter视图的详细信息,请访问此处


I'm newbie in codeigniter and still learning. Anyone can help for sample in basic view, add, update, delete operation and queries in codeigniter will gladly appreciated.

Just a simple one like creating addressbook for newbie.

thanks,

best regards

解决方案

Some sample queries in Codeigniter

class Names extends Model {
  function addRecord($yourname) {
    $this->db->set("name", $yourname);
    $this->db->insert("names");
    return $this->db->_error_number(); // return the error occurred in last query
  }
  function updateRecord($yourname) {
    $this->db->set("name", $yourname);
    $this->db->update("names");
  }
  function deleteRecord($yourname) {
    $this->db->where("name", $yourname);
    $this->db->delete("names");
  }
  function selectRecord($yourname) {
    $this->db->select("name, name_id");
    $this->db->from("names");
    $this->db->where("name", $yourname);
    $query = $this->db->get();
    return $this->db->result();
  }
  function selectAll() {
     $this->db->select("name");
     $this->db->from("names");
     return $this->db->get();
  }
}

More information and more ways for CRUD in codeigniter active record documentation More about error number over here

A sample controller

class names_controller extends Controller {
   function addPerson() {
      $this->load->Model("Names");
      $name = $this->input->post("name"); // get the data from a form submit
      $name = $this->xss->clean();
      $error = $this->Names->addRecord($name);
      if(!$error) {
         $results = $this->Names->selectAll();
         $data['names'] = $results->result();
         $this->load->view("show_names", $data);
      } else {
         $this->load->view("error");
      }
   }
}

More about controllers over here

A sample view - show_names.php

<table>
  <tr>
    <td>Name</td>
  </tr>
  <?php foreach($names as $row): ?>
  <tr><td><?ph echo $row->name; ?></td></tr>
  <?php endforeach; ?>
</table>

More about codeigniter views over here

这篇关于codeigniter视图,添加,更新和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆