摘要:index.php:<?php require __DIR__.'/config/config.php'; $fileds=['id','name','sex','age','email','creat_time']; $table='user'; $ro
index.php:
<?php require __DIR__.'/config/config.php'; $fileds=['id','name','sex','age','email','creat_time']; $table='user'; $rows=$db->select($table,$fileds); $smarty->assign('rows',$rows); $smarty->display('index.html');
index.html:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>江湖人士管理系統(tǒng)</title> <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="tmp/css/style.css"> </head> <body> <div class="content"> <h1>江湖人士管理系統(tǒng) <span style="float: right;margin-right: 20px;"> <a href="/medoo/add.php">增加英雄</a></span></h1> <table class="table table-bordered table-striped table-hover" cellpadding="0" cellspacing="0"> <thead> <tr> <th>id</th> <th>姓名</th> <th>性別</th> <th>年齡</th> <th>郵箱</th> <th>注冊時(shí)間</th> <th>操作</th> </tr> </thead> <tbody> {foreach $rows as $res} <tr> <td>{$res.id}</td> <td>{$res.name}</td> <td>{if $res.sex eq 0}男{else}女{/if}</td> <td>{$res.age}</td> <td>{$res.email}</td> <td>{$res.creat_time|date_format:"%Y-%m-%d"}</td> <td><a href="/medoo/edit.php?id={$res.id}">編輯</a> <a href="/medoo/del.php?id={$res.id}">刪除</a></td> </tr> {foreachelse} <tr> <td colspan="7" style="color: red">暫無數(shù)據(jù)</td> </tr> {/foreach} <tr> <td colspan="7">當(dāng)前共<span style="color:red;font-weight: bold">{count($rows)}</span>條數(shù)據(jù)</td> </tr> </tbody> </table> </div> </body> </html>
add.php:
<?php require __DIR__.'/config/config.php'; if($_POST){ $data[]=array(); $data=[ 'name'=>$_POST['name'], 'sex'=>$_POST['sex'], 'age'=>$_POST['age'], 'email'=>$_POST['email'], 'password'=>sha1($_POST['password']), 'creat_time'=>time() ]; if(!$stmt=$db->select('user','name',['name'=>$_POST['name']])){ $stmt=$db->insert('user',$data); if($stmt->rowCount()){ alert('添加成功','http://www.a.com/medoo/'); }else{ exit('添加失敗'); } }else{ exit('用戶已存在'); } } $smarty->display('add.html');
add.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="tmp/css/style.css"> </head> <body> <div class="edit"> <div class="position">當(dāng)前位置: <a href="/medoo/">主頁</a>>編輯</div> <h1>編輯</h1> <table class="table"> <form action="" method="post"> <input type="hidden" name="id"> <tr> <td>姓名</td> <td><input type="text" name="name"></td> </tr> <tr> <td>密碼</td> <td><input type="text" name="password"></td> </tr> <tr> <td>性別</td> <td> <select name="sex"> <option value="0">男</option> <option value="1">女</option> </select> </td> </tr> <tr> <td>年齡</td> <td><input type="text" name="age"></td> </tr> <tr> <td>郵箱</td> <td><input type="text" name="email"></td> </tr> <tr> <td colspan="2" style="background-color:transparent!important;"> <input type="submit" class="btn-success btn-lager" value="提交"> </td> </tr> </form> </table> </div> </body> </html>
edit.php:
<?php require __DIR__.'/config/config.php'; $id=$_GET['id']; $where=['id'=>$id]; $fileds=['id','name','sex','age','email']; $row=$db->get('user',$fileds,$where); if($_POST) { $data[] = array(); if($_POST['sex']!='女'){ $_POST['sex']=0; }else{ $_POST['sex']=1; } $data=[ 'name'=> $_POST['name'], 'sex'=> $_POST['sex'], 'age' => $_POST['age'], 'email'=> $_POST['email'], 'creat_time' => time() ]; $where=['id'=>$_POST['id']]; $table='user'; $stmt=$db->update($table,$data,$where); if($stmt->rowCount()){ alert("修改成功","http://www.a.com/medoo/"); }else{ exit('修改失敗'.$stmt->errorInfo()); } } $smarty->assign('row',$row); $smarty->display('edit.html'
edit.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="tmp/css/style.css"> </head> <body> <div class="edit"> <div class="position">當(dāng)前位置: <a href="/medoo/">主頁</a>>編輯</div> <h1>編輯</h1> <table class="table"> <form action="" method="post"> <input type="hidden" name="id" value="{$row.id}"> <tr> <td>姓名</td> <td><input type="text" name="name" value="{$row.name}"></td> </tr> <tr> <td>性別</td> <td><input type="text" name="sex" value="{if $row.sex eq 0}男{else}女{/if}"></td> </tr> <tr> <td>年齡</td> <td><input type="text" name="age" value="{$row.age}"></td> </tr> <tr> <td>郵箱</td> <td><input type="text" name="email" value="{$row.email}"></td> </tr> <tr> <td colspan="2" style="background-color:transparent!important;"> <input type="submit" class="btn-success btn-lager" value="提交"> </td> </tr> </form> </table> </div> </body> </html>
del.php:
<?php require __DIR__.'/config/config.php'; $stmt=$db->delete('user',['id'=>$_GET['id']]); if($stmt->rowCount()){ alert('刪除成功','http://www.a.com/medoo/'); }else{ echo'刪除失敗'.$stmt->errorInfo(); }
config.php配置:
<?php //配置Smarty模版引擎 //如果是通過composer下載的配置如下: require __DIR__.'/database.php'; //實(shí)例化Smarty $smarty=new Smarty(); //配置四個目錄:必選 //配置模板目錄 $smarty->setTemplateDir(__DIR__.'/../tmp'); //編譯文件所在目錄 $smarty->setCompileDir(__DIR__.'/../tmp_c'); //緩存目錄 $smarty->setCacheDir(__DIR__.'/../cache'); //配置目錄 $smarty->setConfigDir(__DIR__.'/../config'); //可選配置 $smarty->setLeftDelimiter('{'); $smarty->setRightDelimiter('}'); //窗口提示信息 function alert($message,$url){ exit('<script>alert("'.$message.'");window.location.href="'.$url.'"</script>'); }
database.php:
<?php require __DIR__.'/../vendor/autoload.php'; use Medoo\Medoo as DB; //配置Medoo數(shù)據(jù)庫信息 $config=[ 'database_type' => 'mysql', 'database_name' => 'edu', 'server' => 'localhost', 'username' => 'root', 'password' => 'root', ]; $db=new DB($config);
批改老師:韋小寶批改時(shí)間:2019-02-26 10:02:19
老師總結(jié):不錯不錯 寫的很棒 有想法也知道怎么去練習(xí) 加油吧!