我学网站编程属于半途出家的类型,本是搞运维的,进了现在的公司后意识到学习一门编程语言的重要性,便从平时的工作时间里抽出部分来做学习和练习。
公司做技术的都是属于编程出身的,从网站设计到手机程式设计,好像大多数的人都在走这样的一条道路。
公司的部分业务是做手机游戏开发的,测试设备也越来越多,管理权归我们运维两个人所有,一直以来都是用个小笔记本做外借的登记,每天都有不同的测试人员过来借设备,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里面的功能只是很少:
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写的,代码如下,做部分的注释说明
下面截个图展示一下界面的设计:
由图可以看到当设备已被借出的时候,外借按钮没法操作,显示外借时间,背景为红色,此时只能点归还的按钮,当设备已归还,显示归还时间,显示绿色背景,前面的外借按钮可以操作,但先要从选择菜单选择一个外借的人员才能操作。这部分的功能主要靠javascript来控制。前面的简单的js代码里有说明。
写下这个小小的程序希望能够在工作上能够帮到自己减少重复的操作,并且让自己保持学习状态和培养写程序的习惯。
love,code,this is my life.