ci获取当前控制器,当前方法,增删改查
2016-10-19 09:31
295 查看
在core里新建MY_controller
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class MY_Controller extends CI_Controller{ public function __construct(){ parent::__construct(); echo "你当前的控制器:"."<font style='color:red'>".$this->router->class."</font>"; echo "<br>"; echo "你当前的方法:"."<font style='color:red'>".$this->router->method."</font>"; } } ?> C层
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Index extends MY_Controller { /* 构造方法 */ public function __construct(){ parent::__construct(); } /** * Index Page for this controller. * * Maps to the following URL * http://example.com/index.php/welcome * - or - * http://example.com/index.php/welcome/index * - or - * Since this controller is set as the default controller in * config/routes.php, it's displayed at http://example.com/ * * So any other public methods not prefixed with an underscore will * map to /index.php/welcome/<method_name> * @see https://codeigniter.com/user_guide/general/urls.html */ /**显示表单 * [index description] * @return [type] [description] */ public function index() { $this->load->view('Index/wel.html'); } /** * 添加方法 * */ public function add_pro(){ $data=array( 'names'=>$this->input->post('names'), 'url'=>$this->input->post('url'), 'tell'=>$this->input->post('tell'), 'show'=>$this->input->post('show'), 'sort'=>$this->input->post('sort') ); $res=$this->db->insert('ci0509',$data); if($res){ redirect('Index/show'); }else{ echo "添加失败"; } } /** * 显示列表 */ public function show(){ $sql="select * from ci0509"; //var_dump($sql); $res=$this->db->query($sql); $list=$res->result_array(); //var_dump($list); $data['list']=$list; $this->load->view('Index/show_list.html',$data); } /** * 删除 */ public function del(){ $id=$this->input->get('id'); $res=$this->db->delete('ci0509',array('id'=>$id)); if($res){ redirect('Index/show'); }else{ echo "删除失败"; } } /** * 修改 */ public function update(){ $id=$this->input->get('id'); $list=$this->db->get_where('ci0509',array('id'=>$id))->result_array(); $data['list']=$list; $this->load->view('Index/updates.html',$data); } /** * 执行修改 */ function update_pro(){ $id=$this->input->post('id'); $data=array( 'names'=>$this->input->post('names'), 'url'=>$this->input->post('url'), 'tell'=>$this->input->post('tell'), 'show'=>$this->input->post('show'), 'sort'=>$this->input->post('sort') ); $this->db->where('id',$id); if($this->db->update('ci0509',$data)){ redirect('Index/show'); }else{ echo "修改失败"; } } } 视图show
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <table border="1"> <tr> <td>编号</td> <td>商品名称</td> <td>商品网址</td> <td>商品描述</td> <td>是否显示</td> <td>排序</td> <td>操作</td> </tr> <?php foreach($list as $v){?> <tr> <td><?php echo $v['id'];?></td> <td><?php echo $v['names'];?></td> <td><?php echo $v['url'];?></td> <td><?php echo $v['tell'];?></td> <td><?php if($v['show']==1){ echo "√"; }else{ echo "×"; }?></td> <td><?php echo $v['sort'];?></td> <td><a href="<?php echo site_url('Index/del?id='.$v['id']) ?>">删除</a>||<a href="<?php echo site_url('Index/update?id='.$v['id'])?>">修改</a></td> </tr> <?php } ?> </table> </body> </html> 视图update
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>表单页面</title> </head> <body> <?php foreach($list as $v){ ?> <form action="<?php echo site_url('Index/update_pro'); ?>" method="post"> <input type="hidden" name="id" value='<?php echo $v['id'] ?>' > <table> <tr> <td>商品名称</td> <td><input type="text" name="names"value="<?php echo $v['names'] ?>"></td> </tr> <tr> <td>品牌网址</td> <td><input type="text" name="url" value="<?php echo $v['url'] ?>"></td> </tr> <tr> <td>品牌描述</td> <td><textarea name="tell" cols="30" rows="10"><?php echo $v['tell'] ?></textarea></td> </tr> <tr> <td>是否显示</td> <td><input type="radio" name="show" value="1" <?php if($v['show']==1){ echo "checked"; } ?>/>显示 <input type="radio" name="show" value="0"<?php if($v['show']==0){ echo "checked";} ?>/>不显示 </td> </tr> <tr> <td>排序</td> <td><input type="text" name="sort" value="<?php echo $v['sort'] ?>"> </td> </tr> <tr> <td></td> <td><input type="submit" value="修改"></td> </tr> </table> </form> <?php } ?> </body> </html> 视图wel
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>表单页面</title> </head> <body> <form action="<?php echo site_url('index/add_pro'); ?>" method="post"> <table> <tr> <td>商品名称</td> <td><input type="text" name="names"></td> </tr> <tr> <td>品牌网址</td> <td><input type="text" name="url"></td> </tr> <tr> <td>品牌描述</td> <td><textarea name="tell" cols="30" rows="10"></textarea></td> </tr> <tr> <td>是否显示</td> <td><input type="radio" name="show" value="1" >显示 <input type="radio" name="show" value="0">不显示 </td> </tr> <tr> <td>排序</td> <td><input type="text" name="sort"> </td> </tr> <tr> <td></td> <td><input type="submit" value="添加"></td> </tr> </table> </form> </body> </html>
相关文章推荐
- ci获取当前控制器类名和方法名和PHP获取当前类名、函数名、方法名方法
- ci得到控制器类名和函数名的方法和PHP获取当前类名、函数名、方法名方法
- ci获取当前url链接的分组,控制器,方法
- 获取当前控制器,封装方法可直接调用
- MVC 在action方法中获取当前action的控制器名和action名
- iOS如何获取当前View所在控制器的方法
- yii2.0如何获取当前的控制器名与方法名
- iOS获取当前屏幕控制器的方法
- yii中获取当前模块,控制器,方法
- ios 获取当前控制器的方法
- 获取当前视图所在控制器的方法
- 获取当前控制器的方法
- yii2 视图获取当前控制器的方法名称
- CI框架获取控制器名和方法名
- C#获取当前程序运行路径的方法集合
- C# 获取当前路径方法
- 获取当前窗口标题的方法
- Java在不同环境下获取当前路径的方法
- MFC获取系统当前时间的方法
- Mysql获取当前日期的前几天日期的方法