実現したいこと
タイトルの通り、「リンクのパラメータに日付の情報を持たせたい」
つまり、
<a href="menu_delete.php?action=delete&id={$row.id}&date={$date->format('Ymd')} class="complate" name="delete">完了</a>
このようなリンクのdate={$date->format('Ymd')}
に日付の情報を持たせたいです。
現在の状況
現在筋トレメニューを日付別に登録して管理するwebアプリケーションを作成していて、今はメニュー削除処理を作成しているのですが、削除処理ファイルに遷移するとカレンダーファイルから持ってきた日付のデータが消えてしまいます。
これを直して削除処理を実行した時に日付データを引き回したいです。
該当のソースコード
php
//calendar.php(カレンダー機能のphpファイルです) <?php require_once '../libs/init.php'; //今日の日付を持ったオブジェクト $current_date = new DateTime(); //日曜日の始まりにする処理 if(0 < $current_date->format('w')) { $current_date->modify(sprintf('- %d days', $current_date->format('w'))); } $last_date = (clone $current_date)->modify('+ 7 days'); $dates = []; while($current_date < $last_date) { $dates[] = clone $current_date; $current_date->modify('+ 1 day'); } //smarty接続ファイル $smarty = getSmarty(); $smarty->assign('dates', $dates); $smarty->display('calendar.tpl');
{* calendare.tpl(カレンダー機能のsmartyファイルです) *} <!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>PHPカレンダー</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <link href="https://fonts.googleapis.com/css?family=Noto+Sans+JP" rel="stylesheet"> </head> <body> {foreach $dates as $date} //日付データを持たせたパラメータのリンク <a href="menupost.php?date={$date->format('Ymd')}">{$date->format('Y/m/d')}</a><br> {/foreach} </body> </html>
//menupost.php(メニュー追加機能のphpファイルです) <?php require_once '../libs/init.php'; if(!isset($_SESSION['user_id'])) { header("Location: login.php"); } $date = $_GET['date']; try { $pdo = db_connect(); //メニュー情報取得 $menus = fetchAllMenus($pdo, $_SESSION['user_id'], $date); } catch (PDOException $Exception) { print "error:". $Exception->getMessage(); } $smarty = getSmarty(); $smarty->assign('menus', $menus); $smarty->assign('date', $date); $smarty->display('menupost.tpl');
{* メニュー追加機能の表示ファイル(こちらは必要な部分だけにして抜粋してます) *} <table class="table table-hover mt-5"> <thead> <tr> <th scope="col"></th> <th scope="col">メニュー</th> <th scope="col">回数</th> <th scope="col">セット数</th> <th></th> </tr> </thead> <p>{$date}</p> {foreach from=$menus item=$row} <tbody> <tr> <td></td> <td>{$row.menu}</td> <td>{$row.num}</td> <td>{$row.setnum}</td> {* ここで問題が起きてます(date) *} <td><a href="menu_delete.php?action=delete&id={$row.id}&date={$date->format('Ymd')} class="complate" name="delete">完了</a></td> </tr> </tbody> {/foreach} </table> <form action="insert.php" id="contact" method="post"> <div class="container"> <div class="head"> <h2>メニューを追加しましょう!</h2> </div> <input type="hidden" name="date" value="{$date}"></input> <input type="text" name="menu" placeholder="トレーニングメニュー"/><br /> <input type="number" name="num" placeholder="回数or秒数" /> <input type="number" name="setnum" placeholder="セット数"/><br /> <button id="submit" type="submit">追加</button> </div> </form>
php
<?php //menu_delete.php require_once '../libs/init.php'; $pdo = db_connect(); try { $pdo->beginTransaction(); $id = $_REQUEST['id']; $date = $_REQUEST['date']; var_dump($date); $delete = deleteTrainingMenu($pdo, $id, $date); } catch(PDOException $Exception) { $pdo->rollBack(); print 'エラー:'. $Exception->getMessage(); } if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'delete' && $_REQUEST['id'] > 0) { header("Location:menupost.php"); }
ちなみにdeleteTrainingMenu()はこちらです、
php
function deleteTrainingMenu($pdo, $id, $date) { $sql = "DELETE FROM `trainingmenu` WHERE id = :id AND date = :date"; $stmt = $pdo->prepare($sql); $stmt->bindValue(':id', $id, PDO::PARAM_INT); $stmt->bindValue(':date', $date, PDO::PARAM_INT); $stmt->execute(); }
画面遷移
calendar.php(ここでカレンダーから日付を選択します。ちなみにここで日付データを持たせています)->menupost.php(ここでメニューを追加したり削除用のページに遷移したりします)->menu_delete.php(ここで削除機能を実行します。ですが日付のデータを持たせることができません)
上手くいかない原因の仮説
new Dateなどで現在時刻を取得できていないから?
パラメータがただの文字列としか扱われていないかも?
以上です。
コードが長くなってしまいましたが、お時間のある方はご回答よろしくお願いします。
まだ回答がついていません
会員登録して回答してみよう