<?php
$keyname="tp_gmo_corp"; //GOM法人口座
$keyname="tp_gmo_priv"; //GMO個人口座
$keyname="tp_stripe"; //Stripe
$keyname="tp_paypal"; //Pyapal
$keyname="tp_gmo_priv"; // 今回
// 縦型がいいか、横型がいいか 横型のORマッパー形式
$tabledic=array();
$tabledic["tp_gmo_corp"]["tabelename"]="tp_gmo_corp";
$tabledic["tp_gmo_corp"]["culumzok"]=array(1,1,0,0,0,1);
$tabledic["tp_gmo_corp"]["culumselect"]=array(1,1,1,1,1,1);
$tabledic["tp_gmo_corp"]["culumcheck"]=array(1,1,1,1,1,1);
$tabledic["tp_gmo_corp"]["culumname"]=array("tp_datetime","tp_summary","tp_deposit","tp_investment","tp_balance","tp_memo");
$tabledic["tp_gmo_corp"]["filename"]="gmo_corp.csv";
$tabledic["tp_gmo_priv"]["tabelename"]="tp_gmo_priv";
$tabledic["tp_gmo_priv"]["culumzok"]=array(1,1,0,0,0,1);
$tabledic["tp_gmo_priv"]["culumselect"]=array(1,1,1,1,1,1);
$tabledic["tp_gmo_priv"]["culumcheck"]=array(1,1,1,1,1,1);
$tabledic["tp_gmo_priv"]["culumname"]=array("tp_datetime","tp_summary","tp_deposit","tp_investment","tp_balance","tp_memo");
$tabledic["tp_gmo_priv"]["filename"]="gmo_priv.csv";
$tabledic["tp_stripe"]["tabelename"]="tp_stripe";
$tabledic["tp_stripe"]["culumzok"]=array(1,1,0,0,0,1);
$tabledic["tp_stripe"]["culumselect"]=array(1,1,1,1,1,1);
$tabledic["tp_stripe"]["culumcheck"]=array(1,1,1,1,1,1);
$tabledic["tp_stripe"]["culumname"]=array("tp_datetime","tp_summary","tp_deposit","tp_investment","tp_balance","tp_memo");
$tabledic["tp_stripe"]["filename"]="stripe.csv";
$tabledic["tp_paypal"]["tabelename"]="tp_paypal";
$tabledic["tp_paypal"]["culumzok"]=array(1,1,0,0,0,1);
$tabledic["tp_paypal"]["culumselect"]=array(1,1,1,1,1,1);
$tabledic["tp_paypal"]["culumcheck"]=array(1,1,1,1,1,1);
$tabledic["tp_paypal"]["culumname"]=array("tp_datetime","tp_summary","tp_deposit","tp_investment","tp_balance","tp_memo");
$tabledic["tp_paypal"]["filename"]="paypal.csv";
$tabelename=$tabledic[$keyname]["tabelename"];
$culumzok=$tabledic[$keyname]["culumzok"];
$culumselect=$tabledic[$keyname]["culumselect"];
$culumcheck=$tabledic[$keyname]["culumcheck"];
$culumname=$tabledic[$keyname]["culumname"];
$filename=$tabledic[$keyname]["filename"];
print_r($tabelename);
print("----\n");
print_r($culumzok);
print("----\n");
print_r($culumselect);
print("----\n");
print_r($culumcheck);
print("----\n");
print_r($culumname);
print("----\n");
print_r($filename);
print("----\n");
//exit("ok");
// 2回目はandがつこので、 最初だけの処理で行う
// foreach php 最初だけ
// 参考 https://qiita.com/_hiro_dev/items/fc48722eb518c6382895
$sql_select="select count(*) from ".$tabelename." where ";
foreach ($culumname as $index => $culum) {
if(!($index === array_key_first($culumname))){ // 1回目以降
$sql_select=$sql_select."and ";
}
$sql_select=$sql_select.$culum."= :".$culum." ";
}
print($sql_select);
//$Sql = "insert into ".$tabelename."tp_gmo_corp (tp_datetime,tp_summary,tp_deposit,tp_investment,tp_balance,tp_memo) values (?,?,?,?,?,?)";
$sql_insert="insert into ".$tabelename."(";
$sql_insert2="";
foreach ($culumname as $index => $culum) {
if(!($index === array_key_first($culumname))){ // 1回目以降
$sql_insert=$sql_insert.", ";
$sql_insert2=$sql_insert2.",";
}
$sql_insert=$sql_insert.$culum;
$sql_insert2=$sql_insert2."?";
}
$sql_insert=$sql_insert.") values (".$sql_insert2.")";
print($sql_select);
print("\n");
print($sql_insert);
//exit("ok");
try {
$pdo = new PDO('mysql:dbname=openbook;host=xxx.xxx.xxx.xxx;charset=utf8mb4', 'acountname', 'password', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
} catch (PDOException $e) {
exit($e->getMessage());
}
// CSVファイルを読み込みモードで開く
$file_name="inp_data.csv";
//$file_name="inp_data_.csv";
$file_name=$filename;
$fp = fopen($file_name, "r");
// ファイルを1行ずつ取得する
$fl=0;
while (($line = fgetcsv($fp)) !== FALSE) {
for ($c = 0; $c < count($line); $c++) {
$str = mb_convert_encoding($line[$c] ,"utf-8","sjis");
print("---------\n");
print($str);
print("---------\n");
if($c==2){
print($c);
print("=");
print($str);
print(":");
}
}
if($fl != 0){// ヘッダーは除外
$stmt = $pdo->prepare($sql_select);
foreach ($culumname as $index => $culum) {
if ($culumzok[$index]==1){
$stmt->bindValue(':'.$culum, mb_convert_encoding($line[$index],"utf-8","sjis") , PDO::PARAM_STR);
}else{
if($line[$index]==''){
$stmt->bindValue(':'.$culum, "0" , PDO::PARAM_INT);
}else{
$stmt->bindValue(':'.$culum, $line[$index] , PDO::PARAM_INT);
}
}
}
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 参考 http://hono-wp.seesaa.net/article/411183815.html
// https://qiita.com/wakahara3/items/d7a3674eecd3b021a21e
print_r($result);
print($result[0]["count(*)"]);
if($result[0]["count(*)"]==0){
print("Write");
$stmt = $pdo->prepare($sql_insert);
$mapdata=array();
foreach ($culumname as $index => $culum) {
// $setdata=$line[$index];
if ($culumzok[$index]==1) { // 文字列
$setdata=mb_convert_encoding($line[$index],"utf-8","sjis") ;
}else{ //数値
if ($line[$index]==""){
$setdata="0";}else{
$setdata=$line[$index];
}}
array_push($mapdata,$setdata);
}
print_r($mapdata);
$flag = $stmt->execute($mapdata);
print($flag);
}
if($result[0]["count(*)"]=!0){
print("Send");
}
}
$fl=1;
}
fclose($fp);
?>