本記事まで対応しているソースコード
※ vol.17 までのバグ修正あり
現在の構造
.
├── css
│ ├── bootstrap-common.css
│ └── fmda-form.css
├── csv
│ └── standings.csv
├── find-records-test.php
├── find-records-test2.php
├── find-records-test3.php
├── html
│ └── edit-record.html
├── index.php
├── js
│ ├── fmda-form-check.js
│ └── fmda-select-page.js
├── json
│ ├── html-contents.json
│ ├── standings-al-east-2024.json
│ └── standings-all-2024.json
└── php
├── bs-builder-class.php
├── bs-frame-class.php
├── filemaker-data-api-class.php
├── php-functions.php
├── preferences.php
└── temporary-functions.php
リクエスト後のレコード表示・遷移の準備
ちょっと、確定申告で、こちらが停滞気味です。
今回のテーマ。長くなりそうなので、数回に分けます。
あまり色んな技術を混ぜないでやった方がいいのではということで、一時的なストレージを使わない方針でやってきましたが、多少後悔しています。
使った方がスッキリできたような……。仕方ありません。このままいきます。
Edit Record のリクエストを出す前に、リクエスト処理後を考えておく必要があります。1レコード編集したら、次のレコードへというユーザの行動は容易に想像つきますよね。
最大30件ですから、Find Records でどんどんやった方がかなり簡単になると思うのですが、これも Find Records はファーストアクセスの1回と決めてしまったので、不採用になります。
本来なら、こういう最大30件と条件が決まっているものは採用してしまった方がいいと思いますが、強行します。
編集機能だけを考えて、リクエスト待ちのページの状態として、
- ファーストアクセスでの表示
- ファーストアクセス以外で1レコード目を含む任意のレコードでの表示
前者は Find Records で取得したデータを使っています。これは、temporary-functions.php
の records_find_records()
関数の中で処理しています。
ここで、処理したデータを後者で再利用したいわけです。
一時的なストレージを使うのであれば、ここで保存するわけです。
それをしないという方針を立ててしまったので、代替案が必要です。
どのレコードを選択しても、レコードのデータが必要なわけですから、取得したデータ丸ごと必要です。
まず、フォームから POST で受け取りたい情報を整理しましょう。
-
ファーストアクセス(Edit Record)
必要なし -
Create Record(レコード新規作成)
FileMaker 情報ブロックの情報
このリクエスト処理後の遷移をファーストアクセスと解すれば、以上のみ。
一連の流れの中にあると解すれば、処理後のレスポンス情報とFileMaker 情報ブロックの情報、直前のページ情報、現在のページ情報 -
Edit Record(レコードの編集)但し、ファーストアクセス以外
レスポンス情報、FileMaker 情報ブロックの情報、現在のページ情報 -
Delete Record()
というわけで、必要なデータを全部、
hidden
タイプのフォームコントロールに仕込んでおく方法を採りたいと思います。
必要な情報は、右の FileMaker 情報ブロックの情報の8項目と Find Records で取得している全レコードの情報です。
FileMaker 情報ブロックの8項目は、そのまま埋め込み、Find Records の取得済みレコード情報は JSON 化して、response-data
として埋め込みます。
必要な変更を加えましょう。まず、JSON に追加する hidden
タイプのフォームコントロールを追加します。
html-contents.json の formControls JSON 配列
"formControls" :
[
{
"id" : "mlb_site_id",
"label" : "MLB 公式サイト ID",
"type" : "text",
"name" : "mlb_site_id",
"placeholder" : "100",
"required" : "1",
"disabled" : "",
"feedback" : "MLB 公式サイトの チーム ID を入力してください。",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "year",
"label" : "シーズン",
"type" : "text",
"name" : "year",
"placeholder" : "2024",
"required" : "1",
"disabled" : "",
"feedback" : "シーズン(西暦)を入力してください。",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "league",
"label" : "リーグ",
"type" : "select",
"name" : "league",
"placeholder" : "",
"required" : "1",
"disabled" : "",
"feedback" : "リーグを選択してください。",
"option" : [
{
"value" : "アメリカン・リーグ"
},
{
"value" : "ナショナル・リーグ"
}
]
},
{
"id" : "division",
"label" : "地区",
"type" : "select",
"name" : "division",
"placeholder" : "",
"required" : "1",
"disabled" : "",
"feedback" : "地区を選択してください。",
"option" : [
{
"value" : "東地区"
},
{
"value" : "中地区"
},
{
"value" : "西地区"
}
]
},
{
"id" : "team",
"label" : "チーム名",
"type" : "text",
"name" : "team",
"placeholder" : "",
"required" : "1",
"disabled" : "",
"feedback" : "チーム名を入力してください。",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "wins",
"label" : "勝利数",
"type" : "text",
"name" : "wins",
"placeholder" : "81",
"required" : "1",
"disabled" : "",
"feedback" : "レギュラーシーズンの勝利数を入力してください。",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "losses",
"label" : "敗戦数",
"type" : "text",
"name" : "losses",
"placeholder" : "81",
"required" : "1",
"disabled" : "",
"feedback" : "レギュラーシーズンの敗戦数を入力してください。。",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "winning_percentage",
"label" : "勝率",
"type" : "text",
"name" : "winning_percentage",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "runs_scored",
"label" : "得点",
"type" : "text",
"name" : "runs_scored",
"placeholder" : "",
"required" : "1",
"disabled" : "",
"feedback" : "レギュラーシーズンの得点を入力してください。",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "runs_allowed",
"label" : "失点",
"type" : "text",
"name" : "runs_allowed",
"placeholder" : "",
"required" : "1",
"disabled" : "",
"feedback" : "レギュラーシーズンの失点を入力してください。",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "pythagorean_expectation",
"label" : "ピタゴラス勝率",
"type" : "text",
"name" : "pythagorean_expectation",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "expected_win_loss",
"label" : "X-W/L",
"type" : "text",
"name" : "expected_win_loss",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "winning_percentage_difference",
"label" : "勝率差分",
"type" : "text",
"name" : "winning_percentage_difference",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "database",
"label" : "",
"type" : "hidden",
"name" : "database",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "layout",
"label" : "",
"type" : "hidden",
"name" : "layout",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "table",
"label" : "",
"type" : "hidden",
"name" : "table",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "recordId",
"label" : "",
"type" : "hidden",
"name" : "record-id",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "modId",
"label" : "",
"type" : "hidden",
"name" : "mod-id",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "totalRecordCount",
"label" : "",
"type" : "hidden",
"name" : "total-record-count",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "foundCount",
"label" : "",
"type" : "hidden",
"name" : "found-count",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "returnedCount",
"label" : "",
"type" : "hidden",
"name" : "returnd-count",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
},
{
"id" : "responseData",
"label" : "",
"type" : "hidden",
"name" : "response-data",
"placeholder" : "",
"required" : "",
"disabled" : "",
"feedback" : "",
"option" : [
{
"value" : ""
}
]
}
],
今更ですが、キーの命名は、以下で書いています。
- id → camelCase
- レコードのフィールドデータの name → snake_case
- それ以外の HTML 属性 → kebab-case
次に、用意したフォームコントロールのデータを用意するわけですが、最後の response-data
の生成用に関数を追加します。
php-functions.php に追加
function mb_left(string $str, int $length, string $encoding = "UTF-8"): string
{
return mb_substr($str, 0, $length, "UTF-8");
}
データ作成は、Find Records のリクエスト処理後に作るので、temporary-functions.php
の records-find-records()
関数を変更します。
temporary-functions.php の records-find-records() 関数
function records_find_records( ): void
{
global $fmda, $response, $bearer_session_token, $database, $layout, $request_body;
global $result, $data_info, $team_data, $response_field_data;
global $info_item_content;
if ($fmda->getMessagesCode($response) === '0') {
// セッション有効なら、行いたいリクエスト実行
// レコードの検索
$response = $fmda->findRecords($database, $layout, $bearer_session_token, $request_body);
// 結果セット処理
$result = $fmda->json2array($response);
if ($fmda->getMessagesCode($response) === '0') {
$data_info = $result['response']['dataInfo'];
$team_data = $result['response']['data'];
// vol.18 追加
$response_data = '[';
foreach ($team_data as $key => $value) {
$response_data .= "{"fieldData": {";
foreach ($value['fieldData'] as $filed_data_key => $field_data_value) {
$response_data .= """ . $filed_data_key . "": "" . $field_data_value . "", ";
}
$response_data = mb_left($response_data, mb_strlen($response_data) - 2);
$response_data .= "}, "recordId": "" . $value['recordId'] . "", ";
$response_data .= ""modId": "" . $value['modId'] . ""}, ";
$response_data = mb_left($response_data, mb_strlen($response_data) - 2);
$response_data .= ", ";
}
$response_data = mb_left($response_data, mb_strlen($response_data) - 2);
$response_data .= "]";
// 追加部分
array_push($info_item_content, $data_info['database']);
array_push($info_item_content, $data_info['layout']);
array_push($info_item_content, $data_info['table']);
array_push($info_item_content, $team_data[0]['recordId']);
array_push($info_item_content, $team_data[0]['modId']);
array_push($info_item_content, $data_info['totalRecordCount']);
array_push($info_item_content, $data_info['foundCount']);
array_push($info_item_content, $data_info['returnedCount']);
// vol.18 変更部分
$response_field_data = [
$team_data[0]['fieldData']['mlb_site_id'],
$team_data[0]['fieldData']['year'],
$team_data[0]['fieldData']['league'],
$team_data[0]['fieldData']['division'],
$team_data[0]['fieldData']['team'],
$team_data[0]['fieldData']['wins'],
$team_data[0]['fieldData']['losses'],
$team_data[0]['fieldData']['winning_percentage'],
$team_data[0]['fieldData']['runs_scored'],
$team_data[0]['fieldData']['runs_allowed'],
$team_data[0]['fieldData']['pythagorean_expectation'],
$team_data[0]['fieldData']['expected_win_loss'],
$team_data[0]['fieldData']['winning_percentage_difference'],
$data_info['database'],
$data_info['layout'],
$data_info['table'],
$team_data[0]['recordId'],
$team_data[0]['modId'],
$data_info['totalRecordCount'],
$data_info['foundCount'],
$data_info['returnedCount'],
$response_data
];
}
} else {
// セッション無効なら、必要な Web データを保持して、ユーザに再ログインを促す
echo "<div class=\"alert alert-warning\" role=\"alert\">セッションが切れました。再ログインしてください。</div>\n";
}
}
preferences.php
の66行目 $response_field_data = [];
の下にフォームコントロールへ埋め混むFileMaker 情報用の変数を設定しておきます。
preferences.php へ追加
$data_info_database = '';
$data_info_layout = '';
$data_info_table = '';
$data_info_record_id = '';
$data_info_mod_id = '';
$data_info_total_record_count = '';
$data_info_found_count = '';
$data_info_returned_count = '';
hidden
フォームコントロールに対応するため、bs-builder-class.php
の CreateForm()
メソッドを変更します。
bs-builder-class.php の CreateForm() メソッドの変更
static function CreateForm (
array &$form_prefernces,
array &$form_controls,
array &$field_data,
): void {
echo <<<_HTML_
<div class="col-md-7 col-lg-8">
<h4 class="mb-3">{$form_prefernces[0]}</h4>
<form action="{$form_prefernces[1]}" method="POST" class="needs-validation" novalidate>
<div class="row g-3">
_HTML_; // begin
$i = 0;
foreach ($form_controls as $key => $value) {
$required = '';
$secondary_label = '';
if ($value['required'] === '1')
$required = 'required';
else
$secondary_label = '<span class="text-body-secondary">(任意)</span>';
if ($value['type'] === 'select') {
echo <<<_HTML_
<div class="col-sm-6">
<label for="{$value['id']}" class="form-label">{$value['label']}{$secondary_label}</label>
<select class="form-select" id="{$value['id']}" name="{$value['name']}" {$required} {$value['disabled']}>
<option value="">選択...</option>
_HTML_;
foreach ($value['option'] as $option_key => $option_value) {
$selected = $field_data[$i] === $option_value['value'] ? 'selected' : '';
$current_value = $selected === 'selected' ? $field_data[$i] : $option_value['value'];
echo "<option value=\"{$current_value}\" {$selected} >{$option_value['value']}</option>\n";
}
echo "</select>\n";
} elseif ($value['type'] === 'hidden') {
echo <<<_HTML_
<div class="col-sm-6">
<input type="{$value['type']}" class="form-control" id="{$value['id']}" name="{$value['name']}" value="{$field_data[$i]}">
_HTML_;
} else {
echo <<<_HTML_
<div class="col-sm-6">
<label for="{$value['id']}" class="form-label">{$value['label']}{$secondary_label}</label>
<input type="{$value['type']}" class="form-control" id="{$value['id']}" name="{$value['name']}" value="{$field_data[$i]}" placeholder="{$value['placeholder']}" {$required} {$value['disabled']}>
_HTML_;
} // if ($value['type'] === 'select')
if (!empty($required)) {
echo <<<_HTML_
<div class="invalid-feedback">
{$value['feedback']}
</div>
_HTML_;
} // if (!empty($required))
echo "</div>\n";
$i += 1;
} // foreach ($form_controls as $key => $value)
echo <<<_HTML_
</div> <!-- / .row -->
<hr class="my-4">
<button class="w-100 btn {$form_prefernces[2]} btn-lg" type="submit">{$form_prefernces[3]}</button>
</form> <!-- / .needs-validation -->
_HTML_;
}
これで一旦、ファーストアクセスして、追加した hidden
タイプのコントロールがどうなっているか、吐き出された HTML ソースコードを見てみましょう。
<div class="col-sm-6">
<input type="hidden" class="form-control" id="database" name="database" value="BaseballSavant">
</div>
<div class="col-sm-6">
<input type="hidden" class="form-control" id="layout" name="layout" value="standings">
</div>
<div class="col-sm-6">
<input type="hidden" class="form-control" id="table" name="table" value="standings">
</div>
<div class="col-sm-6">
<input type="hidden" class="form-control" id="recordId" name="record-id" value="3">
</div>
<div class="col-sm-6">
<input type="hidden" class="form-control" id="modId" name="mod-id" value="0">
</div>
<div class="col-sm-6">
<input type="hidden" class="form-control" id="totalRecordCount" name="total-record-count" value="27">
</div>
<div class="col-sm-6">
<input type="hidden" class="form-control" id="foundCount" name="found-count" value="27">
</div>
<div class="col-sm-6">
<input type="hidden" class="form-control" id="returnedCount" name="returnd-count" value="27">
</div>
<div class="col-sm-6">
<input type="hidden" class="form-control" id="responseData" name="response-data" value="[{"fieldData": {"standings_id": "3", "mlb_site_id": "147", "year": "2024", "league": "アメリカン・リーグ", "division": "東地区", "team": "ニューヨーク・ヤンキース", "wins": "94", "losses": "68", "winning_percentage": "", "runs_scored": "815", "runs_allowed": "668", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "3", "modId": "0"}, {"fieldData": {"standings_id": "7", "mlb_site_id": "114", "year": "2024", "league": "アメリカン・リーグ", "division": "中地区", "team": "クリーブランド・ガーディアンズ", "wins": "92", "losses": "69", "winning_percentage": "", "runs_scored": "708", "runs_allowed": "621", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "7", "modId": "0"}, {"fieldData": {"standings_id": "1", "mlb_site_id": "110", "year": "2024", "league": "アメリカン・リーグ", "division": "東地区", "team": "ボルチモア・オリオールズ", "wins": "91", "losses": "71", "winning_percentage": "", "runs_scored": "786", "runs_allowed": "699", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "1", "modId": "0"}, {"fieldData": {"standings_id": "12", "mlb_site_id": "117", "year": "2024", "league": "アメリカン・リーグ", "division": "西地区", "team": "ヒューストン・アストロズ", "wins": "88", "losses": "73", "winning_percentage": "", "runs_scored": "740", "runs_allowed": "649", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "12", "modId": "0"}, {"fieldData": {"standings_id": "8", "mlb_site_id": "116", "year": "2024", "league": "アメリカン・リーグ", "division": "中地区", "team": "デトロイト・タイガース", "wins": "86", "losses": "76", "winning_percentage": "", "runs_scored": "682", "runs_allowed": "642", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "8", "modId": "0"}, {"fieldData": {"standings_id": "9", "mlb_site_id": "118", "year": "2024", "league": "アメリカン・リーグ", "division": "中地区", "team": "カンザスシティ・ロイヤルズ", "wins": "86", "losses": "76", "winning_percentage": "", "runs_scored": "735", "runs_allowed": "644", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "9", "modId": "0"}, {"fieldData": {"standings_id": "14", "mlb_site_id": "136", "year": "2024", "league": "アメリカン・リーグ", "division": "西地区", "team": "シアトル・マリナーズ", "wins": "85", "losses": "77", "winning_percentage": "", "runs_scored": "676", "runs_allowed": "607", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "14", "modId": "0"}, {"fieldData": {"standings_id": "10", "mlb_site_id": "142", "year": "2024", "league": "アメリカン・リーグ", "division": "中地区", "team": "ミネソタ・ツインズ", "wins": "82", "losses": "80", "winning_percentage": "", "runs_scored": "742", "runs_allowed": "735", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "10", "modId": "0"}, {"fieldData": {"standings_id": "2", "mlb_site_id": "111", "year": "2024", "league": "アメリカン・リーグ", "division": "東地区", "team": "ボストン・レッドソックス", "wins": "81", "losses": "81", "winning_percentage": "", "runs_scored": "751", "runs_allowed": "747", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "2", "modId": "0"}, {"fieldData": {"standings_id": "4", "mlb_site_id": "139", "year": "2024", "league": "アメリカン・リーグ", "division": "東地区", "team": "タンパベイ・レイズ", "wins": "80", "losses": "82", "winning_percentage": "", "runs_scored": "604", "runs_allowed": "663", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "4", "modId": "0"}, {"fieldData": {"standings_id": "15", "mlb_site_id": "140", "year": "2024", "league": "アメリカン・リーグ", "division": "西地区", "team": "テキサス・レンジャーズ", "wins": "78", "losses": "84", "winning_percentage": "", "runs_scored": "683", "runs_allowed": "738", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "15", "modId": "0"}, {"fieldData": {"standings_id": "5", "mlb_site_id": "141", "year": "2024", "league": "アメリカン・リーグ", "division": "東地区", "team": "トロント・ブルージェイズ", "wins": "74", "losses": "88", "winning_percentage": "", "runs_scored": "671", "runs_allowed": "743", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "5", "modId": "0"}, {"fieldData": {"standings_id": "11", "mlb_site_id": "133", "year": "2024", "league": "アメリカン・リーグ", "division": "西地区", "team": "オークランド・アスレチックス", "wins": "69", "losses": "93", "winning_percentage": "", "runs_scored": "643", "runs_allowed": "764", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "11", "modId": "0"}, {"fieldData": {"standings_id": "13", "mlb_site_id": "108", "year": "2024", "league": "アメリカン・リーグ", "division": "西地区", "team": "ロサンゼルス・エンゼルス", "wins": "63", "losses": "99", "winning_percentage": "", "runs_scored": "635", "runs_allowed": "797", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "13", "modId": "0"}, {"fieldData": {"standings_id": "6", "mlb_site_id": "145", "year": "2024", "league": "アメリカン・リーグ", "division": "中地区", "team": "シカゴ・ホワイトソックス", "wins": "41", "losses": "121", "winning_percentage": "", "runs_scored": "507", "runs_allowed": "813", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "6", "modId": "0"}, {"fieldData": {"standings_id": "19", "mlb_site_id": "143", "year": "2024", "league": "ナショナル・リーグ", "division": "東地区", "team": "フィラデルフィア・フィリーズ", "wins": "95", "losses": "67", "winning_percentage": "", "runs_scored": "784", "runs_allowed": "671", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "19", "modId": "0"}, {"fieldData": {"standings_id": "23", "mlb_site_id": "158", "year": "2024", "league": "ナショナル・リーグ", "division": "中地区", "team": "ミルウォーキー・ブリュワーズ", "wins": "93", "losses": "69", "winning_percentage": "", "runs_scored": "777", "runs_allowed": "641", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "23", "modId": "0"}, {"fieldData": {"standings_id": "16", "mlb_site_id": "144", "year": "2024", "league": "ナショナル・リーグ", "division": "東地区", "team": "アトランタ・ブレーブス", "wins": "89", "losses": "73", "winning_percentage": "", "runs_scored": "704", "runs_allowed": "607", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "16", "modId": "0"}, {"fieldData": {"standings_id": "18", "mlb_site_id": "121", "year": "2024", "league": "ナショナル・リーグ", "division": "東地区", "team": "ニューヨーク・メッツ", "wins": "89", "losses": "73", "winning_percentage": "", "runs_scored": "768", "runs_allowed": "697", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "18", "modId": "0"}, {"fieldData": {"standings_id": "26", "mlb_site_id": "109", "year": "2024", "league": "ナショナル・リーグ", "division": "西地区", "team": "アリゾナ・ダイアモンドバックス", "wins": "89", "losses": "73", "winning_percentage": "", "runs_scored": "886", "runs_allowed": "788", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "26", "modId": "0"}, {"fieldData": {"standings_id": "21", "mlb_site_id": "112", "year": "2024", "league": "ナショナル・リーグ", "division": "中地区", "team": "シカゴ・カブス", "wins": "83", "losses": "79", "winning_percentage": "", "runs_scored": "736", "runs_allowed": "669", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "21", "modId": "0"}, {"fieldData": {"standings_id": "25", "mlb_site_id": "138", "year": "2024", "league": "ナショナル・リーグ", "division": "中地区", "team": "セントルイス・カージナルス", "wins": "83", "losses": "79", "winning_percentage": "", "runs_scored": "672", "runs_allowed": "719", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "25", "modId": "0"}, {"fieldData": {"standings_id": "22", "mlb_site_id": "113", "year": "2024", "league": "ナショナル・リーグ", "division": "中地区", "team": "シンシナティ・レッズ", "wins": "77", "losses": "85", "winning_percentage": "", "runs_scored": "699", "runs_allowed": "694", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "22", "modId": "0"}, {"fieldData": {"standings_id": "24", "mlb_site_id": "134", "year": "2024", "league": "ナショナル・リーグ", "division": "中地区", "team": "ピッツバーグ・パイレーツ", "wins": "76", "losses": "86", "winning_percentage": "", "runs_scored": "665", "runs_allowed": "739", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "24", "modId": "0"}, {"fieldData": {"standings_id": "20", "mlb_site_id": "120", "year": "2024", "league": "ナショナル・リーグ", "division": "東地区", "team": "ワシントン・ナショナルズ", "wins": "71", "losses": "91", "winning_percentage": "", "runs_scored": "660", "runs_allowed": "764", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "20", "modId": "0"}, {"fieldData": {"standings_id": "17", "mlb_site_id": "146", "year": "2024", "league": "ナショナル・リーグ", "division": "東地区", "team": "マイアミ・マーリンズ", "wins": "62", "losses": "100", "winning_percentage": "", "runs_scored": "637", "runs_allowed": "841", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "17", "modId": "0"}, {"fieldData": {"standings_id": "27", "mlb_site_id": "115", "year": "2024", "league": "ナショナル・リーグ", "division": "西地区", "team": "コロラド・ロッキーズ", "wins": "61", "losses": "101", "winning_percentage": "", "runs_scored": "682", "runs_allowed": "929", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "27", "modId": "0"}]">
</div>
うまくいきました。JSON のダブルクォーテーションは、HTML エンティティの "
を使っています。これ JavaScript を使うとシングルクォーテーションでもうまくいくはずですが、このようなJSONストリングを、シングルクォーテーションを使って、json_decode()
を使ってもうまくいきません。submit 後のデータは、次のような状態で、$_POST['response-data']
に入ってきます。
[{"fieldData": {"standings_id": "3", "mlb_site_id": "147", "year": "2024", "league": "アメリカン・リーグ", "division": "東地区", "team": "ニューヨーク・ヤンキース", "wins": "94", "losses": "68", "winning_percentage": "", "runs_scored": "815", "runs_allowed": "668", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "3", "modId": "0"}, {"fieldData": {"standings_id": "7", "mlb_site_id": "114", "year": "2024", "league": "アメリカン・リーグ", "division": "中地区", "team": "クリーブランド・ガーディアンズ", "wins": "92", "losses": "69", "winning_percentage": "", "runs_scored": "708", "runs_allowed": "621", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "7", "modId": "0"}, {"fieldData": {"standings_id": "1", "mlb_site_id": "110", "year": "2024", "league": "アメリカン・リーグ", "division": "東地区", "team": "ボルチモア・オリオールズ", "wins": "91", "losses": "71", "winning_percentage": "", "runs_scored": "786", "runs_allowed": "699", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "1", "modId": "0"}, {"fieldData": {"standings_id": "12", "mlb_site_id": "117", "year": "2024", "league": "アメリカン・リーグ", "division": "西地区", "team": "ヒューストン・アストロズ", "wins": "88", "losses": "73", "winning_percentage": "", "runs_scored": "740", "runs_allowed": "649", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "12", "modId": "0"}, {"fieldData": {"standings_id": "8", "mlb_site_id": "116", "year": "2024", "league": "アメリカン・リーグ", "division": "中地区", "team": "デトロイト・タイガース", "wins": "86", "losses": "76", "winning_percentage": "", "runs_scored": "682", "runs_allowed": "642", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "8", "modId": "0"}, {"fieldData": {"standings_id": "9", "mlb_site_id": "118", "year": "2024", "league": "アメリカン・リーグ", "division": "中地区", "team": "カンザスシティ・ロイヤルズ", "wins": "86", "losses": "76", "winning_percentage": "", "runs_scored": "735", "runs_allowed": "644", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "9", "modId": "0"}, {"fieldData": {"standings_id": "14", "mlb_site_id": "136", "year": "2024", "league": "アメリカン・リーグ", "division": "西地区", "team": "シアトル・マリナーズ", "wins": "85", "losses": "77", "winning_percentage": "", "runs_scored": "676", "runs_allowed": "607", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "14", "modId": "0"}, {"fieldData": {"standings_id": "10", "mlb_site_id": "142", "year": "2024", "league": "アメリカン・リーグ", "division": "中地区", "team": "ミネソタ・ツインズ", "wins": "82", "losses": "80", "winning_percentage": "", "runs_scored": "742", "runs_allowed": "735", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "10", "modId": "0"}, {"fieldData": {"standings_id": "2", "mlb_site_id": "111", "year": "2024", "league": "アメリカン・リーグ", "division": "東地区", "team": "ボストン・レッドソックス", "wins": "81", "losses": "81", "winning_percentage": "", "runs_scored": "751", "runs_allowed": "747", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "2", "modId": "0"}, {"fieldData": {"standings_id": "4", "mlb_site_id": "139", "year": "2024", "league": "アメリカン・リーグ", "division": "東地区", "team": "タンパベイ・レイズ", "wins": "80", "losses": "82", "winning_percentage": "", "runs_scored": "604", "runs_allowed": "663", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "4", "modId": "0"}, {"fieldData": {"standings_id": "15", "mlb_site_id": "140", "year": "2024", "league": "アメリカン・リーグ", "division": "西地区", "team": "テキサス・レンジャーズ", "wins": "78", "losses": "84", "winning_percentage": "", "runs_scored": "683", "runs_allowed": "738", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "15", "modId": "0"}, {"fieldData": {"standings_id": "5", "mlb_site_id": "141", "year": "2024", "league": "アメリカン・リーグ", "division": "東地区", "team": "トロント・ブルージェイズ", "wins": "74", "losses": "88", "winning_percentage": "", "runs_scored": "671", "runs_allowed": "743", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "5", "modId": "0"}, {"fieldData": {"standings_id": "11", "mlb_site_id": "133", "year": "2024", "league": "アメリカン・リーグ", "division": "西地区", "team": "オークランド・アスレチックス", "wins": "69", "losses": "93", "winning_percentage": "", "runs_scored": "643", "runs_allowed": "764", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "11", "modId": "0"}, {"fieldData": {"standings_id": "13", "mlb_site_id": "108", "year": "2024", "league": "アメリカン・リーグ", "division": "西地区", "team": "ロサンゼルス・エンゼルス", "wins": "63", "losses": "99", "winning_percentage": "", "runs_scored": "635", "runs_allowed": "797", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "13", "modId": "0"}, {"fieldData": {"standings_id": "6", "mlb_site_id": "145", "year": "2024", "league": "アメリカン・リーグ", "division": "中地区", "team": "シカゴ・ホワイトソックス", "wins": "41", "losses": "121", "winning_percentage": "", "runs_scored": "507", "runs_allowed": "813", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "6", "modId": "0"}, {"fieldData": {"standings_id": "19", "mlb_site_id": "143", "year": "2024", "league": "ナショナル・リーグ", "division": "東地区", "team": "フィラデルフィア・フィリーズ", "wins": "95", "losses": "67", "winning_percentage": "", "runs_scored": "784", "runs_allowed": "671", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "19", "modId": "0"}, {"fieldData": {"standings_id": "23", "mlb_site_id": "158", "year": "2024", "league": "ナショナル・リーグ", "division": "中地区", "team": "ミルウォーキー・ブリュワーズ", "wins": "93", "losses": "69", "winning_percentage": "", "runs_scored": "777", "runs_allowed": "641", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "23", "modId": "0"}, {"fieldData": {"standings_id": "16", "mlb_site_id": "144", "year": "2024", "league": "ナショナル・リーグ", "division": "東地区", "team": "アトランタ・ブレーブス", "wins": "89", "losses": "73", "winning_percentage": "", "runs_scored": "704", "runs_allowed": "607", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "16", "modId": "0"}, {"fieldData": {"standings_id": "18", "mlb_site_id": "121", "year": "2024", "league": "ナショナル・リーグ", "division": "東地区", "team": "ニューヨーク・メッツ", "wins": "89", "losses": "73", "winning_percentage": "", "runs_scored": "768", "runs_allowed": "697", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "18", "modId": "0"}, {"fieldData": {"standings_id": "26", "mlb_site_id": "109", "year": "2024", "league": "ナショナル・リーグ", "division": "西地区", "team": "アリゾナ・ダイアモンドバックス", "wins": "89", "losses": "73", "winning_percentage": "", "runs_scored": "886", "runs_allowed": "788", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "26", "modId": "0"}, {"fieldData": {"standings_id": "21", "mlb_site_id": "112", "year": "2024", "league": "ナショナル・リーグ", "division": "中地区", "team": "シカゴ・カブス", "wins": "83", "losses": "79", "winning_percentage": "", "runs_scored": "736", "runs_allowed": "669", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "21", "modId": "0"}, {"fieldData": {"standings_id": "25", "mlb_site_id": "138", "year": "2024", "league": "ナショナル・リーグ", "division": "中地区", "team": "セントルイス・カージナルス", "wins": "83", "losses": "79", "winning_percentage": "", "runs_scored": "672", "runs_allowed": "719", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "25", "modId": "0"}, {"fieldData": {"standings_id": "22", "mlb_site_id": "113", "year": "2024", "league": "ナショナル・リーグ", "division": "中地区", "team": "シンシナティ・レッズ", "wins": "77", "losses": "85", "winning_percentage": "", "runs_scored": "699", "runs_allowed": "694", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "22", "modId": "0"}, {"fieldData": {"standings_id": "24", "mlb_site_id": "134", "year": "2024", "league": "ナショナル・リーグ", "division": "中地区", "team": "ピッツバーグ・パイレーツ", "wins": "76", "losses": "86", "winning_percentage": "", "runs_scored": "665", "runs_allowed": "739", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "24", "modId": "0"}, {"fieldData": {"standings_id": "20", "mlb_site_id": "120", "year": "2024", "league": "ナショナル・リーグ", "division": "東地区", "team": "ワシントン・ナショナルズ", "wins": "71", "losses": "91", "winning_percentage": "", "runs_scored": "660", "runs_allowed": "764", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "20", "modId": "0"}, {"fieldData": {"standings_id": "17", "mlb_site_id": "146", "year": "2024", "league": "ナショナル・リーグ", "division": "東地区", "team": "マイアミ・マーリンズ", "wins": "62", "losses": "100", "winning_percentage": "", "runs_scored": "637", "runs_allowed": "841", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "17", "modId": "0"}, {"fieldData": {"standings_id": "27", "mlb_site_id": "115", "year": "2024", "league": "ナショナル・リーグ", "division": "西地区", "team": "コロラド・ロッキーズ", "wins": "61", "losses": "101", "winning_percentage": "", "runs_scored": "682", "runs_allowed": "929", "pythagorean_expectation": "", "expected_win_loss": "", "winning_percentage_difference": ""}, "recordId": "27", "modId": "0"}]
JSON 配列形式になっていますね。これを json_decode()
することで、 PHP 配列に変換できます。
このデータを、セッション中持ち続ければいいわけです。しかし、繰り返しになりますが、これでは、常に、response-data
を常に持ち続けないといけないわけですから、迂遠な方法だと思います。
リクエスト自体を PHP ではなく、JavaScript にした方が、スッキリ書けると思います。
今回、積み残しているのは、
- submit ボタンを押した後の処理
- フッタリンクで
response-data
を渡す処理
以上になります。