質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

mysqli

MySQLiはPHP5より導入されているデータベース用のドライバです。MySQL 4.1.3以降の新しい機能の利点をまとめています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

2362閲覧

オブジェクト指向について

yutaishikawa_

総合スコア58

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

mysqli

MySQLiはPHP5より導入されているデータベース用のドライバです。MySQL 4.1.3以降の新しい機能の利点をまとめています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2015/07/22 00:50

編集2015/07/22 13:21

いつもお世話になります。
今回、再びクラス化以前の問題でつまづいてしまいました。
仕事を進めるにあたって最後の課題になるので、お力添えいただければ嬉しいです。

現在、タスク管理カレンダーを制作しています。
カレンダーの機能を説明すると、
・無限カレンダー
・MySQLと接続してデータベース上でタスク管理ができる
・登録したタスク一覧ページの作成(taskListDay.php)
・日にちをクリックするとタスクがでる。
・タスクを作成・削除・更新する機能
以上をオブジェクト指向で、コーディングします。

やりたいこと
・無限カレンダー(index.php)の日付(タスクがDBにインサートされている日)をクリックすると、$_GETでDB上にある、その日のタスクををリンク先(taskListDay.php)のURLに?=の状態で送ってあげる。
・タスクをインサートしている日をクリックする。→削除・更新・変更ボタンがついているページ(edition.php)へ遷移。その際インサートされている日にちの情報をもとに、削除・更新・変更ができる状態にする。
・タスクの削除・更新・変更。
以上を、現在の自力で書いてみたコードでは遂行できなかったので、アドバイス等いただけると嬉しいです。

コードはトップページのindex.php、タスク表示ページのtaskListDay.php、タスク編集ページのedition.php、全動作をカプセル化しているcalender.phpを記載いたします。

少し長い質問となりましたが、私にとって初心者最後の難関です。
何卒宜しくお願いいたします。

index

PHP

1<?php 2require_once('calender.php'); 3 4$cal = new Calender(); 5$cal->setTimeStamp(); 6$cal->query(); 7?> 8<!DOCTYPE html> 9<html> 10<title>カレンダー</title> 11<meta charset="utf-8"> 12<table border="1"> 13 <tr> 14 <th><a href="?ym=<?php echo $cal->h($cal->prev()); ?>">&laquo;</a></th> 15 <th colspan="5"><?php echo $cal->h($cal->yearManth()); ?></th> 16 <th><a href="?ym=<?php echo $cal->h($cal->next()); ?>">&raquo;</a></th> 17 </tr> 18 <thead> 19 <tr> 20 <th></th> 21 <th></th> 22 <th></th> 23 <th></th> 24 <th></th> 25 <th></th> 26 <th></th> 27 </tr> 28 </thead> 29 <tbody> 30 <?php 31 foreach ($cal->getWeeks() as $week) { 32 echo $week; 33 } 34 ?> 35 </tbody> 36</table> 37<form action="edit_do/" method="POST"> 38<p>タイトル : <input type="text" name="title" size="20"></p> 39<p>日時 : <input type="date" name="task_date" min="2015-07-01"></p> 40<p>場所 : <input type="text" name="place" size="20"></p> 41<p>メモ : <textarea type="text" name="memo" rows="4" cols="40"></textarea></p> 42<input type="submit" value="追加する"><input type="reset" value="リセット"> 43</form> 44</html>

taskListDay

PHP

1<?php 2require_once('calender.php'); 3 4$cal = new Calender(); 5$cal->Create(); 6?> 7<!DOCTYPE html> 8<html lang="ja"> 9<meta charset="utf-8"> 10<title>Today | タスク一覧</title> 11<h1>タスク一覧</h1> 12<p>タイトルをクリックすると編集できます。</p> 13<table border="1"> 14 <thead> 15 <tr> 16 <th>タイトル</th> 17 <th>日時</th> 18 <th>場所</th> 19 <th>メモ</th> 20 </tr> 21 </thead> 22 <tbody> 23 <tr> 24 <td><?php echo $cal->task(); ?></td> 25 </tr> 26 </tbody> 27</table> 28<form action="index.php"> 29 <input type="submit" value="トップページへ戻る"> 30</form> 31</html>

edition

PHP

1<?php 2require_once('calender.php'); 3 4$cal = new Calender(); 5$cal->edit(); 6?> 7<!DOCTYPE html> 8<html lang="ja"> 9<meta charset="utf-8"> 10<title>Edit | イベントを編集する</title> 11<h1>イベントを編集する</h1> 12<table border="1"> 13 <thead> 14 <tr> 15 <th>編集するイベントを選択</th> 16 </tr> 17 </thead> 18</table> 19<form action="edittinal_do.php" method="POST"> 20タスク内容:<br /> 21<textarea name="comment" cols="30" rows="5"> 22 <?php echo $cal->h($cal->task); ?></textarea><br /> 23</textarea><br /> 24<?php echo "<input type=hidden name=id value=\"".$_GET['id']."\">"; ?> 25<input type="submit" value="イベントを更新する"> 26</form> 27<form action="delete.php"> 28 <input type="submit" value="イベントを削除する"> 29</form> 30</html>

calender

PHP

1<?php 2// dbconnect.php 3class dbconnect { 4 //mysqli の インスタンス化 、接続 5 public function ConnectDb() { 6 $this->mysqli = new mysqli('localhost', 'server', ''pass', 'db'); 7 if ($this->mysqli->connect_error){ 8 die('Connect Error (' . $this->mysqli->connect_errno . ') '. $this->mysqli->connect_error); 9 exit; 10 } 11 } 12} 13 14class Calender extends dbconnect { 15 protected $weeks = array(); 16 protected $timeStamp; 17 // mysqli の メソッド 18 public function __construct(){ 19 $dbconnect = new dbconnect(); 20 if ($this->timeStamp === false) { 21 $this->timeStamp = time(); 22 } 23 // DB接続 24 parent::ConnectDb(); 25 } 26 // 現在の日時取得 (Y-m) 27 public function setTimeStamp(){ 28 $ym = isset($_GET['ym']) ? $_GET['ym'] : date("Y-m"); 29 $this->timeStamp = strtotime($ym . "-01"); 30 if ($this->timeStamp === false){ 31 $this->timeStamp = time(); 32 } 33 } 34 // 現在の日時取得 (Y-m-d) 35 public function setTimeStamp_day(){ 36 $ymd = isset($_GET['ymd']) ? $_GET['ymd'] : date("Y-m-d"); 37 $this->timeStamp = strtotime($ymd); 38 if ($this->timeStamp === false){ 39 $this->timeStamp = time(); 40 } 41 } 42 public function query() { 43 // SQLで今月を取得する処理。先月の最終日と翌月の初日を不等号で期間を指定し、ANDで結ぶ。 44 $lastMonth = date("Y-m-d",strtotime("last day of - 1 month",$this->timeStamp)); 45 $nextMonth = date("Y-m-d",strtotime("first day of + 1 month",$this->timeStamp)); 46 $userData = array(); 47 // PHP->MySQLtable 48 $query = $this->mysqli->query("SELECT * FROM tasks WHERE task_date > '$lastMonth' AND task_date <'$nextMonth'"); 49 if (!$query) { 50 die('クエリーが失敗しました。'.mysql_error()); 51 } 52 // 連想配列で$rowを取得する 53 while($row = $query->fetch_assoc()) { 54 $userData[date('j',strtotime($row['task_date']))] = array( 55 // 各メンバ取得 56 'id' => $row['id'], 57 'title' => $row['title'], 58 'task_date' => $row['task_date'], 59 'place' => $row['place'], 60 'memo' => $row['memo'], 61 ); 62 } 63 // 最終日取得 64 $lastday = date("t",$this->timeStamp); 65 // 1日の曜日番号の数だけ空白を出力(7月だと3つ) 66 $youbi = date("w",mktime(0,0,0,date("m",$this->timeStamp),1,date("Y",$this->timeStamp))); 67 // $weekを初期化 68 $week = ""; 69 // 結合代入演算子を使用して、<td>を取得。一週間(0~6の<td>)が完成。 70 $week .= str_repeat('<td></td>', $youbi); 71 // 上記の定義で1日から31日までfor文で取得 72 for ($day = 1; $day <= $lastday; $day++ ,$youbi++) { 73 // もし$userData[日付]の変数が 空の場合 74 if (isset($userData[$day])) { 75 // リンクをはる 76 $week .= '<td><a href="taskListDay.php">'.$day.'</a></td>'; 77 }else{ 78 // リンクをはらない 79 $week .= '<td class="youbi_'.($youbi % 7).'">'.$day.'</td>'; 80 } 81 // $youbiを7で割った余り=6。(土曜日) 82 if ($youbi % 7 == 6 OR $day == $lastday) { 83 // もし 1 = $lastdayだったとき、<tb>に$youbiを7で割ったものを6で引く 84 if($day == $lastday) { 85 $week .= str_repeat('<td></td>', 6 - ($youbi % 7)); 86 } 87 // 結果、<tr>は$week 88 $this->weeks[]='<tr>' .$week .'</tr>'; 89 // $weekを初期化する 90 $week = ''; 91 } 92 // week""を初期化し続ける処理によってカレンダー型を形成。 93 } 94 } 95 96 public function Create(){ 97 // タスクの配列を準備 98 $this->task = ""; 99 $taskData = array(); 100 // $pass = file_get_contents('edition.php'); 101 // PHP->MySQLtable 102 // タスクのパラメータを送れるようにする。 103 $view = $this->mysqli->query("SELECT * FROM tasks"); 104 // エラー処理 105 if (!$view) { 106 die('クエリーが失敗しました。'.mysql_error()); 107 } 108 while ($this->row = $view->fetch_assoc()) { 109 // 各メンバ取得 110 $id = $this->row['id']; 111 $title = $this->row['title']; 112 $task_date = $this->row['task_date']; 113 $place = $this->row['place']; 114 $memo = $this->row['memo']; 115 $this->task .= "<tr><td><a href='edition.php'>${title}</a></td><td>${task_date}</td><td>${place}</td><td>${memo}</td></tr>"; 116 } 117 118 } 119 // html記述用 120 public function h($s){ 121 return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); 122 } 123 // リストを取得 124 public function mysqli_result() { 125 return $this->mysqli; 126 } 127 // タスクを取得 128 public function getTasks() { 129 $sql = "SELECT * FROM tasks"; 130 if ($ymd) { 131 $ymd = mysql_real_escape_string($ymd); 132 $sql.= " WHERE FROM_UNIXTIME(task_date, '%Y-%m-%d') = '$ymd' "; 133 } 134 $query = $this->mysqli->query($sql); 135 } 136 // 一週を取得 137 public function getWeeks() { 138 return $this->weeks; 139 } 140 // 前月を取得 141 public function prev() { 142 return date("Y-m", mktime(0, 0, 0, date("m", $this->timeStamp)-1, 1, date("Y", $this->timeStamp))); 143 } 144 // 翌月を取得 145 public function next() { 146 return date("Y-m", mktime(0, 0, 0, date("m", $this->timeStamp)+1, 1, date("Y", $this->timeStamp))); 147 } 148 // 現在の日時を取得 (Y-m) 149 public function yearManth() { 150 return date("Y-m" ,$this->timeStamp); 151 } 152 public function task() { 153 return $this->task; 154 } 155 public function getData(){ 156 return $this->userData; 157 } 158 public function edit() { 159 if (!isset($_GET['id'])) { 160 $errors[] = 'errors'; 161 } 162 $st = $this->mysqli->prepare("SELECT * FROM tasks where id = $id"); 163 $st->execute(array($_GET['id'])); 164 $this->row = $st->fetch(); 165 $this->task = $this->row['id']; 166 } 167 168 public function edit_do(){ 169 if (isset($_POST['id'])) { 170 !$errors[] = 'errors'; 171 } 172 $id = $_POST['id']; 173 $stmt = $this->mysqli->prepare("UPDATE tasks set memo = ? where id = '$id';"); 174 $stmt->execute(array($_POST['memo'])); 175 } 176} 177?>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

具体的に何が動かない書いてあった方が回答しやすいです。
ひとまず、

lang

1 $this->mysqli = new mysqli('localhost', 'server', ''pass', 'db');

は、

lang

1 $this->mysqli = new mysqli('localhost', 'server', 'pass', 'db');

ではないかと思います。

投稿2015/07/23 00:00

eripong

総合スコア1546

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yutaishikawa_

2015/07/30 01:37

ご指摘ありがとうございました。 細かく具体化することで、わかりやすいご回答をいただくことができました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問