健康的な食生活のために意識すべきポイント

getElementByIdとは

ChatGPT

getElementById は、JavaScriptのメソッドの1つで、HTMLドキュメント内の指定されたIDを持つ要素を取得するために使用されます。このメソッドは、 document オブジェクトから呼び出され、ID属性を持つ要素を特定し、その要素を返します。

例えば、次のようなHTMLコードがあった場合に、 getElementById メソッドを使用することができます。

phpCopy code<!DOCTYPE html>
<html>
  <head>
    <title>Example</title>
  </head>
  <body>
    <div id="myDiv">This is my div element.</div>
  </body>
</html>

この場合、 getElementById メソッドを使用して、IDが myDiv である <div> 要素を取得することができます。

javascriptCopy codevar element = document.getElementById("myDiv");

この element 変数には、IDが myDiv である要素が格納されます。この要素に対して、さまざまな操作を行うことができます。

var groupCount = extraTimesDiv.getElementsByClassName("time-input-group").length; 意味は?

ChatGPT

このJavaScriptのコードは、 extraTimesDiv 要素内にあるクラス名が time-input-group である要素の数を数え、その数を groupCount 変数に代入しています。

getElementsByClassName() メソッドは、指定されたクラス名を持つ要素のコレクションを取得します。この場合、 extraTimesDiv 要素内の time-input-group クラスを持つ要素を取得しています。getElementsByClassName はコレクションを返すため、.length を使用して、要素の数を数えることができます。そして、数が groupCount 変数に代入され、これ以降の処理で使用されることになります。

つまり、このコードは extraTimesDiv 要素内にある time-input-group クラスを持つ要素の数を groupCount 変数に格納するという意味です。

laravel $postに格納されているデータを確認する方法ってvardump('$_POST')じゃなかったっけ?

ChatGPT

Laravelで $post 変数に格納されているデータを確認する方法として、 var_dump($_POST) という方法もありますが、 $post 変数自体は通常存在しないため、 $_POST を直接参照する必要があります。

ただし、 var_dump($_POST) を使用する場合は、PHPの標準的な方法で、 $_POST 変数に格納されたHTTP POSTリクエストのデータを確認することができます。これはLaravelの特定の機能ではありません。

Laravelでは、フォームデータの受信や、APIからのリクエストデータの処理には、 Request オブジェクトを使用することが一般的です。たとえば、次のようにして、現在のリクエストの全ての入力データを取得することができます。

phpCopy codeuse Illuminate\Http\Request;

Route::post('/example', function(Request $request) {
    $inputData = $request->all();
    dd($inputData);
});

この場合、$inputDataには、現在のリクエストの全ての入力データが格納されます。そして、 dd 関数を使用して、これらのデータを表示することができます。

おかしいところある? @include('header') <div class="box"> <div class="body"> @if ($_SERVER['REQUEST_METHOD'] === 'POST') @php $set_count = count($_POST) / 2; $mealArray = array(); $exerciseArray = array(); $otherArray = array(); $sleepArray = array(); @endphp <h1>投稿完了</h1> <a>・食事内容</br></a> @for ($i = 1; $i <= $set_count; $i++) @php $mealTime=$_POST['meal-time' . $i] . ' ' ; $mealDetail=htmlspecialchars($_POST['meal-comment' . $i], ENT_QUOTES, 'UTF-8' ); $mealContents[$i]=$mealTime . $mealDetail . '<br/>' ; $mealArray[]=$mealContents[$i]; @endphp <a>{{ $mealContents[$i] }}</a> @endfor <a>・運動内容</br></a> @for ($i = 1; $i <= $set_count; $i++) @php $mealTime=$_POST['exercise-time' . $i] . ' ' ; $mealDetail=htmlspecialchars($_POST['exercise-comment' . $i], ENT_QUOTES, 'UTF-8' ); $mealContents[$i]=$mealTime . $mealDetail . '<br/>' ; $mealArray[]=$mealContents[$i]; @endphp <a>{{ $mealContents[$i] }}</a> @endfor <a>・その他</br></a> @for ($i = 1; $i <= $set_count; $i++) @php $mealTime=$_POST['other-time' . $i] . ' ' ; $mealDetail=htmlspecialchars($_POST['other-comment' . $i], ENT_QUOTES, 'UTF-8' ); $mealContents[$i]=$mealTime . $mealDetail . '<br/>' ; $mealArray[]=$mealContents[$i]; @endphp <a>{{ $mealContents[$i] }}</a> @endfor <a>・睡眠時間</br></a> @for ($i = 1; $i <= $set_count; $i++) @php $mealTime=$_POST['rising-time' . $i] . ' ' ; $mealTime=$_POST['retiring-time' . $i]; $mealContents[$i]=$mealTime . $mealDetail . '<br/>' ; $mealArray[]=$mealContents[$i]; @endphp <a>{{ $mealContents[$i] }}</a> @endfor @endif <div class="top"> <a href="./main.php">トップへ戻る</a> </div> </div> </div> @include('footer')

ChatGPT

いくつか修正が必要な箇所があります。

  1. @php@endphp の間に挟まれたPHPコード内で、 $mealContents を定義していないため、 $mealContents[$i] に値を代入することができません。そのため、 $mealArray には正しい値が格納されません。以下のように修正する必要があります。

phpCopy code@php
$set_count = count($_POST) / 2;
$mealArray = array();
$exerciseArray = array();
$otherArray = array();
$sleepArray = array();
$mealContents = array(); // 追加
@endphp
  1. 運動内容を格納する配列名が、 $mealArray になっているため、 $exerciseArray に修正する必要があります。以下のように修正してください。

phpCopy code@for ($i = 1; $i <= $set_count; $i++)
    @php
    $exerciseTime = $_POST['exercise-time' . $i] . ' ';
    $exerciseDetail = htmlspecialchars($_POST['exercise-comment' . $i], ENT_QUOTES, 'UTF-8' );
    $exerciseContents[$i] = $exerciseTime . $exerciseDetail . '<br/>';
    $exerciseArray[] = $exerciseContents[$i];
    @endphp
    <a>{{ $exerciseContents[$i] }}</a>
@endfor
  1. その他の内容を格納する配列名が、 $mealArray になっているため、 $otherArray に修正する必要があります。以下のように修正してください。

phpCopy code@for ($i = 1; $i <= $set_count; $i++)
    @php
    $otherTime = $_POST['other-time' . $i] . ' ';
    $otherDetail = htmlspecialchars($_POST['other-comment' . $i], ENT_QUOTES, 'UTF-8' );
    $otherContents[$i] = $otherTime . $otherDetail . '<br/>';
    $otherArray[] = $otherContents[$i];
    @endphp
    <a>{{ $otherContents[$i] }}</a>
@endfor
  1. 睡眠時間のコードで、 $mealDetail を代入している箇所がありますが、適切な変数名は $sleepDetail である必要があります。以下のように修正してください。

phpCopy code@for ($i = 1; $i <= $set_count; $i++)
    @php
    $risingTime = $_POST['rising-time' . $i] . ' ';
    $retiringTime = $_POST['retiring-time' . $i] . ' ';
    $sleepContents[$i] = $risingTime . $retiringTime . '<br/>';
    $sleepArray[] = $sleepContents[$i];
    @endphp
    <a>{{ $sleepContents[$i] }}</a>
@endfor

おかしいところある? <!DOCTYPE html> <html lang="ja"> <head> <title>ヘルマネ</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-..." crossorigin="anonymous"> <style> .box { margin-bottom: 20px; margin-right: 120px; margin-left: 120px; } .box h3 { margin-top: 120px; margin-bottom: 20px; } .body { background-color: #ffffff ; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3); padding: 20px; } .body p { color: black; font-size: 20px; font-weight: bold; } .top { margin-top: 20px; } .body h1 { text-align: center; font-size: 2rem; color: #388e3c ; margin-bottom: 30px; } @media screen and (max-width: 896px) { .box { margin-top: 160px; margin-bottom: 80px; margin-right: 0; margin-left: 0; } } </style> </head> <body> @include('header') <div class="box"> <div class="body"> @if ($_SERVER['REQUEST_METHOD'] === 'POST') @php $set_count = count($_POST) / 2; $mealArray = array(); $exerciseArray = array(); $otherArray = array(); $sleepArray = array(); @endphp <h1>投稿完了</h1> <a>・食事内容</br></a> @for ($i = 1; $i <= $set_count; $i++) @php $mealTime=$_POST['meal-time' . $i] . ' ' ; $mealDetail=htmlspecialchars($_POST['meal-comment' . $i], ENT_QUOTES, 'UTF-8' ); $mealContents[$i]=$mealTime . $mealDetail . '<br/>' ; $mealArray[]=$mealContents[$i]; @endphp <a>{{ $mealContents[$i] }}</a> @endfor <a>・運動内容</br></a> @for ($i = 1; $i <= $set_count; $i++) @php $exerciseTime=$_POST['exercise-time' . $i] . ' ' ; $exerciseDetail=htmlspecialchars($_POST['exercise-comment' . $i], ENT_QUOTES, 'UTF-8' ); $exerciseContents[$i]=$exerciseTime . $exerciseDetail . '<br/>' ; $exerciseArray[]=$exerciseContents[$i]; @endphp <a>{{ $exerciseContents[$i] }}</a> @endfor <a>・その他</br></a> @for ($i = 1; $i <= $set_count; $i++) @php $otherTime=$_POST['other-time' . $i] . ' ' ; $otherDetail=htmlspecialchars($_POST['other-comment' . $i], ENT_QUOTES, 'UTF-8' ); $otherContents[$i]=$otherTime . $otherDetail . '<br/>' ; $otherArray[]=$otherContents[$i]; @endphp <a>{{ $otherContents[$i] }}</a> @endfor <a>・睡眠時間</br></a> @php $risingTime=$_POST['rising-time']; $retiringTime=$_POST['retiring-time']; @endphp <a>{{ $risingTime }} ~ {{ $retiringTime }}</a> @endif <div class="top"> <a href="./main.php">トップへ戻る</a> </div> </div> </div> @include('footer') </body>

ChatGPT

見た限り、特に問題はなさそうです。ただし、@include('header')@include('footer')があるため、これらのファイルが存在しない場合にはエラーが発生する可能性があります。また、$_SERVER['REQUEST_METHOD']を使用しているため、POSTメソッドでのアクセスのみを想定していることに留意する必要があります。

$set_countの中身を見たいのだけど、どう記述したらいい?

ChatGPT

$set_countの中身を確認するために、以下のように記述することができます。

scssCopy codevar_dump($set_count);

または

bashCopy codeecho $set_count;

これらのコードを、@if ($_SERVER['REQUEST_METHOD'] === 'POST')の下に挿入することで、$set_countの中身を確認することができます。

どう修正したらいい? ①post.php <!DOCTYPE html> <html> <head> <title>ヘルマネ</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> @include('header') <style> form { width: 375px; margin: 0 auto; font-family: Arial, sans-serif; font-size: 16px; } #meal -contents { box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3); background-color: #ffffff ; padding: 20px; border-radius: 10px; } #exercise -contents { box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3); background-color: #ffffff ; padding: 20px; border-radius: 10px; } #other -contents { box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3); background-color: #ffffff ; padding: 20px; border-radius: 10px; } #sleep -contents { box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3); background-color: #ffffff ; padding: 20px; border-radius: 10px; } .time-input-group { padding-bottom: 10px; } label { display: inline-block; text-align: right; margin-right: 10px; vertical-align: top; } select, textarea { width: 280px; padding: 5px; border: 1px solid #ccc ; } input[type="submit"] { font-size: 16px; background-color: #428bca ; color: #fff ; border: none; border-radius: 5px; padding: 5px 30px; display: block; margin-top: 0; margin-bottom: 40px; margin-right: auto; margin-left: auto; } input[type="submit"] { margin-top: 20px; } input[type="submit"]:hover { background-color: #3071a9 ; } </style> <script> // +ボタンがクリックされたときに実行する関数 function addTimeInputGroupMeal() { var extraTimesDiv = document.getElementById("meal-contents"); var groupCount = extraTimesDiv.getElementsByClassName("time-input-group").length; if (groupCount < 8) { var newGroup = document.createElement("div"); newGroup.className = "time-input-group"; var newLabelTime = document.createElement("label"); newLabelTime.htmlFor = "meal-time" + (groupCount + 1); newLabelTime.innerHTML = "食事時間"; var newInput = document.createElement("input"); newInput.type = "time"; newInput.name = "meal-time" + (groupCount + 1); newInput.required = true; var newLabelComment = document.createElement("label"); newLabelComment.htmlFor = "meal-comment" + (groupCount + 1); newLabelComment.innerHTML = "内容"; var newTextArea = document.createElement("textarea"); newTextArea.name = "meal-comment" + (groupCount + 1); newTextArea.placeholder = "納豆、チキンステーキ、味噌汁etc..."; newTextArea.required = true; newTextArea.maxlength = 50; newGroup.appendChild(newLabelTime); newGroup.appendChild(newInput); newGroup.appendChild(document.createElement("br")); newGroup.appendChild(document.createElement("br")); newGroup.appendChild(newLabelComment); newGroup.appendChild(newTextArea); extraTimesDiv.appendChild(newGroup); } } function addTimeInputGroupExercise() { var extraTimesDiv = document.getElementById("exercise-contents"); var groupCount = extraTimesDiv.getElementsByClassName("time-input-group").length; if (groupCount < 8) { var newGroup = document.createElement("div"); newGroup.className = "time-input-group"; var newLabelTime = document.createElement("label"); newLabelTime.htmlFor = "exercise-time" + (groupCount + 1); newLabelTime.innerHTML = "運動時間"; var newInput = document.createElement("input"); newInput.type = "time"; newInput.name = "exercise-time" + (groupCount + 1); newInput.required = true; var newLabelComment = document.createElement("label"); newLabelComment.htmlFor = "exercise-comment" + (groupCount + 1); newLabelComment.innerHTML = "内容"; var newTextArea = document.createElement("textarea"); newTextArea.name = "meal-comment" + (groupCount + 1); newTextArea.placeholder = "腕立て伏せ30回、ジョギング20分etc..."; newTextArea.required = true; newTextArea.maxlength = 50; newGroup.appendChild(newLabelTime); newGroup.appendChild(newInput); newGroup.appendChild(document.createElement("br")); newGroup.appendChild(document.createElement("br")); newGroup.appendChild(newLabelComment); newGroup.appendChild(newTextArea); extraTimesDiv.appendChild(newGroup); } } function addTimeInputGroupOther() { var extraTimesDiv = document.getElementById("other-contents"); var groupCount = extraTimesDiv.getElementsByClassName("time-input-group").length; if (groupCount < 8) { var newGroup = document.createElement("div"); newGroup.className = "time-input-group"; var newLabelTime = document.createElement("label"); newLabelTime.htmlFor = "other-time" + (groupCount + 1); newLabelTime.innerHTML = "その他"; var newInput = document.createElement("input"); newInput.type = "time"; newInput.name = "other-time" + (groupCount + 1); newInput.required = true; var newLabelComment = document.createElement("label"); newLabelComment.htmlFor = "other-comment" + (groupCount + 1); newLabelComment.innerHTML = "内容"; var newTextArea = document.createElement("textarea"); newTextArea.name = "meal-comment" + (groupCount + 1); newTextArea.placeholder = "瞑想、サウナ、サプリメントetc..."; newTextArea.required = true; newTextArea.maxlength = 50; newGroup.appendChild(newLabelTime); newGroup.appendChild(newInput); newGroup.appendChild(document.createElement("br")); newGroup.appendChild(document.createElement("br")); newGroup.appendChild(newLabelComment); newGroup.appendChild(newTextArea); extraTimesDiv.appendChild(newGroup); } } </script> </head> <body> <form method="POST" action="./post_comp.php" onsubmit="return validateTextArea()"> @csrf <!-- 食事内容 --> <div id="meal-contents"> <label for="meal-time1">食事時間</label> <input type="time" name="meal-time1" required><br><br> <div class="time-input-group"> <label for="meal-comment1">内容</label> <textarea name="meal-comment1" placeholder="納豆、チキンステーキ、味噌汁etc..." required maxlength="50"></textarea> </div> </div> <button type="button" onclick="addTimeInputGroupMeal()">食事を追加+</button></br></br> <!-- 運動内容 --> <div id="exercise-contents"> <label for="exercise-time1">運動時間</label> <input type="time" name="exercise-time1" required><br><br> <div class="time-input-group"> <label for="exercise-comment1">内容</label> <textarea name="exercise-comment1" placeholder="腕立て伏せ30回、ジョギング20分etc..." required maxlength="50"></textarea> </div> </div> <button type="button" onclick="addTimeInputGroupExercise()">運動を追加+</button></br></br> <!-- その他 --> <div id="other-contents"> <label for="other-time1">その他</label> <input type="time" name="other-time1" required><br><br> <div class="time-input-group"> <label for="other-comment1">内容</label> <textarea name="other-comment1" placeholder="瞑想、サウナ、サプリメントetc..." required maxlength="50"></textarea> </div> </div> <button type="button" onclick="addTimeInputGroupOther()">その他を追加+</button></br></br> <!-- 睡眠時間 --> <div id="sleep-contents"> <label for="sleep-time">睡眠時間</label> <input type="time" name="rising-time" required> ~ <input type="time" name="retiring-time" required> </div> <input type="submit" value="送信"> </form> @include('footer') </body> </html> ②post_comp.php <!DOCTYPE html> <html lang="ja"> <head> <title>ヘルマネ</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-..." crossorigin="anonymous"> <style> .box { margin-bottom: 20px; margin-right: 120px; margin-left: 120px; } .box h3 { margin-top: 120px; margin-bottom: 20px; } .body { background-color: #ffffff ; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3); padding: 20px; } .body p { color: black; font-size: 20px; font-weight: bold; } .top { margin-top: 20px; } .body h1 { text-align: center; font-size: 2rem; color: #388e3c ; margin-bottom: 30px; } @media screen and (max-width: 896px) { .box { margin-top: 160px; margin-bottom: 80px; margin-right: 0; margin-left: 0; } } </style> </head> <body> @include('header') <div class="box"> <div class="body"> <h1>投稿完了</h1> @if ($_SERVER['REQUEST_METHOD'] === 'POST') @php $set_count = count($_POST) / 2; $mealArray = array(); $exerciseArray = array(); $otherArray = array(); var_dump($_POST); exit; @endphp <a>・食事内容</br></a> @for ($i = 1; $i <= $set_count; $i++) @php $mealTime=$_POST['meal-time' . $i] . ' ' ; $mealDetail=htmlspecialchars($_POST['meal-comment' . $i], ENT_QUOTES, 'UTF-8' ); $mealContents[$i]=$mealTime . $mealDetail . '<br/>' ; $mealArray[]=$mealContents[$i]; @endphp <a>{{ $mealContents[$i] }}</a> @endfor <a>・運動内容</br></a> @for ($i = 1; $i <= $set_count; $i++) @php $exerciseTime=$_POST['exercise-time' . $i] . ' ' ; $exerciseDetail=htmlspecialchars($_POST['exercise-comment' . $i], ENT_QUOTES, 'UTF-8' ); $exerciseContents[$i]=$exerciseTime . $exerciseDetail . '<br/>' ; $exerciseArray[]=$exerciseContents[$i]; @endphp <a>{{ $exerciseContents[$i] }}</a> @endfor <a>・その他</br></a> @for ($i = 1; $i <= $set_count; $i++) @php $otherTime=$_POST['other-time' . $i] . ' ' ; $otherDetail=htmlspecialchars($_POST['other-comment' . $i], ENT_QUOTES, 'UTF-8' ); $otherContents[$i]=$otherTime . $otherDetail . '<br/>' ; $otherArray[]=$otherContents[$i]; @endphp <a>{{ $otherContents[$i] }}</a> @endfor <a>・睡眠時間</br></a> @php $risingTime=$_POST['rising-time']; $retiringTime=$_POST['retiring-time']; @endphp <a>{{ $risingTime }} ~ {{ $retiringTime }}</a> @endif <div class="top"> <a href="./main.php">トップへ戻る</a> </div> </div> </div> @include('footer') </body>

ChatGPT

修正点:

  • 関数addTimeInputGroupExercise()内のnewTextAreaのname属性を"meal-comment" + (groupCount + 1)から"exercise-comment" + (groupCount + 1)に変更する必要があります。

修正後のコード:

javascriptCopy codefunction addTimeInputGroupExercise() {
  var extraTimesDiv = document.getElementById("exercise-contents");
  var groupCount = extraTimesDiv.getElementsByClassName("time-input-group").length;

  if (groupCount < 8) {
    var newGroup = document.createElement("div");
    newGroup.className = "time-input-group";

    var newLabelTime = document.createElement("label");
    newLabelTime.htmlFor = "exercise-time" + (groupCount + 1);
    newLabelTime.innerHTML = "運動時間";

    var newInput = document.createElement("input");
    newInput.type = "time";
    newInput.name = "exercise-time" + (groupCount + 1);
    newInput.required = true;

    var newLabelComment = document.createElement("label");
    newLabelComment.htmlFor = "exercise-comment" + (groupCount + 1);
    newLabelComment.innerHTML = "内容";

    var newTextArea = document.createElement("textarea");
    newTextArea.name = "exercise-comment" + (groupCount + 1);
    newTextArea.placeholder = "腕立て伏せ30回、...";
    newTextArea.required = true;
    newTextArea.maxlength = 50;

    newGroup.appendChild(newLabelTime);
    newGroup.appendChild(newInput);
    newGroup.appendChild(document.createElement("br"));
    newGroup.appendChild(document.createElement("br"));
    newGroup.appendChild(newLabelComment);
    newGroup.appendChild(newTextArea);

    extraTimesDiv.appendChild(newGroup);
  }
}

post_comp.phpの$set_countのところ、どうやったら追加した分のcontentの内容を表示できるだろう

ChatGPT

$set_countは、投稿フォームに入力された内容をデータベースに保存するための処理の中で、追加されたデータの数を数えるために使用されます。そのため、$set_count自体は、投稿された内容を表示するためには使用できません。

投稿された内容を表示するには、データベースからデータを取得して表示する必要があります。具体的には、以下の手順を実行する必要があります。

  1. データベースに接続する。

  2. データベースから投稿されたデータを取得する。

  3. 取得したデータをHTMLのテーブル形式で表示する。

以下は、$set_countの直後にデータベースから投稿された内容を取得して表示するサンプルコードです。

phpCopy code// データベースに接続する
$db = new mysqli('localhost', 'ユーザー名', 'パスワード', 'データベース名');

// エラーチェック
if ($db->connect_error) {
    echo $db->connect_error;
    exit();
}

// データを取得する
$result = $db->query("SELECT * FROM posts");

// データを表示する
echo '<table>';
while ($row = $result->fetch_assoc()) {
    echo '<tr>';
    echo '<td>' . $row['title'] . '</td>';
    echo '<td>' . $row['content'] . '</td>';
    echo '</tr>';
}
echo '</table>';

// データベースを切断する
$db->close();

このコードでは、postsというテーブルから全てのデータを取得し、HTMLのテーブル形式で表示しています。実際のコードでは、表示する項目や表示方法を変更することができます。

例えばse_countが8あります。ただ、meal-contentは2つしかありません。それでエラーが表示されます。そこをどうやったら修正できるだろう

ChatGPT

もしset_countmeal-contentの数を超える場合にエラーが発生する場合、次のように修正できます。

phpCopy code$meal_contents = array("food1", "food2");
$set_count = 8;

// meal-contentの数とset_countを比較し、小さい方の数でループを回す
for ($i = 0; $i < min(count($meal_contents), $set_count); $i++) {
  echo $meal_contents[$i];
}

この修正により、set_countmeal-contentの数よりも大きい場合でもエラーが発生しなくなります。ただし、ループで表示されるのはmeal-contentの数に制限されます。もしset_countに基づいて追加のcontentを表示したい場合は、何らかの方法で新しいcontentを作成する必要があります。

post.phpからn個のmeal-contentが送られてきます。それをmeal-contet1, mealcontent2...といった形で送信されます。同じようにother-content1..., exercise-content1...と送られます。それらを表示するためにはどうしたらいいか

ChatGPT

post.phpから送信されてくるn個のmeal-content、other-content、exercise-contentなどは、それぞれの内容を示す名前の後に数値が付与された形で送られてきます。例えば、meal-content1、meal-content2、other-content1、exercise-content1などです。

そのため、post_comp.phpでそれぞれのコンテンツを表示するには、forループを使用して、n個のコンテンツを1つずつ表示する必要があります。以下は、meal-contentとother-contentを表示する例です。

phpCopy code<?php
$set_count = $_POST['set_count'];

for($i = 1; $i <= $set_count; $i++) {
    if(isset($_POST['meal-content'.$i])) {
        $meal_content = $_POST['meal-content'.$i];
        echo 'Meal '.$i.': '.$meal_content.'<br>';
    }

    if(isset($_POST['other-content'.$i])) {
        $other_content = $_POST['other-content'.$i];
        echo 'Other '.$i.': '.$other_content.'<br>';
    }
}
?>

この例では、$set_countの回数だけループを回し、meal-contentとother-contentの各セットを表示しています。もしexercise-contentがあれば、同様にループを回し、exercise-contentを表示することもできます。

noteに掲載します。タイトル考えて!

まじか

ChatGPT

ご不明な点がある場合は何でもお聞きください。お力になれることがあれば喜んでお答えいたします。

この記事が気に入ったらサポートをしてみませんか?