投稿

ラベル(スニペット)が付いた投稿を表示しています

PHPスニペット・文字列関連

 随時追加 空白・スペース除去 $string = str_replace(array(" ", " "), "", $string); String改行で分割 $body = str_replace(array("\r\n", "\r", "\n"), "\n", $body); $body_list = explode("\n", $body);

JSスニペット 非同期関連

複数非同期通信のスマートなawait await並べるだけだと順次通信してしまうので工夫する const get_schedules_days=async()=>{ //これだと遅い //await get_a_schedules(); //await get_b_schedules(); let results=[]; results.push(get_a_schedules()); results.push(get_b_schedules()); await Promise.all(results); schedules_days_show(); } const get_a_schedules=async()=>{ await $.ajax({ type : "GET", url : "./rest/get_a_schedules/", data : { }, dataType : "json", cache : false }).done(function (data) { }).fail(function (data) { }).always(function (data) { }); } const get_b_schedules=async()=>{ await $.ajax({ type : "GET", url : "./rest/get_b_schedules/", data : { }, dataType : "json", cache : false }).done(function (data) { }).fail(function (data) { }).always(function (data) { }); }

JSスニペット DOM関連

idセレクタ 基本から document.getElementById("id") Select選択をvalueに合わせる ※通信編でおまけとして書いたやつ const selected_option =(target_id,value)=>{ let option_list = document.getElementById(target_id).options; for ( var r_key in option_list) { option_row = option_list[r_key]; if(option_row.value==value){ option_row.selected = true; } } }

JSスニペット 通信関連

 GETパラメタ取得 var queryString = window.location.search; if(queryString){ queryString = decodeURI(queryString.substring(1)); var parameters = queryString.split('&'); for (var param of parameters) { var element = param.split('='); var paramName = decodeURIComponent(element[0]); var paramValue = decodeURIComponent(element[1]); queryObject[paramName] = paramValue; } }  API叩いたとき向けselectのoptionをjsonで持ってきて指定のエレメントにセットする const setSelect = async(url,element_id,value_key,text_key)=> { await $.ajax({ type : "GET", url : url, data : { }, dataType : "json", cache : false }).done(function (json) { var select_element = document.getElementById(element_id); select_element.innerHTML = ""; for ( var romMap of json["data"]) { var option = document.createElement('option'); option.setAttribute('value', romMap[value_key]); option.innerHTML = romMap[text_key]; ...

PHPスニペット・日付関連

 随時追加 DateTimeオブジェクト //指定 $date = new DateTime('2021-01-01 00:00:00'); //今 $date = new DateTime(); $today_ymd = $date->format('Y-m-d'); //1日後 $date->modify('+1 days'); $nextday_ymd = $date->format('Y-m-d'); //1年前 $date->modify('-1 years');

PHPスニペット スマレジAPI関係

スマレジAPIをPHPで叩くときのスニペット 契約IDとアクセストークンをよういしてください。 またスマレジディベロッパーのAPIとは別なので注意。そちらのほうが機能が多いし更新があるらしいが、使い方に癖がぜんぜん違うので後ほど調査 ここに載せているスマレジAPIバージョン2は今後更新しないとのこと。 paramsの注意点 改行コードが有るとだめみたいなのです・・・ 取り置きの取得 disposeDivisionを0指定にしないとキャンセルした取り置きとキャンセル取り置き注文が引っかかってしまう function get_transaction_torioki( $contract_id ,$access_token){ $data='proc_name=transaction_ref&params={"fields":[ "transactionHeadId", "transactionDateTime", "transactionHeadDivision", "cancelDivision", "unitNonDiscountsubtotal", "unitDiscountsubtotal", "subtotal", "subtotalDiscountPrice", "subtotalDiscountRate", "subtotalDiscountDivision", "pointDiscount", "total", "taxExclude", "taxInclude", "roundingDivision", "roundingPrice", "cashTotal", "creditTotal...

PHPスニペット バックログAPI関係

nulabバックログのAPI をPHPで叩くときのスニペット APIキー(個人設定のAPIから取得可能)と割り当てられたURLを用意してください。 プロジェクト一覧の取得 https://developer.nulab.com/ja/docs/backlog/api/2/get-project-list/# 管理者権限のAPIキーで全部所得 function get_projects( $baseurl ,$apiKey){ $api_url = $baseurl."/api/v2/projects?apiKey=".$apiKey."&all=true"; $headers = array('Content-Type:application/x-www-form-urlencoded'); $context = array( 'http' => array( 'method' => 'GET', 'header' => $headers, 'ignore_errors' => true, ) ); $response = file_get_contents($api_url, false, stream_context_create($context)); return json_decode($response,true); } ユーザー一覧の取得 https://developer.nulab.com/ja/docs/backlog/api/2/get-user-list/# 全員分取得 function get_users( $baseurl ,$apiKey){ $api_url = $baseurl."/api/v2/users?apiKey=...

JSスニペット 日付関連

 随時追加 簡易yyyy/MM/dd 一桁月、日がおかしかったので修正 var day = new Date(); document.getElementById("today").innerHTML = day.getFullYear()+"/"+(("00" + (day.getMonth()+1)).slice(-2))+"/"+("00" + day.getDate()).slice(-2)+""; 日時、曜日 var today = new Date(); var dayOfWeek = today.getDay() ; // 曜日(数値) var dayOfWeekStr = [ "日", "月", "火", "水", "木", "金", "土" ][dayOfWeek] ;

JSスニペット 文字列関連

随時追加 文字列から空白除去 string = string .replace(/\s+/g, ""); 文字列部分検索 var string = 'abcdefg'; var pattern = 'fg'; if(string.indexOf(pattern) > -1){ // 部分一致のときの処理 } 名前を空白除去でlist検索例 const imgList = [{"name":"山田 太郎","href":"yamada.jpg"},{"name":"田中 次郎","href":"tanaka.jpg"}]; const getTantouImg = (name)=>{ name = name.replace(/\s+/g, ""); for(const imgMap of imgList){ let imgMapName = imgMap.name.replace(/\s+/g, ""); if(imgMapName.startsWith(name)){ return imgMap.href; } } return "./img/nonimg.jpg"; } URLエンコード・デコード const uri = 'https://mozilla.org/?x=шеллы'; const encoded = encodeURI(uri); console.log(encoded); // expected output: "https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B" try { console.log(decodeURI(encoded)); // expected output: "https://mozilla.org/?x=шеллы" } catch (e) { // catches ...