标签:php mysql javascript 设备外借管理系统
我学网站编程属于半途出家的类型,本是搞运维的,进了现在的公司后意识到学习一门编程语言的重要性,便从平时的工作时间里抽出部分来做学习和练习。
公司做技术的都是属于编程出身的,从网站设计到手机程式设计,好像大多数的人都在走这样的一条道路。
公司的部分业务是做手机游戏开发的,测试设备也越来越多,管理权归我们运维两个人所有,一直以来都是用个小笔记本做外借的登记,每天都有不同的测试人员过来借设备,ipad,iphone,android机,借了又还,还了另外一个人过来借,不用多久,那笔记本已经累积到厚厚的一叠了。我想要不写个小工具来登记代替人手的繁琐操作,一来可以当做技术练兵,二来可以显示显示IT部的专业技能,呵呵。
二话不说,马上动手。
需求:
列出所有测试设备的外借状态,我们给每台设备分配一个唯一的号码做记录。状态包括是否借出了,借出时间,借出人员,是否归还了,归还时间。
如果是外借了,显示外借人是谁,借出时间,此时外借按钮无法操作,归还按钮可操作。
如果已归还, 显示归还时间,此时归还按钮无法操作,外借按钮可操作。
列出公司经常要借设备的人的名字,选择菜单操作。
提交外借操作的时候,如果外借人名字为空,则不允许操作,用js控制。
关于数据库,用mysql,简单的一张表
CREATE TABLE `device_map` ( `device_id` varchar(10) NOT NULL, `alias` varchar(30) DEFAULT NULL, `staff` varchar(30) DEFAULT NULL, `borrowed` tinyint(1) DEFAULT ‘0‘ COMMENT ‘0 = in stock / 1 = borrowed‘, `borrow_time` timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00‘, `return_time` timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00‘, PRIMARY KEY (`device_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
关于数据库的操作,我沿用之前写的一个简单php class,因为都是涉及更新的操作,所以比较简单,这里写的class只是为了记录一下,其实这个例子里用到的class里面的功能只是很少:
<?php class Database { protected $conn, $sql, $resultSet; protected $errMsg; protected $db; private $trace = false, $logFile, $showError = false; function __construct($host, $userid, $password, $db, $port = "3306", $encoding="utf8") { $this->Open($host, $userid, $password, $db, $port, $encoding); } function Open($host, $userid, $password, $db, $port = "3306", $encoding="utf8") { $host = $host . ":" . $port; $this->conn = @mysql_connect($host, $userid, $password); mysql_query("SET NAMES ‘{$encoding}‘"); if (!$this->conn) { $this->errMsg = mysql_error(); if ($this->showError) echo $this->errMsg."\n"; if ($this->logFile) error_log ( date("Y-m-d H:i:s")."\t".$this->errMsg."\n", 3, $this->logFile); return false; } if (!@mysql_select_db($db)) { $this->errMsg = mysql_error(); if ($this->showError) echo $this->errMsg."\n"; if ($this->logFile) error_log ( date("Y-m-d H:i:s")."\t".$this->errMsg."\n", 3, $this->logFile); return false; } $this->db = $db; return true; } function Close() { if ($this->conn) { if (!@mysql_close($this->conn)) { $this->errMsg = mysql_error(); if ($this->showError) echo $this->errMsg."\n"; if ($this->logFile) error_log ( date("Y-m-d H:i:s")."\t".$this->errMsg."\n", 3, $this->logFile); return false; } else { $this->conn = ""; $this->sql = ""; $this->resultSet = ""; } } return true; } function Query($sql) { if ($this->trace) echo $sql . "\n"; $this->resultSet = @mysql_query($sql); if (!$this->resultSet) { $this->errMsg = mysql_error(); if ($this->showError) echo $this->errMsg."\n"; if ($this->logFile) { error_log ( date("Y-m-d H:i:s")."\t".$sql."\n", 3, $this->logFile); error_log ( date("Y-m-d H:i:s")."\t".$this->errMsg."\n", 3, $this->logFile); } return false; } return $this->resultSet; } function Fetch() { if ($this->resultSet) { $oldmsg = mysql_error(); $row = @mysql_fetch_assoc($this->resultSet); if ($oldmsg != mysql_error()) { if ($this->showError) echo $this->errMsg."\n"; $this->errMsg = mysql_error(); if ($this->logFile) error_log ( date("Y-m-d H:i:s")."\t".$this->errMsg."\n", 3, $this->logFile); return false; } return $row; } } #function checkUser($username,$email) function checkUser($username) { $sql1="select * from users where `username`=‘".$username."‘"; #$sql2="select * from users where `email`=$email"; #$this->Query($sql1); return $this->FetchNumRows(); return $username; } function FetchResult() { if ($this->resultSet) { $oldmsg = mysql_error(); $row = @mysql_result($this->resultSet, 0); if ($oldmsg != mysql_error()) { $this->errMsg = mysql_error(); if ($this->showError) echo $this->errMsg."\n"; if ($this->logFile) error_log ( date("Y-m-d H:i:s")."\t".$this->errMsg."\n", 3, $this->logFile); return false; } return $row; } } function FetchNumRows() { if ($this->resultSet) { $oldmsg = mysql_error(); $row = @mysql_num_rows($this->resultSet); if ($oldmsg != mysql_error()) { $this->errMsg = mysql_error(); if ($this->showError) echo $this->errMsg."\n"; if ($this->logFile) error_log ( date("Y-m-d H:i:s")."\t".$this->errMsg."\n", 3, $this->logFile); return false; } return $row; } } function FetchAll() { $rows = array(); while ($row = $this->Fetch()) { $rows[] = $row; } return $rows; } function FetchResultDetailAll() { if ($this->resultSet) { $totalrows = @mysql_num_rows($this->resultSet); $oldmsg = mysql_error(); for ($i=0; $i<$totalrows; $i++){ $rows[$i] = @mysql_fetch_assoc($this->resultSet); if ($oldmsg != mysql_error()) { $this->errMsg = mysql_error(); if ($this->showError) echo $this->errMsg."\n"; if ($this->logFile) error_log ( date("Y-m-d H:i:s")."\t".$this->errMsg."\n", 3, $this->logFile); return false; } } return $rows; } } function FetchResultAll() { if ($this->resultSet) { $totalrows = @mysql_num_rows($this->resultSet); $oldmsg = mysql_error(); for ($i=0; $i<$totalrows; $i++){ $rows[] = @mysql_result($this->resultSet, $i); if ($oldmsg != mysql_error()) { $this->errMsg = mysql_error(); if ($this->showError) echo $this->errMsg."\n"; if ($this->logFile) error_log ( date("Y-m-d H:i:s")."\t".$this->errMsg."\n", 3, $this->logFile); return false; } } return $rows; } } function GetId() { $sql = "SELECT LAST_INSERT_ID() AS ID"; $result = $this->Query($sql); if ($result) { $rs = $this->Fetch(); return $rs["ID"]; } } function replaceRecord($table, array $values, $type = "REPLACE") { foreach ($values as $key=>$value){ $ar_str[] = "`". $key . "`" . " = ‘". $value ."‘"; } $str = implode(", ", $ar_str); $sql = "$type INTO $table SET $str"; #echo $sql."\n"; return $this->Query($sql); #return $str; #return $sql; } function deleteRecord($table, array $values) { foreach ($values as $key=>$value){ $ar_str[] = "`". $key . "`" . " = ‘". $value ."‘"; } $str = implode(" and ", $ar_str); $sql = "DELETE FROM $table where $str"; return $this->Query($sql); #return $str; #return $sql; } function Insert($table, array $values, $type = "INSERT") { foreach ($values as $key=>$value){ $ar_str[] = "`". $key . "`" . " = ‘". $this->AddQuote($value) ."‘"; } $str = implode(", ", $ar_str); $sql = "$type INTO $table SET $str"; return $this->Query($sql); } function Update($table, array $values, $wherestr) { foreach ($values as $key=>$value){ $ar_str[] = "`". $key . "`" . " = ‘". $this->AddQuote($value) ."‘"; } $str = implode(", ", $ar_str); $sql = "UPDATE $table SET $str WHERE $wherestr"; return $this->Query($sql); } function Replace($table, array $values) { return $this->Insert($table, $values, "REPLACE"); } function GetList($table, $wherestr = "", array $option = array()) { $fieldstr = isset($option["fields"]) ? $option["fields"] : "*"; $sql = "SELECT $fieldstr FROM " . $table; if ($wherestr != "") $sql .= " WHERE $wherestr "; if (isset($option["orderby"]) && $option["orderby"]) { $sql .= " ORDER BY ". $option["orderby"]; } if (isset($option["limit"]) && isset($option["offset"]) && $option["limit"] && $option["offset"]) { $sql .= " LIMIT {$option["offset"]},{$option["limit"]}"; } $rs = $this->Query($sql); while ($row = $this->Fetch()) { if (!(isset($option["key"]) && $option["key"])) { $data[] = $row; } else { if(!(isset($option["key_array"]) && $option["key_array"])) { $data[$row[$option["key"]]] = $row; } else { $data[$row[$option["key"]]][] = $row; } } } return $data; } protected function AddQuote($str) { //$str = mysql_real_escape_string($str); //$str = str_replace(‘\\‘, ‘\\\\‘, $str); //$str = str_replace(‘\\"‘, ‘"‘, $str); //$str = ereg_replace ("‘", "‘‘", $str); return $str; } function GenOption($default_sort = ‘‘, $default_order = ‘‘) { $page = $_POST["page"]; $rows = $_POST["rows"]; $sort = $_POST["sort"] ? $_POST["sort"] : $default_sort; $order = $_POST["order"] ? $_POST["order"] : $default_order; $offset = ($page - 1) * $limit; if ($page) { $option["offset"] = $offset; $option["limit"] = $limit; $option["limit_sql"] = " LIMIT $offseta,$limit"; } if ($sort) { if (empty($order)) { $order = "asc"; } $option["orderby"] = $sort." ".$order; $option["orderby_sql"] = " ORDER BY $sort $order"; } return $option; } function HasOperator($str) { $str = trim($str); if ( ! preg_match("/(\s|<|>|!|=|is null|is not null| like )/i", $str)) { return FALSE; } return TRUE; } public function GenWhere() { $where = array(); if ($search = $_POST["search"]) { foreach ($search as $key=>$value) { $ar_key = explode(":", $key); if (is_string($value)) { if ($value !== "") { $ar_where = array(); foreach ($ar_key as $k) { if ($this->HasOperator($value)) { $ar_where[] = is_numeric($k) ? $value : $k." ".$value; } else { $ar_where[] = $k." = ‘".$value."‘"; } } $where_key = "(".implode(" || ", $ar_where).")"; $where[] = $where_key; } } elseif (is_array($value)) { foreach ($value as $operator=>$search_value) { if ($search_value !== "") { $ar_where = array(); foreach ($ar_key as $k) { switch ($operator) { case "bm": $ar_where[] = $k." LIKE ‘%".$search_value."%‘"; break; case "rm": $ar_where[] = $k." LIKE ‘".$search_value."%‘"; break; case "lm": $ar_where[] = $k." LIKE ‘%".$search_value."‘"; break; case "gt": $ar_where[] = $k." > ‘". $search_value."‘"; break; case "ge": $ar_where[] = $k." >= ‘". $search_value."‘"; break; case "lt": $ar_where[] = $k." < ‘".$search_value."‘"; break; case "le": $ar_where[] = $k." <= ‘".$search_value."‘"; break; case "gt-t": $ar_where[] = $k." > ‘". $search_value." 00:00:00‘"; break; case "ge-t": $ar_where[] = $k." >= ‘". $search_value." 00:00:00‘"; break; case "lt-t": $ar_where[] = $k." < ‘".$search_value." 23:59:59‘"; break; case "le-t": $ar_where[] = $k." <= ‘".$search_value." 23:59:59‘"; break; } } $where_key = "(".implode(" || ", $ar_where).")"; $where[] = $where_key; } } } } } return $where ? " WHERE " . implode(" AND ", $where) : ""; } function SetTrace($value) { $this->trace = $value; } function SetShowError($value) { $this->showError = $value; } function SetLogFile($value) { $this->logFile= $value; } function GetErrMsg() { return $this->errMsg; } } ?>
还有个简单的配置文件:
<?php $DB["host"] = "127.0.0.1"; $DB["user"] = "###"; $DB["pass"] = "###"; $DB["name"] = "device_management"; $DB["encoding"] = "utf8"; //数据库的连接参数 $staff=array( ‘Funny Ho‘ , ‘Tom Chan‘ , ‘Bruce Tam‘ , ); //列出员工的名字 ?>
程序是php写的,代码如下,做部分的注释说明
<html> <head> <link rel="stylesheet" type="text/css" href="css/style.css"> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> //我这里用jquery的库,主要是喜欢它操作DOM的时候省去不少代码,方便高效。 <script type="text/javascript"> $(document).ready(function(){ $(".borrow_disable").attr(‘disabled‘,‘disable‘); $(".return_disable").attr(‘disabled‘,‘disable‘); $(".select_staff").each(function(){ var staff=$(this).val(); //alert(staff); $(this).closest(‘td‘).next().find(‘input[name="staff"]‘).val(staff); }) //第一个函数控制已外借状态和以归还状态的设备各自的按钮将无法操作 $(".select_staff").each(function(){ $(this).change(function(){ var staff=$(this).val(); $(this).closest(‘td‘).next().find(‘input[name="staff"]‘).val(staff); }); //alert(staff); }) //第二个函数控制选择表单的选中项同步到提交外借的表单里,表单里的输入项是隐藏的。 $(".borrow_form").each(function(){ $(this).submit(function(event){ var staff=$(this).find(‘input[name="staff"]‘).val(); if (staff=="N/A") { event.preventDefault(); alert(‘please select a staff!‘); } }) }) //第三个函数控制如果提交外借按钮点击时外借人员名字为"N/A"是将无法操作,就是规定外借的时候必须选择外借的人是谁。 }); </script> </head> <body> <div class=content> <table> <tr><th>Device</th><th>Staff</th><th>Borrow</th><th>Borrow Time</th><th>Return</th><th>Return Time</th></tr> <?php require_once(‘device_management/database.php‘); require_once(‘device_management/config.inc‘); //加载数据库的操作文件 $staff_list=$staff; $DB = new Database ($DB["host"], $DB["user"], $DB["pass"], $DB["name"]); if ($_REQUEST[‘borrowed‘]==1) { $data[‘borrowed‘]=1; if ((!empty($_REQUEST[‘device_id‘])) and (!empty($_REQUEST[‘staff‘])) and ($_REQUEST[‘staff‘]!=="N/A")) { $data[‘device_id‘]=$_REQUEST[‘device_id‘]; $data[‘staff‘]=$_REQUEST[‘staff‘]; $data[‘borrow_time‘]=date(‘Y-m-d H:i:s‘,time()); $DB->replaceRecord(‘device_map‘,$data); } } if ($_REQUEST[‘borrowed‘]==0) { $data[‘borrowed‘]=0; if (!empty($_REQUEST[‘device_id‘])) { $data[‘device_id‘]=$_REQUEST[‘device_id‘]; $data[‘staff‘]=$_REQUEST[‘staff‘]; $data[‘return_time‘]=date(‘Y-m-d H:i:s‘,time()); $DB->replaceRecord(‘device_map‘,$data); } } //var_dump($data); //上面的代码检查是否有提交表单的操作,如果有,再判断是外借还是归还的操作。 $sql="select `device_id`,`staff`,`borrowed`,`borrow_time`,`return_time` from `device_map`"; $rs=$DB->Query($sql); while ($row=mysql_fetch_assoc($rs) ) { $machine=$row[‘device_id‘]; if ($row[‘borrow_time‘]!==‘0000-00-00 00:00:00‘) { $borrow_time=$row[‘borrow_time‘]; $borrow_time_class="borrow_time"; } else { $borrow_time="N/A"; $borrow_time_class="no_time"; } if ($row[‘return_time‘]!==‘0000-00-00 00:00:00‘) { $return_time=$row[‘return_time‘]; $return_time_class="return_time"; } else { $return_time="N/A"; $return_time_class="no_time"; } if (is_null($row[‘staff‘] )) { $staff="N/A"; } else $staff=$row[‘staff‘]; if ($staff=="N/A") { $staff="N/A"; } if ($row[‘borrowed‘]==0) { $borrow_class=‘borrow_enable‘; $borrowed=0; $return_class=‘return_disable‘; } else { $borrow_class=‘borrow_disable‘; $borrowed=1; $return_class=‘return_enable‘; } $options=""; //echo $staff;exit; if ($staff=="N/A") { $options="<option selected value=\"N/A\">N/A</option>" ; //echo $options."\n"; } else { $options="<option value=\"N/A\">N/A</option>" ; $selected_class="unselected"; } foreach ($staff_list as $stf) { if ($staff == $stf) { $options.="<option style=\"background-color:green;color:#FFF\" selected value=\"".$staff."\">$staff</option>" ; $selected_class="selected"; } else { $options.="<option value=\"$stf\">$stf</option>"; //$selected_class="unselected"; } } //echo $options; $borrow_form="<form class=borrow_form action=index.php method=get><input class=hidden value=$machine name=device_id></input><input class=\"hidden\" value=\"\" name=staff><input class=hidden value=1 name=borrowed></input><input class=$borrow_class type=submit value=borrow></input></form>"; $return_form="<form class=return_form action=index.php methond=get><input class=hidden value=$machine name=device_id></input><input class=hidden value=0 name=borrowed></input><input class=$return_class type=submit value=return></input></form>"; echo "<tr><td class=machine>".$machine."</td><td class=staff><select class=\"select_staff\" name=staff>$options</select></td><td>$borrow_form</td><td class=$borrow_time_class>$borrow_time</td><td>".$return_form."</td><td class=$return_time_class>$return_time</td></tr>"; // } } //上面的循环是根据设备的状态输出不同的css样式以方便管理者浏览,比如外借状态下显示外借的时间为红色背景,外借按钮无法操作,归还按钮可操作。 //归还状态下显示的时间为绿色背景,并且归还按钮不可操作,外借按钮可操作。 $DB->Close(); ?> </table> </div> </body> </html>
下面截个图展示一下界面的设计:
由图可以看到当设备已被借出的时候,外借按钮没法操作,显示外借时间,背景为红色,此时只能点归还的按钮,当设备已归还,显示归还时间,显示绿色背景,前面的外借按钮可以操作,但先要从选择菜单选择一个外借的人员才能操作。这部分的功能主要靠javascript来控制。前面的简单的js代码里有说明。
写下这个小小的程序希望能够在工作上能够帮到自己减少重复的操作,并且让自己保持学习状态和培养写程序的习惯。
love,code,this is my life.
本文出自 “安家圈子交流学习” 博客,请务必保留此出处http://brucetam.blog.51cto.com/1863614/1546979
标签:php mysql javascript 设备外借管理系统
原文地址:http://brucetam.blog.51cto.com/1863614/1546979