ci获取当前控制器,当前方法,增删改查

在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>




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值