<?php
//DBへの接続
include('/xampp/data/conn.php');
//POSTの受信
$fnc = filter_input(INPUT_POST, 'fnc');
$set_id = filter_input(INPUT_POST, 'set_id');
$set_sectionCode = filter_input(INPUT_POST, 'set_sectionCode');
$set_staffCode = filter_input(INPUT_POST, 'set_staffCode');
$set_class = filter_input(INPUT_POST, 'set_class');
$set_workDate = filter_input(INPUT_POST, 'set_workDate');
$set_customerCode = filter_input(INPUT_POST, 'set_customerCode');
$set_interviewer = filter_input(INPUT_POST, 'set_interviewer');
$set_comment = filter_input(INPUT_POST, 'set_comment');
$select_id = filter_input(INPUT_POST, 'select_id');
$select_sectionCode = filter_input(INPUT_POST, 'select_sectionCode');
$select_staffCode = filter_input(INPUT_POST, 'select_staffCode');
$select_customerName = filter_input(INPUT_POST, 'select_customerName');
$select_customerName = mb_convert_kana($select_customerName,'A');
//変数リセット
$mess = null;
$OPT_DATA = null;
$sectionCode = null;
$sectionName = null;
$staffCode = null;
$staffName = null;
$opt_staff = null;
$class_display = null;
$opt_section = null;
$workDate = null;
$opt_date = null;
$set_class_display = null;
//変数表示変換
if($set_class == 1){
$set_class_display = '得意先訪問';
}
if($set_class == 2){
$set_class_display = '社内作業';
}
//データチェック
if($fnc == 1 or $fnc == 2){
if(empty($set_sectionCode)){
$no_input = 1;
$mess = '部署';
}
if(empty($set_staffCode)){
if(!empty($mess)){
$mess = $mess.'、担当者';
}else{
$mess = '担当者';
}
}
if(empty($set_class)){
if(!empty($mess)){
$mess = $mess.'、分類';
}else{
$mess = '分類';
}
}
if(empty($set_comment)){
if(!empty($mess)){
$mess = $mess.'、内容';
}else{
$mess = '内容';
}
}
if(!empty($mess)){
$mess = ''.$mess.'が未登録です。';
}
//登録更新SQL
if(empty($mess)){
$workMonth = date("Ym",strtotime($workDate)); //日付をYYYYMM形式に変換
if(empty($set_interviewer)){
$set_interviewer_inp = 'null';
}else{
$set_interviewer_inp = "'$set_interviewer'";
}
if(empty($set_customerCode)){
$set_customerCode = 'null';
}
$workMonth = date("Ym",strtotime($set_workDate));
if($fnc == 1){
if(empty($set_id)){
$sql = "insert into dailyreport
(
sectionCode
,staffCode
,customerCode
,class
,workDate
,workMonth
,interviewer
,comment
) value (
$set_sectionCode
,$set_staffCode
,$set_customerCode
,$set_class
,'$set_workDate'
,$workMonth
,$set_interviewer_inp
,'$set_comment'
);";
mysqli_query($conn,$sql) or die ("err_ins $sql");
$mess = '登録されました。';
$sql = "select id from dailyreport where staffCode = $set_staffCode order by id desc;";
$res = mysqli_query($conn,$sql) or die("error $sql");
$row = mysqli_fetch_array($res);
$set_id = $row["id"];
}else{
$sql = "update dailyreport set
sectionCode = $set_sectionCode
,staffCode = $set_staffCode
,customerCode = $set_customerCode
,class = $set_class
,workDate = '$set_workDate'
,workMonth = $workMonth
,interviewer = $set_interviewer_inp
,comment = '$set_comment'
where id = $set_id
;";
mysqli_query($conn,$sql) or die ("err_up $sql");
$mess = '更新されました。';
}
}
}
}
//削除SQL
if($fnc == 9){
$sql = "delete from dailyreport where id =$select_id;";
mysqli_query($conn,$sql) or die ("err_del $sql");
$mess = 'ID'.$select_id.'が削除されました。';
}
//修正データ抽出(登録直後および一覧からの修正を$set_idの有無で判定し抽出)
if(strlen($set_id)){
$sql = "select b.sectionCode,c.staffCode,d.customerCode,a.workDate,a.class,a.comment,a.interviewer,d.customerName
from dailyreport as a
left join Msection as b on a.sectionCode = b.sectionCode
left join Mstaff as c on a.staffCode = c.staffCode
left join Mcustomer as d on a.customerCode = d.customerCode
where a.id = $set_id
;";
$res = mysqli_query($conn,$sql) or die("er select $sql");
$row = mysqli_fetch_array($res);
$set_sectionCode = $row["sectionCode"];
$set_staffCode = $row["staffCode"];
$set_workDate = $row["workDate"];
$set_class = $row["class"];
$set_interviewer = $row["interviewer"];
$set_customerCode = $row["customerCode"];
$select_customerName = $row["customerName"];
$set_comment = $row["comment"];
$btn = '更新'; //最下部の「更新」ボタン表示
}else{
$btn = '登録'; //最下部の「登録」ボタン表示
}
//変数表示変換
if($set_class == 1){
$set_class_display = '得意先訪問';
}
if($set_class == 2){
$set_class_display = '社内作業';
}
//部署option
$sql = "select sectionCode,sectionName from Msection;";
$res = mysqli_query($conn,$sql) or die("error$sql");
while($row = mysqli_fetch_array($res)){
$sectionCode_opt = $row["sectionCode"];
$sectionName_opt = $row["sectionName"];
$opt_section .= '<option value="'.$sectionCode_opt.'">'.$sectionName_opt;
}
//選択された部署名の表示
if(strlen($set_sectionCode)){
$sql = "select sectionCode,sectionName from Msection where sectionCode = $set_sectionCode;";
$res = mysqli_query($conn,$sql) or die("error $sql");
$row = mysqli_fetch_array($res);
$sectionName = $row["sectionName"];
}
//担当option
if(strlen($set_sectionCode)){
$sql = "select staffCode,staffName from Mstaff where sectionCode = $set_sectionCode;";
$res = mysqli_query($conn,$sql) or die("error $sql");
while($row = mysqli_fetch_array($res)){
$staffCode_opt = $row["staffCode"];
$staffName_opt = $row["staffName"];
$opt_staff .= '<option value="'.$staffCode_opt.'">'.$staffName_opt;
}
}
//選択された担当者名の表示
if(strlen($set_staffCode)){
$sql = "select staffCode,staffName from Mstaff where staffCode = $set_staffCode;";
$res = mysqli_query($conn,$sql) or die("error $sql");
$row = mysqli_fetch_array($res);
$staffName = $row["staffName"];
}
//実施日option
$day0 = date("Y-m-d");
$cnt = 10; //10日前まで表示
for( $i = 0 ; $i <= $cnt ; $i++ ){
$day = date("Y-m-d",strtotime("-$i day",strtotime($day0)));
$opt_date .= '<option value="'.$day.'">'.$day;
}
//$_POSTデータの保持
$OPT_DATA .= '<input type="hidden" name="select_sectionCode" value="'.$select_sectionCode.'">';
$OPT_DATA .= '<input type="hidden" name="select_staffCode" value="'.$select_staffCode.'">';
$OPT_DATA .= '<input type="hidden" name="set_sectionCode" value="'.$set_sectionCode.'">';
$OPT_DATA .= '<input type="hidden" name="set_staffCode" value="'.$set_staffCode.'">';
$OPT_DATA .= '<input type="hidden" name="set_customerCode" value="'.$set_customerCode.'">';
$OPT_DATA .= '<input type="hidden" name="set_interviewer" value="'.$set_interviewer.'">';
$OPT_DATA .= '<input type="hidden" name="set_class" value="'.$set_class.'">';
$OPT_DATA .= '<input type="hidden" name="set_workDate" value="'.$set_workDate.'">';
$OPT_DATA .= '<input type="hidden" name="set_id" value="'.$set_id.'">';
$OPT_DATA .= '<input type="hidden" name="select_customerName" value="'.$select_customerName.'">';
//HTML本文
echo <<<EOT
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="/style.css" type="text/css">
<title>日報登録</title>
</head>
<body>
<table style="font-size:12px;">
<tr>
<td width="150">日報登録</td>
<td width="380">{$mess}</td>
<form method="POST" action="{$_SERVER['PHP_SELF']}">
<td>
<input type="submit" style="width:80px;" value="新規登録">
</td>
</form>
<form method="POST" action="dailyreport_list.php">
<td>
<input type="submit" style="width:80px;" value="一覧へ戻る">
</td>
</form>
</tr>
</table>
<table bgcolor="#a9a9a9" cellspacing="1px" style="font-size:12px;" >
<tr bgcolor="#D3D3D3" style="height:24px;" align="center">
<td width="70">部署名</td>
<form method="POST" action="{$_SERVER['PHP_SELF']}">
{$OPT_DATA}
<td>
<select name="set_sectionCode" onchange="submit(this.form)" style="width:100px;">
<option value="{$set_sectionCode}" selected>{$sectionName}
{$opt_section}
</select>
</td>
</form>
<td width="70">担当者</td>
<form method="POST" action="{$_SERVER['PHP_SELF']}">
{$OPT_DATA}
<td>
<select name="set_staffCode" onchange="submit(this.form)" style="width:100px;">
<option value="{$set_staffCode}" selected>{$staffName}
{$opt_staff}
</select>
</td>
</form>
<form method="POST" action="{$_SERVER['PHP_SELF']}">
{$OPT_DATA}
<td width="70">作業分類</td>
<td>
<select name="set_class" onchange="submit(this.form)" style="width:100px;">
<option value="{$set_class}" selected>{$set_class_display}
<option value="1">得意先訪問
<option value="2">社内作業
</select>
</td>
</form>
<form method="POST" action="{$_SERVER['PHP_SELF']}">
{$OPT_DATA}
<td width="70">実施日</td>
<td>
<select name="set_workDate" onchange="submit(this.form)" style="width:100px;">
<option value="{$set_workDate}" selected>{$set_workDate}
{$opt_date}
</select>
</td>
</form>
</tr>
</table>
EOT;
//顧客訪問の場合得意先検索画面を表示をする
if($set_class == 1 and strlen($set_workDate)){
echo <<<EOT
<table bgcolor="#a9a9a9" cellspacing="1px" style="table-layout:fixed;font-size:12px;margin-top:-1px;" >
<tr bgcolor="#D3D3D3" style="height:24px;" align="center">
<form method="POST" action="{$_SERVER['PHP_SELF']}">
{$OPT_DATA}
<td width="70">得意先</td>
<td width="286"><input type="text" name="select_customerName" style="width:278px;" value="{$select_customerName}"></td>
<td width="60"><input type="submit" value="検索" style="width:100%;"></td>
</form>
EOT;
if(strlen($set_customerCode)){
//得意先が選択された場合面談者を表示
echo <<<EOT
<form method="POST" action="{$_SERVER['PHP_SELF']}">
{$OPT_DATA}
<input type="hidden" name="fnc" value="1">
<input type="hidden" name="set_id" value="{$set_id}">
<td width="70">面談者</td>
<td width="203"><input type="text" name="set_interviewer" style="width:192px;" value="{$set_interviewer}"></td>
EOT;
}
echo <<<EOT
</tr>
</table>
EOT;
//得意先検索された場合候補先を表示(社内作業時は表示させない)
if($set_class == 1 and strlen($select_customerName) and empty($set_customerCode)){
echo <<<EOT
<table bgcolor="#a9a9a9" cellspacing="1px" style="font-size:12px;margin-top:-1px;" >
<tr bgcolor="#D3D3D3" style="height:24px;" align="center">
<td width="359">得意先名</td><td width="60">選択</td>
</tr>
EOT;
$sql = "select customerCode,customerName from Mcustomer where customerName like '%$select_customerName%';";
$res = mysqli_query($conn,$sql) or die("error $sql");
while($row = mysqli_fetch_array($res)){
$customerCode = $row["customerCode"];
$customerName = $row["customerName"];
echo <<<EOT
<tr bgcolor="white" style="height:24px;" align="center">
<form method="POST" action="{$_SERVER['PHP_SELF']}">
{$OPT_DATA}
<input type="hidden" name="select_customerName" value="{$customerName}">
<input type="hidden" name="set_customerCode" value="{$customerCode}">
<td width="253" bgcolor="white" align="left">{$customerName}</td>
<td width="60"><input type="submit" value="選択" style="width:100%;"></td>
</form>
EOT;
}
echo <<<EOT
</tr>
</table>
EOT;
}
}
//作業内容登録表示
if($set_class == 2 or ($set_class == 1 and strlen($set_customerCode))){
echo <<<EOT
<table bgcolor="#a9a9a9" cellspacing="1px" style="font-size:12px;margin-top:-1px;" >
<tr bgcolor="#D3D3D3">
EOT;
if($set_class <> 1 ){ //社内作業用のPOSTデータ
echo <<<EOT
<form method="POST" action="{$_SERVER['PHP_SELF']}">
{$OPT_DATA}
<input type="hidden" name="fnc" value="1">
<input type="hidden" name="set_id" value="{$set_id}">
EOT;
}
echo <<<EOT
<td width="70" align="center">内容</td>
<td width="628">
<textarea type="text" name="set_comment" rows="20" cols="86">{$set_comment}</textarea>
</td>
</tr>
</table>
<table>
<tr>
<td width="635"></td>
<td width="60">
<input type="submit" style="width:60px;" value="{$btn}">
</td>
</form>
</tr>
</table>
EOT;
}
echo <<<EOT
</body>
</html>
EOT;
?>
処理を繰り返すときに使用します。このプログラムの場合、$iの初期値が0で繰り返し処理毎$i++で$iに1が加算されます。この加算が$i <= 10 ($cnt = 10で169行目に定義)
まで続けるという処理になります。