PHP 残数管理 師匠からの教え SQLぶっ叩け

ど〜もです!

GW最高ですね。

GWが1年間続いて、お金が月30万くらい入ってくれば。。。

最高ですね。

引き寄せよ法則ーーー>働け!!!GW!GW!GW!

!!--------- fuck -------------!!

っていう事で、

今日は、僕のプログラミングのお師匠さんのお言葉で、教えてもらったことを、PHPや、mysqlをご利用の方は参考にしてください。

 

とりあえず、ページ画面を貼ります。

f:id:toshi-n:20190430004356p:plain

次、週

 

f:id:toshi-n:20190430004455p:plain

 

師匠の有難き言葉からの結果から・・・

1:合計処理に関してはすぐ完了できました。

2;それでれ、(シングル、ダブル、ツイン、和室,etc)などの処理は、PHP側プログラミングして、データを処理しようとしてました。

ーー結果ーー

2:は、処理が難しく、SQLでデータを取り出し、そこで処理をしました。

師匠曰く、SQLのデータ処理の方が早い。

むしろ、こういったSQLプログラミングに対しては、言語側で処理をするよりも、SQL側で処理をした方が、処理速度は早いとこのことでしたので、師匠が言われたとりに、SQLで処理を行いました。

 

ーー処理ーー

・42とういう定数に対しての、処理です。

残り残数 つまり、

42の定数に対して、

その日の、plan_loom_type(カラム名) を引く、

で合計値は出ます。

------

ルームタイプの合計をsqlで処理しました。

sqlではなく、phpプログラミングで格闘したあとを残しました。

 

------------------------- Code ---------------------------

 

<?php

ini_set('display_errors', "On");

require_once("data/db_info.php");

// エスケープ処理
function h($s) {
return htmlspecialchars($s, ENT_QUOTES, "UTF-8");
}

/*------------------- 週間カレンダー処理 ------------------*/

/*--- 本日の取得 ---*/
function getToday($date = 'Y-m-d') {

$today = new DateTime();
return $today->format($date);
}

/*--- 本日かどうかをチェック ---*/
function isToday($year, $month, $day) {

$today = getToday('Y-n-j');

if($today == $year . "-" . $month . "-" . $day) {

return true;
}

return false;
}

/*--- 今週の日曜日の日付を返す ---*/
function getSunday() {

$today = new DateTime();
$w = $today->format('w'); // 曜日取得
$ymd = $today->format('Y-m-d'); // 今日の 年-月-日 取得

$next_prev = new DateTime();
$next_prev->modify("-{$w}day");

return $next_prev->format('Ymd');
}

/*--- 今週の月曜日を返す ---*/
function getMonday() {

$today = new DateTime();
$w = $today->format('w');
$ymd = $today->format('Y-m-d');

if($w == 0) {
$d = 6;
} else {
$d = $w - 1;
}

$next_prev = new DateTime($ymd);
$next_prev->modify("-{$d}day");

return $next_prev->format('Ymd');

}

/*--- N日(週)+か-する関数 --- */
function getNthDay($year, $month, $day, $n) {

$next_prev = new DateTime($year . '-' . $month . '-' . $day);
$next_prev->modify($n);

return $next_prev->format('Ymd');
}

/*--- 週間カレンダーの表示 --- */
if(isset($_GET['date'])) {
// 年月日取得
$year_month_day = $_GET['date'];

} else {

// 今週 月曜日取得
$year_month_day = getMonday();
}

// 年月日に変数で取得
$year = substr($year_month_day, 0, 4);
$month = substr($year_month_day,4, 2);
$day = substr($year_month_day, 6,2);

// SQL 用 date 作成
$sql_date = $year . "-" . $month . "-" . $day;

$month = sprintf("%01d", $month);
$day = sprintf("%01d", $day);

$next_week = getNthDay($year, $month, $day, '+1 week'); // 1週間 後
$prev_week = getNthDay($year, $month, $day, '-1 week'); // 1週間 前

// 週間の日付出力
$table = NULL;

for($i = 0;$i < 7;$i++) {
$ymd = getNthDay($year, $month, $day, '+' . $i . 'day');

$y = substr($ymd, 0,4);
$m = substr($ymd, 4,2);
$d = substr($ymd, 6,2);


$n = sprintf("%01d", $m);
$j = sprintf("%01d", $d);

// $t = $j . '日';

if(isToday($y,$m,$d)) {
$table .='<td class="today">' . $j . '</td>';

} else {
$table .="<td>". $j . "</td>";
}
}

$last_day = getNthDay($year, $month, $day, '+6 days');
 
/*--------- 変数 配列定義 -------------*/

$check_in = array();
$plan_loom_type = array();

$arr_index = 0;
/*---------------- DB 接続処理 ----------------*/

try {

$s = new PDO("mysql:host=$SERV;dbname=$DBNM;", $USER, $PASS);

$re = $s->query("select * from user_f01 where (check_in BETWEEN $year_month_day AND $last_day) OR (check_out BETWEEN $year_month_day AND $last_day)");

while($row = $re->fetch()) {

$row['check_in'] = str_replace("-","", $row['check_in']);

$check_in[$arr_index] = $row['check_in']; // チェックイン予定日
$plan_loom_type[$arr_index] = $row['plan_loom_type']; // 部屋タイプ

$arr_index++;
}

} catch (PDOException $e) {
// エラー処理
print "エラー内容です:" . $e->getMessage();
}

var_dump($plan_loom_type);

/*---------------- DB 接続処理 部屋の合計値取得 ----------------*/

$arr_check_in_sum = array();

$arr_plan_loom_type_sum = array();

$arr_index_2 = 0;
 
try {

$s = new PDO("mysql:host=$SERV;dbname=$DBNM;", $USER, $PASS);

$re = $s->query("select check_in,COUNT(plan_loom_type) from user_f01 where (check_in BETWEEN $year_month_day AND $last_day) or (check_out BETWEEN $year_month_day AND $last_day) GROUP BY check_in;)");

while($row = $re->fetch()) {

$row['check_in'] = str_replace("-","", $row['check_in']);

$arr_check_in_sum[$arr_index_2] = $row['check_in']; // チェックイン予定日
$arr_plan_loom_type_sum[$arr_index_2] = $row['COUNT(plan_loom_type)']; // 部屋タイプ

$arr_index_2++;
}

} catch (PDOException $e) {
// エラー処理
print "エラー内容です:" . $e->getMessage();
}


/*---------------- DB 接続処理 日付別 ルームタイプの合計を取得 ----------------*/

// チェックイン日
$arr_check_in_loom_sum_1 = array();
$arr_check_in_loom_sum_2 = array();
$arr_check_in_loom_sum_3 = array();
$arr_check_in_loom_sum_4 = array();

// シングル合計
$arr_plan_loom_type_single = array();
// ダブル合計
$arr_plan_loom_type_double = array();
// ツイン合計
$arr_plan_loom_type_twin = array();
// 和室合計
$arr_plan_loom_type_wa = array();

$arr_index_3 = 0;

try {

/*--------- シングル 合計 -----------*/
$s_1 = new PDO("mysql:host=$SERV;dbname=$DBNM;", $USER, $PASS);
$re_1 = $s_1->query("SELECT check_in,SUM(plan_loom_type = 1) FROM user_f01 WHERE (check_in BETWEEN $year_month_day AND $last_day) GROUP BY check_in");

while($row = $re_1->fetch()) {

$row['check_in'] = str_replace("-","", $row['check_in']);

$arr_check_in_loom_sum_1[$arr_index_3] = $row['check_in']; // チェックイン予定日
$arr_plan_loom_type_single[$arr_index_3] = $row['SUM(plan_loom_type = 1)']; // 部屋タイプ

$arr_index_3++;
}
/*--------- シングル 合計 終了-----------*/

/*--------- ダブル 合計 -----------*/
$s_2 = new PDO("mysql:host=$SERV;dbname=$DBNM;", $USER, $PASS);
$re_2 = $s_2->query("SELECT check_in,SUM(plan_loom_type = 2) FROM user_f01 WHERE (check_in BETWEEN $year_month_day AND $last_day) GROUP BY check_in");
 
while($row = $re_2->fetch()) {
 
$row['check_in'] = str_replace("-","", $row['check_in']);
 
$arr_check_in_loom_sum_2[$arr_index_3] = $row['check_in']; // チェックイン予定日
$arr_plan_loom_type_double[$arr_index_3] = $row['SUM(plan_loom_type = 2)']; // 部屋タイプ
 
$arr_index_3++;
}
/*--------- ダブル 合計 終了-----------*/

/*--------- ツイン 合計 -----------*/
$s_3 = new PDO("mysql:host=$SERV;dbname=$DBNM;", $USER, $PASS);
$re_3 = $s_3->query("SELECT check_in,SUM(plan_loom_type = 3) FROM user_f01 WHERE (check_in BETWEEN $year_month_day AND $last_day) GROUP BY check_in");
 
while($row = $re_3->fetch()) {
 
$row['check_in'] = str_replace("-","", $row['check_in']);
 
$arr_check_in_loom_sum_3[$arr_index_3] = $row['check_in']; // チェックイン予定日
$arr_plan_loom_type_twin[$arr_index_3] = $row['SUM(plan_loom_type = 3)']; // 部屋タイプ
 
$arr_index_3++;
}
/*--------- ツイン 合計 終了-----------*/

/*--------- 和室 合計 -----------*/
$s_4 = new PDO("mysql:host=$SERV;dbname=$DBNM;", $USER, $PASS);
$re_4 = $s_4->query("SELECT check_in,SUM(plan_loom_type = 4) FROM user_f01 WHERE (check_in BETWEEN $year_month_day AND $last_day) GROUP BY check_in");
 
while($row = $re_4->fetch()) {
 
$row['check_in'] = str_replace("-","", $row['check_in']);
 
$arr_check_in_loom_sum_4[$arr_index_3] = $row['check_in']; // チェックイン予定日
$arr_plan_loom_type_wa[$arr_index_3] = $row['SUM(plan_loom_type = 4)']; // 部屋タイプ
 
$arr_index_3++;
}
/*--------- 和室 合計 終了-----------*/

} catch (PDOException $e) {
// エラー処理
print "エラー内容です:" . $e->getMessage();
}

/*------ 日付 & プランタイプ => 1:シングル 2:ダブル 3:ツイン 4:和室 合計 -----*/
$arr_plan_single_result = array();
$arr_plan_double_result = array();
$arr_plan_twin_result = array();
$arr_plan_wa_result = array();

/*--- シングル ---*/
$arr_plan_single_result = array_combine($arr_check_in_loom_sum_1, $arr_plan_loom_type_single);

/*--- ダブル ---*/
$arr_plan_double_result = array_combine($arr_check_in_loom_sum_2, $arr_plan_loom_type_double);

/*--- ツイン ---*/
$arr_plan_twin_result = array_combine($arr_check_in_loom_sum_3, $arr_plan_loom_type_twin);

/*--- 和室 ---*/
$arr_plan_wa_result = array_combine($arr_check_in_loom_sum_4, $arr_plan_loom_type_wa);

 
/*------ 日付 & プランタイプ => 1:シングル 2:ダブル 3:ツイン 4:和室 合計 終了 -----*/

/* 出力テスト */
print "arr_plan_loom_type_twin";
var_dump($arr_plan_loom_type_twin);


$arr_result_sum = array();
/*----- チェックイン日 と プランルームタイプの合計 ------*/
$arr_result_sum = array_combine($arr_check_in_sum,$arr_plan_loom_type_sum);


/*----- 定数で 1日の最大部屋数 ------*/
const MAX_ROOM = 42;


/*------ 日付データ作成 ------*/
$arr_day1 = getNthDay($year, $month, $day, '+1 days');
$arr_day2 = getNthDay($year, $month, $day, '+2 days');
$arr_day3 = getNthDay($year, $month, $day, '+3 days');
$arr_day4 = getNthDay($year, $month, $day, '+4 days');
$arr_day5 = getNthDay($year, $month, $day, '+5 days');
$arr_day6 = getNthDay($year, $month, $day, '+6 days');


/*----- SQLから取り出したデータで 比較用データ作成 -----*/

$arr_result = array_combine($check_in, $plan_loom_type);

// var_dump($arr_result);

/*----- 変数定義 -----*/

// ルームタイプシングル用
$single_sum = 0;


/*---- ルームタイプ 合計処理 処理 -----*/
function loom_count($arr_r,$check_in,$arr_result) {

$loom_sum = 0;
$loom_false = 0;
 
if(in_array($arr_r, $check_in,true)) { /*----- ルームタイプ ありの場合 -------*/
foreach($arr_result as $key => $value) {

if($key == $arr_r) {
return print $value;
}
 
} /*----- END foreach --------*/
 
} else { /*----- ルームタイプ なの場合 -------*/
return print $loom_false;
}

}


?>

<!DOCTYPE html>
<html lang="ja" dir="ltr">
<head>
<meta charset="utf-8">
<title>週間カレンダー</title>

<style>
 
/*------管理画面 タイトル --------*/
.kanri-title-01 {text-align: center;background: #444;color: #fff;
font-size: 140%;padding: 10px 0px;}



/*------- 部屋カテゴリー ----------*/
#zan {background:#444; color:#fff;}
#economy {background: #ff4c67;}
#single {background-color: #abf5ff;}
#double {background-color: #ff99cc;}
#twin {background-color: #ffcc99;}
#washitu {background-color: #ccffcc;}

.r-text {display: inline-block;position: relative;top: -5px;padding-left: 5px;}

/* 残り部屋数 */
#r-zan {width:40px; height: 20px; display: inline-block;background-color:#444;border: 1px solid #333333;}

/* エコノミー */
#r-economy {width:40px; height: 20px; display: inline-block;background-color:#ff4c67;border: 1px solid #333333;}

/* シングル */
#r-single {width:40px; height: 20px; display: inline-block;background-color:#abf5ff;border: 1px solid #333333;}

/* ダブル */
#r-double {width:40px; height: 20px; display: inline-block;background-color:#ff99cc;border: 1px solid #333333;}

/* ツイン */
#r-twin {width:40px; height: 20px; display: inline-block;background-color:#ffcc99;border: 1px solid #333333;}

/* 和室 */
#r-wa {width:40px; height: 20px; display: inline-block;background-color:#ccffcc;border: 1px solid #333333;}

.room-color {margin-top: 1.5em;}


/*----------------------- スマホ -----------------------*/
@media screen and (max-width: 896px) and (min-width: 481px) and (orientation: portrait) {
}



</style>

</head>
<body>

<h1 class="kanri-title-01">週 管理カレンダー 残数管理</h1>

<table border="1" cellspacing="0" cellpadding="10" width="100%" style="border: 1px solid black; border-collapse: collapse;">

<tr>
<th colspan="2"><a href="http://localhost/wordpress/wp-content/themes/soutokukan_01/s-system/r-m-management.php/?date=<?php echo h($prev_week); ?>">&laquo; 前週</a></th>
<th colspan="3"><?php echo $year;?><?php echo $month; ?></th>
<th colspan="3"><a href="http://localhost/wordpress/wp-content/themes/soutokukan_01/s-system/r-m-management.php/?date=<?php echo h($next_week); ?>">次週 &laquo; </a></th>
</tr>

<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>

<!-- 1週間の日にち -->
<tr>
<td></td>
<?php print $table; ?>
</tr>

<!-- プラン別 残数 -->

<tr>
<td id="zan">
残り 部屋数
</td>

<!-- 1日目 -->
<?php if(in_array($year_month_day,$check_in,true)) : ?>
<?php foreach($arr_result_sum as $key => $value) : ?>
 
<?php if($year_month_day == $key) : ?>
<td>
<?php print MAX_ROOM - $value; ?>
</td>
<?php endif; ?>
<?php endforeach; ?>
 
<?php else : ?>
<td>
<?php print MAX_ROOM ; ?>
</td>
<?php endif; ?>

<!-- 2日目 -->

<?php if(in_array($arr_day1,$check_in,true)) : ?>
<?php foreach($arr_result_sum as $key => $value) : ?>
 
<?php if($arr_day1 == $key) : ?>
<td>
<?php print MAX_ROOM - $value; ?>
</td>
<?php endif; ?>
<?php endforeach; ?>
 
<?php else : ?>
<td>
<?php print MAX_ROOM ; ?>
</td>
<?php endif; ?>
 
<?php var_dump($arr_result) ?>
<?php print "arr_result<br><br>"; ?>
<?php var_dump($arr_result_sum); ?>
<?php print "arr_result_sum<br><br>"; ?>

<!-- 3日目 -->
<?php if(in_array($arr_day2, $check_in,true)) : ?>
<?php foreach($arr_result_sum as $key => $value) : ?>
<?php if($arr_day2 == $key) : ?>
<td>
<?php print MAX_ROOM - $value; ?>
</td>
<?php endif; ?>
<?php endforeach; ?>

<?php else: ?>
<td>
<?php print MAX_ROOM; ?>
</td>
<?php endif; ?>
 
<!-- 4日目 -->
<?php if(in_array($arr_day3, $check_in,true)) : ?>
<?php foreach($arr_result_sum as $key => $value) : ?>
<?php if($arr_day3 == $key) : ?>
<td>
<?php print MAX_ROOM - $value; ?>
</td>
<?php endif; ?>
<?php endforeach; ?>

<?php else: ?>
<td>
<?php print MAX_ROOM; ?>
</td>
<?php endif; ?>
 
<!-- 5日目 -->
<?php if(in_array($arr_day4, $check_in,true)) : ?>
<?php foreach($arr_result_sum as $key => $value) : ?>
<?php if($arr_day4 == $key) : ?>
<td>
<?php print MAX_ROOM - $value; ?>
</td>
<?php endif; ?>
<?php endforeach; ?>

<?php else: ?>
<td>
<?php print MAX_ROOM; ?>
</td>
<?php endif; ?>
 
<!-- 6日目 -->
<?php if(in_array($arr_day5, $check_in,true)) : ?>
<?php foreach($arr_result_sum as $key => $value) : ?>
<?php if($arr_day5 == $key) : ?>
<td>
<?php print MAX_ROOM - $value; ?>
</td>
<?php endif; ?>
<?php endforeach; ?>

<?php else: ?>
<td>
<?php print MAX_ROOM; ?>
</td>
<?php endif; ?>
 
<!-- 7日目 -->
<?php if(in_array($arr_day6, $check_in,true)) : ?>
<?php foreach($arr_result_sum as $key => $value) : ?>
<?php if($arr_day6 == $key) : ?>
<td>
<?php print MAX_ROOM - $value; ?>
</td>
<?php endif; ?>
<?php endforeach; ?>

<?php else: ?>
<td>
<?php print MAX_ROOM; ?>
</td>
<?php endif; ?>

 
</tr>

<tr>
<td id="economy">
エコノミー<br>
バス・トイレ無し
</td>

<td> 


</td>
 
<td> 
<!-- 週の1日目 -->
<?php print $arr_day1; ?>
</td>
 
<td> </td> <td> </td> <td> </td> <td> </td> <td> </td>
 
</tr>

<tr>
<td id="single">
シングル
</td>

<!-- 1日目 -->
<td> 
<?php loom_count($year_month_day,$check_in,$arr_plan_single_result); ?>
</td>

<!-- シングル 週の2日目 -->
<td><?php loom_count($arr_day1,$check_in,$arr_plan_single_result); ?></td>
<td><?php loom_count($arr_day2,$check_in,$arr_plan_single_result); ?></td>
<td><?php loom_count($arr_day3,$check_in,$arr_plan_single_result); ?></td>
<td><?php loom_count($arr_day4,$check_in,$arr_plan_single_result); ?></td>
<td><?php loom_count($arr_day5,$check_in,$arr_plan_single_result); ?></td>
<td><?php loom_count($arr_day6,$check_in,$arr_plan_single_result); ?></td>
</tr>

<tr>
<td id="single">
シングル(禁煙)
</td>

<td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td>
</tr>

<tr>
<td id="double">
ダブル
</td>

<!-- 1日目 -->
<td>
<?php loom_count($year_month_day, $check_in, $arr_plan_double_result); ?>
</td>
<!-- ダブル 2日目 -->
<td><?php loom_count($arr_day1, $check_in, $arr_plan_double_result); ?></td>
<td><?php loom_count($arr_day2, $check_in, $arr_plan_double_result); ?></td>
<td><?php loom_count($arr_day3, $check_in, $arr_plan_double_result); ?></td>
<td><?php loom_count($arr_day4, $check_in, $arr_plan_double_result); ?></td>
<td><?php loom_count($arr_day5, $check_in, $arr_plan_double_result); ?></td>
<td><?php loom_count($arr_day6, $check_in, $arr_plan_double_result); ?></td>

</tr>

<tr>
<td id="twin">
 ツイン
</td>

<!-- 1日目 -->
<td>
<?php loom_count($year_month_day,$check_in,$arr_plan_twin_result); ?>
</td>
<!-- ツイン 2日目 -->
<td><?php loom_count($arr_day1,$check_in,$arr_plan_twin_result); ?></td>
<td><?php loom_count($arr_day2,$check_in,$arr_plan_twin_result); ?></td>
<td><?php loom_count($arr_day3,$check_in,$arr_plan_twin_result); ?></td>
<td><?php loom_count($arr_day4,$check_in,$arr_plan_twin_result); ?></td>
<td><?php loom_count($arr_day5,$check_in,$arr_plan_twin_result); ?></td>
<td><?php loom_count($arr_day6,$check_in,$arr_plan_twin_result); ?></td>
</tr>

<tr>
<td id="washitu">
 和室
</td>

<!-- 1日目 -->
<td>
<?php loom_count($year_month_day, $check_in, $arr_plan_wa_result); ?>
</td>
<!-- 和室 2日目 -->
<td><?php loom_count($arr_day1, $check_in, $arr_plan_wa_result); ?></td>
<td><?php loom_count($arr_day2, $check_in, $arr_plan_wa_result); ?></td>
<td><?php loom_count($arr_day3, $check_in, $arr_plan_wa_result); ?></td>
<td><?php loom_count($arr_day4, $check_in, $arr_plan_wa_result); ?></td>
<td><?php loom_count($arr_day5, $check_in, $arr_plan_wa_result); ?></td>
<td><?php loom_count($arr_day6, $check_in, $arr_plan_wa_result); ?></td>
 
</tr>

</table>

<!-- room カラー -->

<div class="room-color">
<span id="r-zan"></span><span class="r-text">残り 部屋数</span>
<span id="r-economy"></span><span class="r-text">エコノミー</span>
<span id="r-single"></span><span class="r-text">シングル</span>
<span id="r-double"></span><span class="r-text">ダブル</span>
<span id="r-twin"></span><span class="r-text">ツイン</span>
<span id="r-wa"></span><span class="r-text">和室</span>
</div>

</body>
</html>