<?php
header("Content-Type: application/json;charset=utf-8");
$staff = array
(
array("name" => "洪七", "number" => "101", "sex" => "男", "job" => "总经理"),
array("name" => "郭靖", "number" => "102", "sex" => "男", "job" => "开发工程师"),
array("name" => "黄蓉", "number" => "103", "sex" => "女", "job" => "产品经理")
);
if ($_SERVER['REQUEST_METHOD']==="GET") {
search(); # code...
}elseif ($_SERVER['REQUEST_METHOD']=="POST") {
//global $staff;
create($staff);
}
function search() {
if (!isset($_GET['number'])||empty($_GET["number"])) {
echo '{"success":false,"msg":"参数错误"}';
return;
}
global $staff;
$number = $_GET["number"];
$result = '{"success":false,"msg":"没有找到员工。"}';
foreach ($staff as $value) {
if($value["number"]==$number){
$result = '{"success":true,"msg":"找到员工:员工编号:' . $value["number"] .
',员工姓名:' . $value["name"] .
',员工性别:' . $value["sex"] .
',员工职位:' . $value["job"] . '"}';
break;
}
}
echo $result;
}
function create(&$staff) {
if (
!isset($_POST['name'])||empty($_POST['name'])
||!isset($_POST['number'])||empty($_POST['number'])
||!isset($_POST['sex'])||empty($_POST['sex'])
||!isset($_POST['job'])||empty($_POST['job'])
) {
echo '{"success":false,"msg":"参数有误"}';
return;# code...
}else{
$newAdds = array();
$newAdd = array();
$newAdd['name'] = $_POST['name'];
$newAdd['number'] = $_POST['number'];
$newAdd['sex'] = $_POST['sex'];
$newAdd['job'] = $_POST['job'];
$newAdds[] = $newAdd;
$staff = array_merge($staff, $newAdds);
$result ='{"success":true,"msg":"添加成功"}';
echo $result;
return $result;
}
}
//print_r($staff);