摘要:登陸后跳轉(zhuǎn)的首頁(userInfo.php):<?php /** * 從session中提取登錄者信息,并且通過smarty渲染管理者頁面,傳遞參數(shù) */ session_start(); //加載smarty require __DIR__.'/config/config.php'; //加載medoo框架 require&nbs
登陸后跳轉(zhuǎn)的首頁(userInfo.php):
<?php /** * 從session中提取登錄者信息,并且通過smarty渲染管理者頁面,傳遞參數(shù) */ session_start(); //加載smarty require __DIR__.'/config/config.php'; //加載medoo框架 require __DIR__.'/connect.php'; //將管理員信息從后臺傳到前臺,并渲染出模板 $admin = $_SESSION['name']; $id = $_SESSION['id']; //查詢數(shù)據(jù)庫的數(shù)據(jù),將數(shù)據(jù)傳到前臺 $table = 'user'; $field = ['id','name','password','sex','age','email','create_time']; //查詢結(jié)果為數(shù)組 $rows = $db->select($table,$field); //print_r($rows);die; $smarty->assign('rows',$rows); $smarty->assign('admin',$admin); $smarty->assign('id',$id); $smarty->display('userInfo.html');
用戶添加user_add.php
<?php /** *處理添加界面?zhèn)鬟^來的數(shù)據(jù) */ //var_dump($_POST); require __DIR__.'/connect.php'; //2.執(zhí)行添加 $table = 'user'; $data = [ 'name'=>$_POST['name'], 'sex'=>$_POST['gender'], 'age'=>$_POST['age'], 'email'=>$_POST['email'], 'password'=>sha1($_POST['password']), 'create_time'=>time() ]; $stmt = $db->insert($table,$data); $status = 0; $message = ''; if($stmt->rowCount()){ $status = 1; $message='添加成功~~'; }else{ $message = '添加失敗~~'; } echo json_encode(['status'=>$status,'message'=>$message]);
渲染修改界面(xr_update.php)
<?php /** * 渲染添加界面 */ //var_dump($_POST); require __DIR__.'/config/config.php'; require __DIR__.'/connect.php'; $id = $_POST['id']; //構(gòu)建查詢條件 $table = 'user'; $field = ['id','name','age','email','password']; $where = ['id'=>$id]; //查詢,傳值,渲染更新界面 $rows = $db->select($table,$field,$where); //var_dump($rows);die; $smarty->assign('rows',$rows); //$smarty->display('update.html'); echo json_encode($smarty->display('update.html'));
用戶修改(user_update.php)
<?php /** *修改用戶 */ //var_dump($_POST);die; //1.實(shí)例化Medoo框架類 require __DIR__.'/connect.php'; $table = 'user'; $data=[ 'name'=>$_POST['name'], 'password'=>$_POST['password'], 'age'=>$_POST['age'], 'email'=>$_POST['email'] ]; $where['id']=$_POST['id']; $stmt = $db->update($table,$data,$where); $status = 0; $message=''; if($stmt->rowCount()){ $status =1; $message='更新成功~~'; }else{ $message = '更新失敗~~'; } echo json_encode(['status'=>$status,'message'=>$message]);
刪除用戶(del.php)
<?php /** * 刪除數(shù)據(jù) */ //var_dump($_POST); require __DIR__.'/connect.php'; //前臺傳過來的需要?jiǎng)h除數(shù)據(jù)的id $id = $_POST['id']; $table = 'user'; $where['id']=$id; $stmt = $db->delete($table,$where); $status=0; $message=''; if($stmt->rowCount()>0){ $status =1; $message = '刪除成功~~'; }else{ $message = '刪除失??!'; } echo json_encode(['status'=>$status,'message'=>$message]);
首頁js代碼(userInfo.html),主要通過點(diǎn)擊事件實(shí)現(xiàn)
<script type="text/javascript"> function logout(){ $.post('../logout.php', function(data){ window.location.href='temp/login.html'; } ) } function add() { $.post('../user_add.php', $('#add').serialize(), function(data){ data = JSON.parse(data); if(data.status==1){ alert(data.message) }else{ alert(data.message); } window.location.reload();//重新加載頁面,顯示所有數(shù)據(jù) }) } function del(id) { //console.log(id); $.post('../del.php', {id:id}, function(data){ //console.log(data); data = JSON.parse(data); if(data.status==1){ alert(data.message) }else{ alert(data.message); } window.location.reload();//重新加載頁面,顯示所有數(shù)據(jù) } ) } function update(id){ // console.log(id); $.post('../xr_update.php', {id:id}, function(data){ //console.log(data); $('body').html(data); }) } </script>
思路以及總結(jié):主要使用bootstrap,smarty,medoo實(shí)現(xiàn),包括登錄,注冊,對用戶進(jìn)行添加,修改和刪除功能。數(shù)據(jù)表包括管理員和用戶兩張表。使用了bootstrap中的表單,模態(tài)框,在使用$.post()函數(shù)傳參數(shù)時(shí),參數(shù)寫成json格式的出現(xiàn)問題,修改smarty配置中的定界符,因?yàn)槟J(rèn)定界符與json數(shù)據(jù)的相同,發(fā)生沖突。實(shí)現(xiàn)添加用戶時(shí),使用了模態(tài)框,修改信息時(shí),使用$.post()從后臺傳json格式頁面,然后通過$.('').html方式顯示出來,這種方式存在問題,顯示出修改界面不進(jìn)行修改,返回時(shí)不能進(jìn)入首頁.怎么實(shí)現(xiàn)傳過來的界面在另一個(gè)地址顯示,不在返回界面展示,分頁功能沒有實(shí)現(xiàn)。通過這個(gè)系統(tǒng),鞏固了bootstrap,smarty和medoo的知識,系統(tǒng)還存在小問題,后期會進(jìn)行不斷的完善。需要多鞏固之前的知識,還需要課后多練習(xí)。
批改老師:天蓬老師批改時(shí)間:2019-01-22 09:12:28
老師總結(jié):通過以上操作, 對MVC的軟件開發(fā)架構(gòu),應(yīng)該有了新的認(rèn)識, 開發(fā)的思維也有了新高度