質問編集履歴

2

公開のご指摘をいただいたので

2015/07/28 00:46

投稿

yutaishikawa_
yutaishikawa_

スコア58

test CHANGED
@@ -1 +1 @@
1
- test
1
+ PHPのカプセル化
test CHANGED
@@ -1 +1,369 @@
1
- のページテストですこのページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。
1
+ んにちは。
2
+
3
+ いよいよクラス化の技術を学び始めました。
4
+
5
+ 今回、前回まで書いてきたコードをカプセル化し、すべての処理を1ファイルずつにまとめました。
6
+
7
+ ですが、なかなかキレイにまとまってはくれず、さっそく不具合が発生。
8
+
9
+ mysqliをメッソド化?できずに、$queryや$taskをvar_dumpしてみても実行結果が0であったり、NULLで返ってくるようになりました。
10
+
11
+ これは、正しく接続できているのか・・・と見直したところ、参考にしている文献を見る限りイマイチピンとくることがありませんでした。
12
+
13
+ こうすればいいよ、というアドバイスがあれば是非お願いいたします。
14
+
15
+ よろしくお願いいたします。
16
+
17
+ index
18
+
19
+ ```PHP
20
+
21
+ <?php
22
+
23
+ require_once('calender.php');
24
+
25
+ $cal = new Calender();
26
+
27
+ $cal->setTimeStamp();
28
+
29
+ $cal->Create();
30
+
31
+ $cal->query();
32
+
33
+ ?>
34
+
35
+ <!DOCTYPE html>
36
+
37
+ <html>
38
+
39
+ <title>カレンダー</title>
40
+
41
+ <meta charset="utf-8">
42
+
43
+ <table border="1">
44
+
45
+ <tr>
46
+
47
+ <th><a href="?ym=<?php echo $cal->h($cal->prev()); ?>">&laquo;</a></th>
48
+
49
+ <th colspan="5"><?php echo $cal->h($cal->yearManth()); ?></th>
50
+
51
+ <th><a href="?ym=<?php echo $cal->h($cal->next()); ?>">&raquo;</a></th>
52
+
53
+ </tr>
54
+
55
+ <thead>
56
+
57
+ <tr>
58
+
59
+ <th>日</th>
60
+
61
+ <th>月</th>
62
+
63
+ <th>火</th>
64
+
65
+ <th>水</th>
66
+
67
+ <th>木</th>
68
+
69
+ <th>金</th>
70
+
71
+ <th>土</th>
72
+
73
+ </tr>
74
+
75
+ </thead>
76
+
77
+ <tbody>
78
+
79
+ <?php
80
+
81
+ foreach ($cal->getWeeks() as $week) {
82
+
83
+ echo $week;
84
+
85
+ }
86
+
87
+ ?>
88
+
89
+ </tbody>
90
+
91
+ </table>
92
+
93
+ <form action="edit_do.php" method="POST">
94
+
95
+ <p>タイトル : <input type="text" name="title" size="20"></p>
96
+
97
+ <p>日時 : <input type="date" name="task_date" min="2015-07-01"></p>
98
+
99
+ <p>場所 : <input type="text" name="place" size="20"></p>
100
+
101
+ <p>メモ : <textarea type="text" name="memo" rows="4" cols="40"></textarea></p>
102
+
103
+ <input type="submit" value="追加する"><input type="reset" value="リセット">
104
+
105
+ </form>
106
+
107
+ </html>
108
+
109
+ ```
110
+
111
+ class
112
+
113
+ ```PHP
114
+
115
+ <?php
116
+
117
+ // dbconnect.php
118
+
119
+ class dbconnect {
120
+
121
+ //mysqli の インスタンス化 、接続
122
+
123
+ public function ConnectDb() {
124
+
125
+ $this->mysqli = new mysqli('localhost', '........', '/////////', '--------');
126
+
127
+ if ($this->mysqli->connect_error){
128
+
129
+ die('Connect Error (' . $this->mysqli->connect_errno . ') '. $this->mysqli->connect_error);
130
+
131
+ exit;
132
+
133
+ }
134
+
135
+ }
136
+
137
+ }
138
+
139
+ class Calender extends dbconnect {
140
+
141
+ protected $weeks = array();
142
+
143
+ protected $timeStamp;
144
+
145
+ // mysqli の メソッド
146
+
147
+ public function __construct(){
148
+
149
+ $dbconnect = new dbconnect();
150
+
151
+ if ($this->timeStamp === false) {
152
+
153
+ $this->timeStamp = time();
154
+
155
+ }
156
+
157
+ // DB接続
158
+
159
+ parent::ConnectDb();
160
+
161
+ }
162
+
163
+ // 現在の日時取得 (Y-m)
164
+
165
+ public function setTimeStamp(){
166
+
167
+ $ym = isset($_GET['ym']) ? $_GET['ym'] : date("Y-m");
168
+
169
+ $this->timeStamp = strtotime($ym . "-01");
170
+
171
+ if ($this->timeStamp === false){
172
+
173
+ $this->timeStamp = time();
174
+
175
+ }
176
+
177
+ }
178
+
179
+ // 現在の日時取得 (Y-m-d)
180
+
181
+ public function setTimeStamp_day(){
182
+
183
+ $ymd = isset($_GET['ymd']) ? $_GET['ymd'] : date("Y-m-d");
184
+
185
+ $this->timeStamp = strtotime($ymd);
186
+
187
+ if ($this->timeStamp === false){
188
+
189
+ $this->timeStamp = time();
190
+
191
+ }
192
+
193
+ }
194
+
195
+ public function query() {
196
+
197
+ // SQLで今月を取得する処理。先月の最終日と翌月の初日を不等号で期間を指定し、ANDで結ぶ。
198
+
199
+ $lastMonth = date("Y-m-d",strtotime("last day of - 1 month",$this->timeStamp));
200
+
201
+ $nextMonth = date("Y-m-d",strtotime("first day of + 1 month",$this->timeStamp));
202
+
203
+ // PHP->MySQLtable
204
+
205
+ $query = $this->mysqli->query("SELECT * FROM tasks WHERE task_date >'$lastMonth' AND task_date <'$nextMonth'");
206
+
207
+ $task = "";
208
+
209
+ // 連想配列で$rowを取得する
210
+
211
+ while($row = $query->fetch_assoc()) {
212
+
213
+ // 各メンバ取得
214
+
215
+ $id = $row['id'];
216
+
217
+ $title = $row['title'];
218
+
219
+ $task_date = $row['task_date'];
220
+
221
+ $place = $row['place'];
222
+
223
+ $memo = $row['memo'];
224
+
225
+ $task .= "<tr><td>${title}</td><td>${task_date}</td><td>${place}</td><td>${memo}</td></tr>";
226
+
227
+ }
228
+
229
+ }
230
+
231
+ public function Create(){
232
+
233
+ // 配列
234
+
235
+ $taskData = array();
236
+
237
+ $lastday = date("t",$this->timeStamp);
238
+
239
+ // 1日の曜日番号の数だけ空白を出力(7月だと3つ)
240
+
241
+ $youbi = date("w",mktime(0,0,0,date("m",$this->timeStamp),1,date("Y",$this->timeStamp)));
242
+
243
+ // $weekを初期化
244
+
245
+ $week = "";
246
+
247
+ // 結合代入演算子を使用して、<td>を取得。一週間(0~6の<td>)が完成。
248
+
249
+ $week .= str_repeat('<td></td>', $youbi);
250
+
251
+ // 上記の定義で1日から31日までfor文で取得
252
+
253
+ for ($day = 1; $day <= $lastday; $day++ ,$youbi++) {
254
+
255
+ // もし$userData[日付]の変数が 空の場合
256
+
257
+ if (isset($userData[$day])) {
258
+
259
+ // リンクをはる
260
+
261
+ $week .= '<td><a href="taskListDay.php">'.$day.'</a></td>';
262
+
263
+ }else{
264
+
265
+ // リンクをはらない
266
+
267
+ $week .= '<td class="youbi_'.($youbi % 7).'">'.$day.'</td>';
268
+
269
+ }
270
+
271
+ // $youbiを7で割った余り=6。(土曜日)
272
+
273
+ if ($youbi % 7 == 6 OR $day == $lastday) {
274
+
275
+ // もし 1 = $lastdayだったとき、<tb>に$youbiを7で割ったものを6で引く
276
+
277
+ if($day == $lastday) {
278
+
279
+ $week .= str_repeat('<td></td>', 6 - ($youbi % 7));
280
+
281
+ }
282
+
283
+ // 結果、<tr>は$week
284
+
285
+ $this->weeks[]='<tr>' .$week .'</tr>';
286
+
287
+ // $weekを初期化する
288
+
289
+ $week = '';
290
+
291
+ }
292
+
293
+ // week""を初期化し続ける処理によってカレンダー型を形成。
294
+
295
+ }
296
+
297
+ }
298
+
299
+ // html記述用
300
+
301
+ public function h($s){
302
+
303
+ return htmlspecialchars($s, ENT_QUOTES, 'UTF-8');
304
+
305
+ }
306
+
307
+ // リストを取得
308
+
309
+ public function mysqli_result() {
310
+
311
+ return $this->mysqli;
312
+
313
+ }
314
+
315
+ // タスクを取得
316
+
317
+ public function getTasks() {
318
+
319
+ $sql = "SELECT * FROM tasks";
320
+
321
+ if ($ymd) {
322
+
323
+ $ymd = mysql_real_escape_string($ymd);
324
+
325
+ $sql.= " WHERE FROM_UNIXTIME(task_date, '%Y-%m-%d') = '$ymd' ";
326
+
327
+ }
328
+
329
+ $query = $this->mysqli->query($sql);
330
+
331
+ }
332
+
333
+ // 一週を取得
334
+
335
+ public function getWeeks() {
336
+
337
+ return $this->weeks;
338
+
339
+ }
340
+
341
+ // 前月を取得
342
+
343
+ public function prev() {
344
+
345
+ return date("Y-m", mktime(0, 0, 0, date("m", $this->timeStamp)-1, 1, date("Y", $this->timeStamp)));
346
+
347
+ }
348
+
349
+ // 翌月を取得
350
+
351
+ public function next() {
352
+
353
+ return date("Y-m", mktime(0, 0, 0, date("m", $this->timeStamp)+1, 1, date("Y", $this->timeStamp)));
354
+
355
+ }
356
+
357
+ // 現在の日時を取得 (Y-m)
358
+
359
+ public function yearManth() {
360
+
361
+ return date("Y-m" ,$this->timeStamp);
362
+
363
+ }
364
+
365
+ }
366
+
367
+ ?>
368
+
369
+ ```

1

test

2015/07/28 00:46

投稿

yutaishikawa_
yutaishikawa_

スコア58

test CHANGED
@@ -1 +1 @@
1
- PHPのクラス化について
1
+ test
test CHANGED
@@ -1,385 +1 @@
1
- んにちは。
2
-
3
- いよいよクラス化の技術を学び始めました。
4
-
5
-
6
-
7
- 今回、前回まで書いてきたコードをカプセル化し、すべての処理を1ファイルずつにまとめました。
8
-
9
-
10
-
11
- ですが、なかなかキレイにまとまってはくれず、さっそく不具合が発生。
12
-
13
- mysqliをメッソド化?できずに、$queryや$taskをvar_dumpしてみても実行結果が0であったり、NULLで返ってくるようになりました。
14
-
15
- これは、正しく接続できているのか・・・と見直したところ、参考にしている文献を見る限りイマイチピンとくることがありませんでした。
16
-
17
-
18
-
19
- こうすればいいよ、というアドバイスがあれば是非お願いいたします。
20
-
21
-
22
-
23
- よろしくお願いいたします。
24
-
25
-
26
-
27
- index
28
-
29
- ```PHP
30
-
31
- <?php
32
-
33
- require_once('calender.php');
34
-
35
-
36
-
37
- $cal = new Calender();
38
-
39
- $cal->setTimeStamp();
40
-
41
- $cal->Create();
42
-
43
- $cal->query();
44
-
45
- ?>
46
-
47
- <!DOCTYPE html>
48
-
49
- <html>
50
-
51
- <title>カレンダー</title>
52
-
53
- <meta charset="utf-8">
54
-
55
- <table border="1">
56
-
57
- <tr>
58
-
59
- <th><a href="?ym=<?php echo $cal->h($cal->prev()); ?>">&laquo;</a></th>
60
-
61
- <th colspan="5"><?php echo $cal->h($cal->yearManth()); ?></th>
62
-
63
- <th><a href="?ym=<?php echo $cal->h($cal->next()); ?>">&raquo;</a></th>
64
-
65
- </tr>
66
-
67
- <thead>
68
-
69
- <tr>
70
-
71
- <th>日</th>
72
-
73
- <th>月</th>
74
-
75
- <th>火</th>
76
-
77
- <th>水</th>
78
-
79
- <th>木</th>
80
-
81
- <th>金</th>
82
-
83
- <th>土</th>
84
-
85
- </tr>
86
-
87
- </thead>
88
-
89
- <tbody>
90
-
91
- <?php
92
-
93
- foreach ($cal->getWeeks() as $week) {
94
-
95
- echo $week;
96
-
97
- }
98
-
99
- ?>
100
-
101
- </tbody>
102
-
103
- </table>
104
-
105
- <form action="edit_do.php" method="POST">
106
-
107
- <p>タイトル : <input type="text" name="title" size="20"></p>
108
-
109
- <p>日時 : <input type="date" name="task_date" min="2015-07-01"></p>
110
-
111
- <p>場所 : <input type="text" name="place" size="20"></p>
112
-
113
- <p>メモ : <textarea type="text" name="memo" rows="4" cols="40"></textarea></p>
114
-
115
- <input type="submit" value="追加する"><input type="reset" value="リセット">
116
-
117
- </form>
118
-
119
- </html>
120
-
121
- ```
122
-
123
-
124
-
125
- class
126
-
127
- ```PHP
128
-
129
- <?php
130
-
131
- // dbconnect.php
132
-
133
- class dbconnect {
134
-
135
- //mysqli の インスタンス化 、接続
136
-
137
- public function ConnectDb() {
138
-
139
- $this->mysqli = new mysqli('localhost', 'tontonmy', 'tontontones', 'st_ishikawa');
140
-
141
- if ($this->mysqli->connect_error){
142
-
143
- die('Connect Error (' . $this->mysqli->connect_errno . ') '. $this->mysqli->connect_error);
144
-
145
- exit;
146
-
147
- }
148
-
149
- }
150
-
151
- }
152
-
153
- class Calender extends dbconnect {
154
-
155
- protected $weeks = array();
156
-
157
- protected $timeStamp;
158
-
159
- // mysqli の メソッド
160
-
161
- public function __construct(){
162
-
163
- $dbconnect = new dbconnect();
164
-
165
- if ($this->timeStamp === false) {
166
-
167
- $this->timeStamp = time();
168
-
169
- }
170
-
171
- // DB接続
172
-
173
- parent::ConnectDb();
174
-
175
- }
176
-
177
- // 現在の日時取得 (Y-m)
178
-
179
- public function setTimeStamp(){
180
-
181
- $ym = isset($_GET['ym']) ? $_GET['ym'] : date("Y-m");
182
-
183
- $this->timeStamp = strtotime($ym . "-01");
184
-
185
- if ($this->timeStamp === false){
186
-
187
- $this->timeStamp = time();
188
-
189
- }
190
-
191
- }
192
-
193
- // 現在の日時取得 (Y-m-d)
194
-
195
- public function setTimeStamp_day(){
196
-
197
- $ymd = isset($_GET['ymd']) ? $_GET['ymd'] : date("Y-m-d");
198
-
199
- $this->timeStamp = strtotime($ymd);
200
-
201
- if ($this->timeStamp === false){
202
-
203
- $this->timeStamp = time();
204
-
205
- }
206
-
207
- }
208
-
209
- public function query() {
210
-
211
- // SQLで今月を取得する処理。先月の最終日と翌月の初日を不等号で期間を指定し、ANDで結ぶ。
212
-
213
- $lastMonth = date("Y-m-d",strtotime("last day of - 1 month",$this->timeStamp));
214
-
215
- $nextMonth = date("Y-m-d",strtotime("first day of + 1 month",$this->timeStamp));
216
-
217
- // PHP->MySQLtable
218
-
219
- $query = $this->mysqli->query("SELECT * FROM tasks WHERE task_date >'$lastMonth' AND task_date <'$nextMonth'");
220
-
221
- $task = "";
222
-
223
- // 連想配列で$rowを取得する
224
-
225
- while($row = $query->fetch_assoc()) {
226
-
227
- // 各メンバ取得
228
-
229
- $id = $row['id'];
230
-
231
- $title = $row['title'];
232
-
233
- $task_date = $row['task_date'];
234
-
235
- $place = $row['place'];
236
-
237
- $memo = $row['memo'];
238
-
239
- $task .= "<tr><td>${title}</td><td>${task_date}</td><td>${place}</td><td>${memo}</td></tr>";
240
-
241
- }
242
-
243
- }
244
-
245
-
246
-
247
- public function Create(){
248
-
249
- // 配列
250
-
251
- $taskData = array();
252
-
253
- $lastday = date("t",$this->timeStamp);
254
-
255
- // 1日の曜日番号の数だけ空白を出力(7月だと3つ)
256
-
257
- $youbi = date("w",mktime(0,0,0,date("m",$this->timeStamp),1,date("Y",$this->timeStamp)));
258
-
259
- // $weekを初期化
260
-
261
- $week = "";
262
-
263
- // 結合代入演算子を使用して、<td>を取得。一週間(0~6の<td>)が完成。
264
-
265
- $week .= str_repeat('<td></td>', $youbi);
266
-
267
- // 上記の定義で1日から31日までfor文で取得
268
-
269
- for ($day = 1; $day <= $lastday; $day++ ,$youbi++) {
270
-
271
- // もし$userData[日付]の変数が 空の場合
272
-
273
- if (isset($userData[$day])) {
274
-
275
- // リンクをはる
276
-
277
- $week .= '<td><a href="taskListDay.php">'.$day.'</a></td>';
278
-
279
- }else{
280
-
281
- // リンクをはらない
282
-
283
- $week .= '<td class="youbi_'.($youbi % 7).'">'.$day.'</td>';
284
-
285
- }
286
-
287
- // $youbiを7で割った余り=6。(土曜日)
288
-
289
- if ($youbi % 7 == 6 OR $day == $lastday) {
290
-
291
- // もし 1 = $lastdayだったとき、<tb>に$youbiを7で割ったものを6で引く
292
-
293
- if($day == $lastday) {
294
-
295
- $week .= str_repeat('<td></td>', 6 - ($youbi % 7));
296
-
297
- }
298
-
299
- // 結果、<tr>は$week
300
-
301
- $this->weeks[]='<tr>' .$week .'</tr>';
302
-
303
- // $weekを初期化する
304
-
305
- $week = '';
306
-
307
- }
308
-
309
- // week""を初期化し続ける処理によってカレンダー型を形成。
310
-
311
- }
312
-
313
- }
314
-
315
- // html記述用
316
-
317
- public function h($s){
318
-
319
- return htmlspecialchars($s, ENT_QUOTES, 'UTF-8');
320
-
321
- }
322
-
323
- // リストを取得
324
-
325
- public function mysqli_result() {
326
-
327
- return $this->mysqli;
328
-
329
- }
330
-
331
- // タスクを取得
332
-
333
- public function getTasks() {
334
-
335
- $sql = "SELECT * FROM tasks";
336
-
337
- if ($ymd) {
338
-
339
- $ymd = mysql_real_escape_string($ymd);
340
-
341
- $sql.= " WHERE FROM_UNIXTIME(task_date, '%Y-%m-%d') = '$ymd' ";
342
-
343
- }
344
-
345
- $query = $this->mysqli->query($sql);
346
-
347
- }
348
-
349
- // 一週を取得
350
-
351
- public function getWeeks() {
352
-
353
- return $this->weeks;
354
-
355
- }
356
-
357
- // 前月を取得
358
-
359
- public function prev() {
360
-
361
- return date("Y-m", mktime(0, 0, 0, date("m", $this->timeStamp)-1, 1, date("Y", $this->timeStamp)));
362
-
363
- }
364
-
365
- // 翌月を取得
366
-
367
- public function next() {
368
-
369
- return date("Y-m", mktime(0, 0, 0, date("m", $this->timeStamp)+1, 1, date("Y", $this->timeStamp)));
370
-
371
- }
372
-
373
- // 現在の日時を取得 (Y-m)
374
-
375
- public function yearManth() {
376
-
377
- return date("Y-m" ,$this->timeStamp);
378
-
379
- }
380
-
381
- }
382
-
383
- ?>
384
-
385
- ```
1
+ のページテストですこのページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。このページはテストです。