iT邦幫忙

DAY 28
7

[鐵鷹練成]CodeIgniter + Bootstrap系列 第 25

[鐵鷹練成]CI之會員登入(新增 修改 刪除)

今天內容會比較多 最後大爆發!!
在views新增一個ModifyUser.php

<?php
$link = 'http://localhost/test/';
?>

<html xmlns="http://www.w3.org/1999/xhtml">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>公佈欄</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="<?=$link;?>css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="<?=$link;?>css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="<?=$link;?>js/jquery.js"></script>
<script src="<?=$link;?>js/bootstrap.min.js"></script>
<style>
#heig{
     height:10px;
}
#color{
     background-color: #666;
     color: #FFF;
}
#color2{
     background-color: #999;
}
</style>


<div class="container">
  <form id="form1" name="form1" method="post" action="<?=$link;?>index1.php/users/UserModify">
  <?php foreach($query->result_array() as $row):?>
    <table width="1200" border="1" class="table table-bordered table-condensed">
      <tr>
        <td colspan="2" id="color"><div align="center"><strong>修改會員資料</strong></div></td>
      </tr>
      <tr>
        <td id="color2"><div align="center"><strong>帳號</strong></div></td>
        <td><label>
          <input name="account" type="text" id="account" value="<?=$row['account'];?>" />
          <input name="id" type="hidden" id="id" value="<?=$row['id'];?>" />
        </label></td>
      </tr>
      <tr>
        <td id="color2"><div align="center"><strong>密碼</strong></div></td>
        <td><label>
          <input name="password" type="text" id="password" value="<?=$row['password'];?>" />
        </label></td>
      </tr>
      <tr>
        <td id="color2"><div align="center"><strong>備註</strong></div></td>
        <td><label>
          <input name="remark" type="text" id="remark" value="<?=$row['remark'];?>" />
        </label></td>
      </tr>
      <tr>
        <td id="color2"><div align="center"><strong>權限</strong></div></td>
        <td><label>
          <select name="class" id="class">
            <option value="<?=$row['class'];?>" selected="selected">
            <?php
               if($row['class']==0){
                    echo "最高權限";
               }else{
                    echo "一般使用者";
               }
               ?>
            </option>
            <option value="1">一般使用者</option>
            <option value="0">最高權限</option>
          </select>
        </label></td>
      </tr>
      <tr>
        <td colspan="2" id="color">
        <div class="btn-group">
        <button class="btn" type="submit">修改</button>
        <button class="btn" type="reset">重填</button>
        <button class="btn" type="button" onClick="javascript:history.back(1)">回會員管理</button>
        </div>
        </td>
      </tr>
    </table>
    <?php endforeach; ?>
  </form>
</div>

然後views/user.php改一下

<?php
$link = 'http://localhost/test/';
?>

<html xmlns="http://www.w3.org/1999/xhtml">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>公佈欄</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="<?=$link;?>css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="<?=$link;?>css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="<?=$link;?>js/jquery.js"></script>
<script src="<?=$link;?>js/bootstrap.min.js"></script>
<style>
#heig{
     height:10px;
}
#color{
     background-color: #666;
     color: #FFF;
}
#color2{
     background-color: #999;
}
</style>


<div class="container">
  <table width="1200" border="1" class="table table-bordered table-condensed table-striped table-hover">
    <tr>
      <td colspan="5" id="color"><div align="center"><strong><h2>會員管理</h2></strong></div></td>
    </tr>
    <tr>
      <td width="150"><div align="center"><strong>快速新增會員</strong></div></td>
      <td colspan="4"><form id="form1" name="form1" method="post" action="<?=$link;?>index1.php/users/AddUser" class="form-inline">
      <div id="heig">
        帳號:
        <label>
          <input type="text" name="account" id="account" />
        </label>
        密碼:
        <label>
          <input type="password" name="password" id="password" />
        </label>
        <label>
          <input type="submit" value="新增" class="btn" />
        </label>
        </div>
      </form></td>
    </tr>
    <tr>
      <td id="color2"><div align="center"><strong>項次</strong></div></td>
      <td id="color2"><div align="center"><strong>帳號</strong></div></td>
      <td id="color2"><div align="center"><strong>備註</strong></div></td>
      <td id="color2"><div align="center"><strong>權限</strong></div></td>
      <td id="color2"><div align="center"><strong>管理</strong></div></td>
    </tr>
    <?php foreach($query->result_array() as $key=>$row):?>
    <tr>
      <td><div align="center"><?=$key+1;?></div></td>
      <td><div align="center"><?=$row['account'];?></div></td>
      <td><div align="center"><?=$row['remark'];?></div></td>
      <td><div align="center">
       <?php
       if($row['class']=="0"){
            echo "最高權限";
       }else{
            echo "一般會員";
            }
       ?>
       </div></td>
      <td><div align="center">
      <div class="btn-group">
      <button class="btn" type="button" onclick="window.location='<?=$link;?>index1.php/users/ModifyUser/<?=$row['id'];?>'">修改</button>
      <a href="<?=$link;?>index1.php/users/DelUser/<?=$row['id'];?>"><button class="btn" type="button" onclick="if(confirm('您確定刪除嗎?')) return true;else return false">刪除</button></a>
      </div>
      </div></td>
    </tr>
    <?php endforeach; ?>
  </table>
</div>

在來controllers/users.php

<?php
class users extends CI_Controller{
   
    function __construct() {
        parent::__construct();
        //載入資料庫行為
        $this->load->model('users_sql');
        /**請參考 http://www.codeigniter.org.tw/user_guide/helpers/url_helper.html **/
        $this->load->helper('url');
    }
 
    /********************************頁面顯示*****************************************/
    function index(){
        $this->load->view('login');
    }
   
    function login(){
        $where = $this->input->post();
        $query = $this->users_sql->where_array($where,'users');
        /*num_rows() 請參考  :   http://www.codeigniter.org.tw/user_guide/database/results.html*/
        if($query->num_rows() > 0){
             $this->user();
        }else{
            redirect(site_url().'users/index');
        }
    }
   
    function user(){
        $data['query'] = $this->users_sql->all('users');
        $this->load->view('user',$data);
    }
   
    function ModifyUser($id){
        $array = array('id'=>$id);
        $data['query'] = $this->users_sql->where_array($array,'users');
        $this->load->view('ModifyUser',$data);
    }
   
    /******************新增**************************/
   
    function AddUser(){
        $all = $this->input->post();
        $this->users_sql->add($all,'users');
        redirect(site_url().'users/user');
    }
   
    /*****************修改**************************/
   
    function UserModify(){
        $array = array('id'=>$this->input->post('id'));
        $all = $this->input->post();
        $this->users_sql->where_update($array,$all,'users');
        redirect(site_url().'users/user');
    }
   
    /**********************刪除************************/
   
    function DelUser($id){
        $array = array('id'=>$id);
        $this->users_sql->del($array,'users');
        redirect(site_url().'users/user');
    }
   
}
?>

models/users_sql.php

<?php
class users_sql extends CI_Model{
   
    function __construct() {
        parent::__construct();
         $this->load->database();
    }
   
    //條件搜尋   
    function where_array($array,$tbale){
        $query = $this->db->where($array)->get($tbale);
        return $query;
    }
    //全部搜尋
    function all($table){
        $query = $this->db->get($table);
        return $query;
    }
    //新增資料
    function add($all,$table){
        $this->db->insert($table,$all);
    }
    //條件 修改
    function where_update($array,$all,$table){
        $this->db->where($array)->update($table,$all);
    }
    //條件 刪除
    function del($array,$table){
        $this->db->where($array)->delete($table);
    }
   
}
?>

完成圖






大概注解一下~~!
待續......


上一篇
[鐵鷹練成]CI之會員登入(會員管理介面)
下一篇
[鐵鷹練成]CI之融合(前言篇)
系列文
[鐵鷹練成]CodeIgniter + Bootstrap31

2 則留言

0
CalvinKuo
iT邦大師 7 級 ‧ 2013-10-13 21:35:33

讚沙發拍手

謝謝

0
stylepan12
iT邦新手 3 級 ‧ 2013-10-14 12:26:00

拍手拍手拍手

謝謝

我要留言

立即登入留言