CI框架增删改查以及跳转引入

 

增删改查

//添加
public function add(){
       $post = $this->input->post();
       $reg = $this->db->insert('uname',$post);
       if($reg){
           redirect('Show/shows');
       }else{
           echo '失败';
       }
}
//展示  分页搜索
public function shows(){
    $post = $this->input->get('uname');
    $p = $this->input->get('p');
    if($p<1) $p=1;
    $where="uname like '%$post%'";
    //分页
    $this->load->library('pagination');
    $count = $this->db->where($where)->count_all_results('uname');
    $config['base_url'] = site_url('Show/shows');
    $config['total_rows'] = $count;
    $config['per_page'] = '5';
    $config['use_page_numbers'] = TRUE;
    $this->pagination->initialize($config);
    $page = $this->pagination->create_links();
    //查询数据
    $resalt = $this->db->where($where)->limit(5,($p-1)*5)->get('uname')->result_array();
    foreach ($resalt as $k=>$v){
        $resalt[$k]['uname']=str_replace($post,"<font style='color:red'>$post</font>",$v['uname']);
    }
    if($post){
        $this->load->view('Show/shows_do',array('resalt'=>$resalt,'page'=>$page));
    }else{
        $this->load->view('Show/shows',array('resalt'=>$resalt,'page'=>$page));
    }
}
<center>
<body>
<input type="text" id="uname">
<button id="search">搜索</button>
<div id="box">
<table border="1">
    <tr>
        <td>ID</td>
        <td>用户名</td>
        <td>密码</td>
        <td>操作</td>
    </tr>
    <?php foreach ($resalt as $k=>$v){?>
    <tr>
        <td><input type="checkbox" name="check" class="check" value="<?php echo $v['id'] ?>" ><?php echo $v['id'] ?></td>
        <td><?php echo $v['uname'] ?></td>
        <td><?php echo $v['upwd'] ?></td>
        <td><a href="<?php echo site_url('Show/update')?>?id=<?php echo $v['id'] ?>">修改</a>||
            <a href="<?php echo site_url('Show/del') ?>?id=<?php echo $v['id'] ?>">删除</a>
        </td>
    </tr>
    <?php } ?>
</table>
    <button id="all">全选</button>
    <button id="none">全不选</button>
    <button id="fan">反选</button>
    <button id="delAll">批删</button>
<?php echo $page ?>
</div>

</center>
<script src="<?php echo base_url('public')?>/jquery-1.7.2.min.js"></script>
<script>
    //反选
    $('#box').delegate('#fan','click',function () {
        var check=$(".check");
        for(var i=0;i<check.length;i++){
                check[i].checked=!check[i].checked;
        }
    })
    //全选
    $('#box').delegate('#all','click',function () {
       $(":checkbox").attr('checked',true);
    })
    //全不选
    $('#box').delegate('#none','click',function () {
        $(":checkbox").attr('checked',false);
    })
    //批删
    $('#box').delegate('#delAll','click',function () {
        var check=$('.check');
        var id="";
        for(var i=0;i<check.length;i++){
            if(check[i].checked){
                id+=','+check[i].value;
            }
        }
        id=id.substr(1)
        //ajax请求
        $.ajax({
            type:"GET",
            url :"<?php echo site_url('Show/delAll')?>",
            data:"id="+id,
            success:function (msg) {
                //location.href="__SELF__";
                $('#box').html(msg)
            }
        })
    })
    var obj=Object;

    //分页
    $('#box').delegate('a[data-ci-pagination-page]','click',function () {
        var p=$(this).attr('data-ci-pagination-page');
        $(this).removeAttr("href");
        if(p<1) p=1
        getAjax(p)
    })

    //搜索
    $('#search').click(function () {
        obj=$('#uname').val();
        //p=1
        getAjax()
    });
    function getAjax(p) {
        //alert(p)
        $.ajax({
            type:"GET",
            url :"<?php echo site_url('Show/shows')?>",
            data:"uname="+obj+"&p="+p,
            success:function (msg) {
                $('#box').html(msg)
            }
        })
    }
</script>
//批删
public function delAll(){
    $id = $this->input->get('id');
    //echo $id;
    $resalt = $this->db->where("id in($id)")->delete('uname');
    if($resalt){
        header("refresh:2;url=shows");
        $this->load->view('Pages/index');
    }else{
        header("refresh:2;url=show");
        print('删除失败,2秒后跳到列表页');
    }
}
//删除
public function del(){
    $id = $this->input->get('id');
    //echo $id;
    $resalt = $this->db->where("id in($id)")->delete('uname');
    if($resalt){
        header("refresh:2;url=shows");
        $this->load->view('Pages/index');
    }else{
        header("refresh:2;url=show");
        print('删除失败,2秒后跳到列表页');
    }
}
//修改查询单条数据
public function update(){
    $id=$this->input->get('id');
    $resalt = $this->db->where(array('id'=>$id))->get('uname')->row_array();
    //print_r($resalt);
    $this->load->view('Show/update',array('resalt'=>$resalt));
}
//修改数据库
public function end(){
    $post = $this->input->post();
    $resalt = $this->db->where(array('id'=>$post['id']))->update('uname',$post);
    if($resalt){
        redirect('Show/shows');
    }else{
        echo "修改失败";
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值