質問編集履歴
1
質問文を修正しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,689 +1,631 @@
|
|
1
|
+
配列をツリー上で出力したいのですが上手くいきません。
|
2
|
+
|
3
|
+
以前いただきました自己結合と[こちら](https://qiita.com/yorozuone/items/736a3cda7264046d6f3e)を参考に実行しましたら下記のように表示されました。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
```sample.phpの結果
|
8
|
+
|
9
|
+
Array
|
10
|
+
|
11
|
+
(
|
12
|
+
|
13
|
+
[0] => Array
|
14
|
+
|
15
|
+
(
|
16
|
+
|
17
|
+
[id] => 1418
|
18
|
+
|
19
|
+
[0] => 1419
|
20
|
+
|
21
|
+
[name] => アメリカ
|
22
|
+
|
23
|
+
[1] => アラスカ
|
24
|
+
|
25
|
+
[subject] => トランプ.1
|
26
|
+
|
27
|
+
[2] => マイク
|
28
|
+
|
29
|
+
[body] => Thanks
|
30
|
+
|
31
|
+
[3] => Thanks
|
32
|
+
|
33
|
+
[parent_id] =>
|
34
|
+
|
35
|
+
[4] => 1418
|
36
|
+
|
37
|
+
[post_user] =>
|
38
|
+
|
39
|
+
[5] =>
|
40
|
+
|
41
|
+
[created_at] => 2020-03-17 13:42:24
|
42
|
+
|
43
|
+
[6] => 2020-03-17 13:42:24
|
44
|
+
|
45
|
+
[updated_at] => 2020-03-26 10:28:31
|
46
|
+
|
47
|
+
[7] => 2020-03-18 14:28:50
|
48
|
+
|
49
|
+
[deleted_at] => 2020-03-26 10:28:31
|
50
|
+
|
51
|
+
[8] =>
|
52
|
+
|
53
|
+
[9] => 1418
|
54
|
+
|
55
|
+
[10] => アメリカ
|
56
|
+
|
57
|
+
[11] => トランプ.1
|
58
|
+
|
59
|
+
[12] => Thanks
|
60
|
+
|
61
|
+
[13] =>
|
62
|
+
|
63
|
+
[14] =>
|
64
|
+
|
65
|
+
[15] => 2020-03-17 13:42:24
|
66
|
+
|
67
|
+
[16] => 2020-03-26 10:28:31
|
68
|
+
|
69
|
+
[17] => 2020-03-26 10:28:31
|
70
|
+
|
71
|
+
)
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
[1] => Array
|
76
|
+
|
77
|
+
(
|
78
|
+
|
79
|
+
[id] => 1418
|
80
|
+
|
81
|
+
[0] => 1420
|
82
|
+
|
83
|
+
[name] => アメリカ
|
84
|
+
|
85
|
+
[1] => アリゾナ
|
86
|
+
|
87
|
+
[subject] => トランプ.1
|
88
|
+
|
89
|
+
[2] => ダグ
|
90
|
+
|
91
|
+
[body] => Thanks
|
92
|
+
|
93
|
+
[3] => Thanks
|
94
|
+
|
95
|
+
[parent_id] =>
|
96
|
+
|
97
|
+
[4] => 1418
|
98
|
+
|
99
|
+
[post_user] =>
|
100
|
+
|
101
|
+
[5] =>
|
102
|
+
|
103
|
+
[created_at] => 2020-03-17 13:42:24
|
104
|
+
|
105
|
+
[6] => 2020-03-17 13:42:24
|
106
|
+
|
107
|
+
[updated_at] => 2020-03-26 10:28:31
|
108
|
+
|
109
|
+
[7] => 2020-03-18 14:28:50
|
110
|
+
|
111
|
+
[deleted_at] => 2020-03-26 10:28:31
|
112
|
+
|
113
|
+
[8] =>
|
114
|
+
|
115
|
+
[9] => 1418
|
116
|
+
|
117
|
+
[10] => アメリカ
|
118
|
+
|
119
|
+
[11] => トランプ.1
|
120
|
+
|
121
|
+
[12] => Thanks
|
122
|
+
|
123
|
+
[13] =>
|
124
|
+
|
125
|
+
[14] =>
|
126
|
+
|
127
|
+
[15] => 2020-03-17 13:42:24
|
128
|
+
|
129
|
+
[16] => 2020-03-26 10:28:31
|
130
|
+
|
131
|
+
[17] => 2020-03-26 10:28:31
|
132
|
+
|
133
|
+
)
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
(中略)
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
[8] => Array
|
142
|
+
|
143
|
+
(
|
144
|
+
|
145
|
+
[id] => 1424
|
146
|
+
|
147
|
+
[0] => 1428
|
148
|
+
|
149
|
+
[name] => 中国
|
150
|
+
|
151
|
+
[1] => 河北
|
152
|
+
|
153
|
+
[subject] => 習近平...
|
154
|
+
|
155
|
+
[2] => 許勤
|
156
|
+
|
157
|
+
[body] => シェイシェイ
|
158
|
+
|
159
|
+
[3] => シェイシェイ
|
160
|
+
|
161
|
+
[parent_id] =>
|
162
|
+
|
163
|
+
[4] => 1424
|
164
|
+
|
165
|
+
[post_user] =>
|
166
|
+
|
167
|
+
[5] =>
|
168
|
+
|
169
|
+
[created_at] => 2020-03-17 13:42:24
|
170
|
+
|
171
|
+
[6] => 2020-03-17 13:42:24
|
172
|
+
|
173
|
+
[updated_at] => 2020-03-26 10:28:54
|
174
|
+
|
175
|
+
[7] => 2020-03-18 14:28:50
|
176
|
+
|
177
|
+
[deleted_at] => 2020-03-26 10:28:54
|
178
|
+
|
179
|
+
[8] =>
|
180
|
+
|
181
|
+
[9] => 1424
|
182
|
+
|
183
|
+
[10] => 中国
|
184
|
+
|
185
|
+
[11] => 習近平...
|
186
|
+
|
187
|
+
[12] => シェイシェイ
|
188
|
+
|
189
|
+
[13] =>
|
190
|
+
|
191
|
+
[14] =>
|
192
|
+
|
193
|
+
[15] => 2020-03-17 13:42:24
|
194
|
+
|
195
|
+
[16] => 2020-03-26 10:28:54
|
196
|
+
|
197
|
+
[17] => 2020-03-26 10:28:54
|
198
|
+
|
199
|
+
)
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
[9] => Array
|
204
|
+
|
205
|
+
(
|
206
|
+
|
207
|
+
[id] => 1424
|
208
|
+
|
209
|
+
[0] => 1429
|
210
|
+
|
211
|
+
[name] => 中国
|
212
|
+
|
213
|
+
[1] => 天津
|
214
|
+
|
215
|
+
[subject] => 習近平...
|
216
|
+
|
217
|
+
[2] => 張国清
|
218
|
+
|
219
|
+
[body] => シェイシェイ
|
220
|
+
|
221
|
+
[3] => シェイシェイ
|
222
|
+
|
223
|
+
[parent_id] =>
|
224
|
+
|
225
|
+
[4] => 1424
|
226
|
+
|
227
|
+
[post_user] =>
|
228
|
+
|
229
|
+
[5] =>
|
230
|
+
|
231
|
+
[created_at] => 2020-03-17 13:42:24
|
232
|
+
|
233
|
+
[6] => 2020-03-17 13:42:24
|
234
|
+
|
235
|
+
[updated_at] => 2020-03-26 10:28:54
|
236
|
+
|
237
|
+
[7] => 2020-03-18 14:28:50
|
238
|
+
|
239
|
+
[deleted_at] => 2020-03-26 10:28:54
|
240
|
+
|
241
|
+
[8] =>
|
242
|
+
|
243
|
+
[9] => 1424
|
244
|
+
|
245
|
+
[10] => 中国
|
246
|
+
|
247
|
+
[11] => 習近平...
|
248
|
+
|
249
|
+
[12] => シェイシェイ
|
250
|
+
|
251
|
+
[13] =>
|
252
|
+
|
253
|
+
[14] =>
|
254
|
+
|
255
|
+
[15] => 2020-03-17 13:42:24
|
256
|
+
|
257
|
+
[16] => 2020-03-26 10:28:54
|
258
|
+
|
259
|
+
[17] => 2020-03-26 10:28:54
|
260
|
+
|
261
|
+
)
|
262
|
+
|
263
|
+
```
|
264
|
+
|
265
|
+
[(親レコード1)id name subject body post_user updated_at]
|
266
|
+
|
267
|
+
└[(子レコード5)id name subject body post_user updated_at]
|
268
|
+
|
269
|
+
└[(子レコード4)id name subject body post_user updated_at]
|
270
|
+
|
271
|
+
└[(子レコード3)id name subject body post_user updated_at]
|
272
|
+
|
273
|
+
└[(子レコード2)id name subject body post_user updated_at]
|
274
|
+
|
275
|
+
└[(子レコード1)id name subject body post_user updated_at]
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
上記のようなツリーを出力するにはどうHTML、phpを書いたらよろしいのでしょうか?
|
280
|
+
|
281
|
+
<li></li>または<table><tr></tr></table>を使って表示してみたいと思っています。
|
282
|
+
|
283
|
+
↓アメリカと中国が親です
|
284
|
+
|
285
|
+
![イメージ説明](df8337bc0dedd1cc8f6c94f7c5f7c723.png)
|
286
|
+
|
287
|
+
下記が現在のsample.phpコードです。コメントアウトを外した場合構文エラーとなってしまいます。
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
```php
|
292
|
+
|
293
|
+
$stmt=$db->prepare("select * from t_bbs as t1 left join t_bbs as t2 on t1.parent_id = t2.id where t2.deleted_at is not null order by t2.id desc;");
|
294
|
+
|
295
|
+
$stmt->execute();
|
296
|
+
|
297
|
+
$row = $stmt->fetchAll();
|
298
|
+
|
299
|
+
// foreach($rows as $row){
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
// }
|
304
|
+
|
305
|
+
// 再帰で関数を呼び出します。
|
306
|
+
|
307
|
+
// RecursionArray($row, 0, $rows, 0);
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
// 実行結果表示
|
312
|
+
|
313
|
+
echo('<pre>');
|
314
|
+
|
315
|
+
print_r($row);
|
316
|
+
|
317
|
+
echo('</pre>');
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
// 再帰処理用の関数です。
|
322
|
+
|
323
|
+
// function RecursionArray($row, $ParentId, &$rows, $hierarchy) {
|
324
|
+
|
325
|
+
//
|
326
|
+
|
327
|
+
// foreach($row as $v) {
|
328
|
+
|
329
|
+
// if ($v['ParentId'] == $ParentId) {
|
330
|
+
|
331
|
+
// $rows[] = row_repeat('-', $hierarchy).$v['name'];
|
332
|
+
|
333
|
+
// RecursionArray($row, $v['Id'], $rows, $hierarchy+1);
|
334
|
+
|
335
|
+
// }
|
336
|
+
|
337
|
+
// }
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
// }
|
342
|
+
|
343
|
+
// while ($rows = $stmt->fetchAll()) {
|
344
|
+
|
345
|
+
// $rows[] = $row;
|
346
|
+
|
347
|
+
// var_dump($rows);
|
348
|
+
|
349
|
+
// }
|
350
|
+
|
351
|
+
```
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
下記index.phpでは親だけの出力はできています。汚いですが、
|
356
|
+
|
357
|
+
```php
|
358
|
+
|
359
|
+
<?php
|
360
|
+
|
361
|
+
require('dbconnect.php');
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
$subject = $_POST['subject'];
|
366
|
+
|
367
|
+
$body = $_POST['body'];
|
368
|
+
|
369
|
+
$post_user = $_POST['post_user'];
|
370
|
+
|
371
|
+
?>
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
<?php
|
376
|
+
|
377
|
+
// create.phpから送信されてきた場合
|
378
|
+
|
379
|
+
if (isset($_POST["subject"], $_POST["post_user"])) {
|
380
|
+
|
381
|
+
//件名が空のとき
|
382
|
+
|
383
|
+
if ($_POST["subject"] == '') {
|
384
|
+
|
385
|
+
$isSubject = false;
|
386
|
+
|
387
|
+
}else {
|
388
|
+
|
389
|
+
$isSubject = true;
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
//投稿ユーザーが空のとき
|
394
|
+
|
395
|
+
if ($_POST["post_user"] == '') {
|
396
|
+
|
397
|
+
$isPost_user = false;
|
398
|
+
|
399
|
+
} else {
|
400
|
+
|
401
|
+
$isPost_user = true;
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
// データベースに追加する
|
408
|
+
|
409
|
+
if (isset($isSubject, $isPost_user)) {
|
410
|
+
|
411
|
+
if ($isSubject && $isPost_user) {
|
412
|
+
|
413
|
+
try {
|
414
|
+
|
415
|
+
// トランザクション開始
|
416
|
+
|
417
|
+
// $pdo->beginTransaction();
|
418
|
+
|
419
|
+
$sql = "insert into t_bbs (subject, post_user) VALUES (:subject, :post_user)";
|
420
|
+
|
421
|
+
// $sql = "insert into t_bbs (subject, body, post_user) values (:subject, :body, :post_user)";
|
422
|
+
|
423
|
+
$stmt = $db->prepare($sql);
|
424
|
+
|
425
|
+
$stmt->bindValue(":subject", $_POST["subject"], PDO::PARAM_STR);
|
426
|
+
|
427
|
+
// $stmt->bindValue(":body", $_POST["body"], PDO::PARAM_STR);
|
428
|
+
|
429
|
+
$stmt->bindValue(":post_user", $_POST["post_user"], PDO::PARAM_STR);
|
430
|
+
|
431
|
+
$stmt->execute();
|
432
|
+
|
433
|
+
// $pdo->commit();
|
434
|
+
|
435
|
+
}catch (PDOException $e){
|
436
|
+
|
437
|
+
print('Error:'.$e->getMessage());
|
438
|
+
|
439
|
+
die();
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
}
|
444
|
+
|
445
|
+
}
|
446
|
+
|
447
|
+
//ここまでcreate.phpの処理
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
// $params = array(':subject => $subject', ':body => $body', ':post_user => $post_user');
|
452
|
+
|
453
|
+
// $stmt->execute($params);
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
$sql = "select * from t_bbs where parent_id is null and deleted_at is null order by id desc";
|
458
|
+
|
459
|
+
$stmt = $db->prepare($sql);
|
460
|
+
|
461
|
+
$stmt->execute();
|
462
|
+
|
463
|
+
while($row = $stmt->fetch()){
|
464
|
+
|
465
|
+
$rows[] = $row;
|
466
|
+
|
467
|
+
}
|
468
|
+
|
469
|
+
/*
|
470
|
+
|
471
|
+
foreach ($statement as $row) {
|
472
|
+
|
473
|
+
$rows[] = $row;
|
474
|
+
|
475
|
+
}
|
476
|
+
|
477
|
+
*/
|
478
|
+
|
479
|
+
//データベース接続切断
|
480
|
+
|
481
|
+
$db = null;
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
?>
|
486
|
+
|
487
|
+
<!DOCTYPE html>
|
488
|
+
|
489
|
+
<html>
|
490
|
+
|
491
|
+
<head>
|
492
|
+
|
493
|
+
<title>一覧表示</title>
|
494
|
+
|
495
|
+
<meta charset="utf-8">
|
496
|
+
|
497
|
+
</head>
|
498
|
+
|
499
|
+
<body>
|
500
|
+
|
501
|
+
<h1>一覧表示</h1>
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
<table border='1'>
|
506
|
+
|
507
|
+
<tr>
|
508
|
+
|
509
|
+
<td><input type="checkbox"></td><td>ID</td><td>件名</td><td>投稿ユーザー</td><td>投稿日時</td>
|
510
|
+
|
511
|
+
</tr>
|
512
|
+
|
513
|
+
<?php
|
514
|
+
|
515
|
+
foreach($rows as $row){
|
516
|
+
|
517
|
+
?>
|
518
|
+
|
519
|
+
<tr>
|
520
|
+
|
521
|
+
<td>
|
522
|
+
|
523
|
+
<form action="delete.php" method="GET" onSubmit="return check()" id="delete">
|
524
|
+
|
525
|
+
<input form="delete" type="checkbox" name="id[]" value="<?php print($row['id']); ?>">
|
526
|
+
|
527
|
+
</form>
|
528
|
+
|
529
|
+
</td>
|
530
|
+
|
531
|
+
<td><?php print($row['id']); ?>
|
532
|
+
|
533
|
+
</td>
|
534
|
+
|
535
|
+
<td>
|
536
|
+
|
537
|
+
<form action="edit.php" method="POST">
|
538
|
+
|
539
|
+
<a href="edit.php?id=<?php echo $row['id'];?>">
|
540
|
+
|
541
|
+
<?php echo $row['subject']; ?>
|
542
|
+
|
543
|
+
</a>
|
544
|
+
|
545
|
+
</form>
|
546
|
+
|
547
|
+
</td>
|
548
|
+
|
549
|
+
<td><?php echo $row['post_user']; ?></td>
|
550
|
+
|
551
|
+
<td><?php echo $row['created_at']; ?></td>
|
552
|
+
|
553
|
+
</tr>
|
554
|
+
|
555
|
+
<?php
|
556
|
+
|
557
|
+
}
|
558
|
+
|
559
|
+
?>
|
560
|
+
|
561
|
+
</table>
|
562
|
+
|
563
|
+
<input form="delete" type="submit" value="削除する">
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
<script type="text/javascript">
|
568
|
+
|
569
|
+
function check(){
|
570
|
+
|
571
|
+
if(window.confirm('本当に削除しますか?')){
|
572
|
+
|
573
|
+
return true;
|
574
|
+
|
575
|
+
var elements = document.getElementsByName('id[]') ;
|
576
|
+
|
577
|
+
for(var i = 0 ; i < elements.length ; i ++){
|
578
|
+
|
579
|
+
if(elements[i].checked == true){
|
580
|
+
|
581
|
+
// alert(elements[i].value);
|
582
|
+
|
583
|
+
location.href = "delete.php";
|
584
|
+
|
585
|
+
}
|
586
|
+
|
587
|
+
}
|
588
|
+
|
589
|
+
}
|
590
|
+
|
591
|
+
else{
|
592
|
+
|
593
|
+
window.alert('キャンセルされました');
|
594
|
+
|
595
|
+
return false;
|
596
|
+
|
597
|
+
}
|
598
|
+
|
599
|
+
}
|
600
|
+
|
601
|
+
</script>
|
602
|
+
|
603
|
+
<p>
|
604
|
+
|
605
|
+
<a href="create.php">新規投稿へ</a>
|
606
|
+
|
607
|
+
</p>
|
608
|
+
|
609
|
+
</body>
|
610
|
+
|
611
|
+
<!-- <form action="delete.php" method="GET" onSubmit="return check()">
|
612
|
+
|
613
|
+
<input type="checkbox" name="id[]" value="<?php echo $row['id']; ?>">
|
614
|
+
|
615
|
+
<?php echo $row['id']; ?>
|
616
|
+
|
617
|
+
<input type="submit" value="削除する">
|
618
|
+
|
619
|
+
</form> -->
|
620
|
+
|
621
|
+
</html>
|
622
|
+
|
623
|
+
|
624
|
+
|
625
|
+
```
|
626
|
+
|
1
627
|
### 追記
|
2
628
|
|
3
|
-
以前いただきました自己結合と[こちら](https://qiita.com/yorozuone/items/736a3cda7264046d6f3e)を参考に実行しましたら下記のように表示されました。
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
```ここに言語を入力
|
8
|
-
|
9
|
-
Array
|
10
|
-
|
11
|
-
(
|
12
|
-
|
13
|
-
[0] => Array
|
14
|
-
|
15
|
-
(
|
16
|
-
|
17
|
-
[id] => 1418
|
18
|
-
|
19
|
-
[0] => 1419
|
20
|
-
|
21
|
-
[name] => アメリカ
|
22
|
-
|
23
|
-
[1] => アラスカ
|
24
|
-
|
25
|
-
[subject] => トランプ.1
|
26
|
-
|
27
|
-
[2] => マイク
|
28
|
-
|
29
|
-
[body] => Thanks
|
30
|
-
|
31
|
-
[3] => Thanks
|
32
|
-
|
33
|
-
[parent_id] =>
|
34
|
-
|
35
|
-
[4] => 1418
|
36
|
-
|
37
|
-
[post_user] =>
|
38
|
-
|
39
|
-
[5] =>
|
40
|
-
|
41
|
-
[created_at] => 2020-03-17 13:42:24
|
42
|
-
|
43
|
-
[6] => 2020-03-17 13:42:24
|
44
|
-
|
45
|
-
[updated_at] => 2020-03-26 10:28:31
|
46
|
-
|
47
|
-
[7] => 2020-03-18 14:28:50
|
48
|
-
|
49
|
-
[deleted_at] => 2020-03-26 10:28:31
|
50
|
-
|
51
|
-
[8] =>
|
52
|
-
|
53
|
-
[9] => 1418
|
54
|
-
|
55
|
-
[10] => アメリカ
|
56
|
-
|
57
|
-
[11] => トランプ.1
|
58
|
-
|
59
|
-
[12] => Thanks
|
60
|
-
|
61
|
-
[13] =>
|
62
|
-
|
63
|
-
[14] =>
|
64
|
-
|
65
|
-
[15] => 2020-03-17 13:42:24
|
66
|
-
|
67
|
-
[16] => 2020-03-26 10:28:31
|
68
|
-
|
69
|
-
[17] => 2020-03-26 10:28:31
|
70
|
-
|
71
|
-
)
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
[1] => Array
|
76
|
-
|
77
|
-
(
|
78
|
-
|
79
|
-
[id] => 1418
|
80
|
-
|
81
|
-
[0] => 1420
|
82
|
-
|
83
|
-
[name] => アメリカ
|
84
|
-
|
85
|
-
[1] => アリゾナ
|
86
|
-
|
87
|
-
[subject] => トランプ.1
|
88
|
-
|
89
|
-
[2] => ダグ
|
90
|
-
|
91
|
-
[body] => Thanks
|
92
|
-
|
93
|
-
[3] => Thanks
|
94
|
-
|
95
|
-
[parent_id] =>
|
96
|
-
|
97
|
-
[4] => 1418
|
98
|
-
|
99
|
-
[post_user] =>
|
100
|
-
|
101
|
-
[5] =>
|
102
|
-
|
103
|
-
[created_at] => 2020-03-17 13:42:24
|
104
|
-
|
105
|
-
[6] => 2020-03-17 13:42:24
|
106
|
-
|
107
|
-
[updated_at] => 2020-03-26 10:28:31
|
108
|
-
|
109
|
-
[7] => 2020-03-18 14:28:50
|
110
|
-
|
111
|
-
[deleted_at] => 2020-03-26 10:28:31
|
112
|
-
|
113
|
-
[8] =>
|
114
|
-
|
115
|
-
[9] => 1418
|
116
|
-
|
117
|
-
[10] => アメリカ
|
118
|
-
|
119
|
-
[11] => トランプ.1
|
120
|
-
|
121
|
-
[12] => Thanks
|
122
|
-
|
123
|
-
[13] =>
|
124
|
-
|
125
|
-
[14] =>
|
126
|
-
|
127
|
-
[15] => 2020-03-17 13:42:24
|
128
|
-
|
129
|
-
[16] => 2020-03-26 10:28:31
|
130
|
-
|
131
|
-
[17] => 2020-03-26 10:28:31
|
132
|
-
|
133
|
-
)
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
[2] => Array
|
138
|
-
|
139
|
-
(
|
140
|
-
|
141
|
-
[id] => 1418
|
142
|
-
|
143
|
-
[0] => 1421
|
144
|
-
|
145
|
-
[name] => アメリカ
|
146
|
-
|
147
|
-
[1] => カリフォルニア
|
148
|
-
|
149
|
-
[subject] => トランプ.1
|
150
|
-
|
151
|
-
[2] => キャビン
|
152
|
-
|
153
|
-
[body] => Thanks
|
154
|
-
|
155
|
-
[3] => Thanks
|
156
|
-
|
157
|
-
[parent_id] =>
|
158
|
-
|
159
|
-
[4] => 1418
|
160
|
-
|
161
|
-
[post_user] =>
|
162
|
-
|
163
|
-
[5] =>
|
164
|
-
|
165
|
-
[created_at] => 2020-03-17 13:42:24
|
166
|
-
|
167
|
-
[6] => 2020-03-17 13:42:24
|
168
|
-
|
169
|
-
[updated_at] => 2020-03-26 10:28:31
|
170
|
-
|
171
|
-
[7] => 2020-03-18 14:28:50
|
172
|
-
|
173
|
-
[deleted_at] => 2020-03-26 10:28:31
|
174
|
-
|
175
|
-
[8] =>
|
176
|
-
|
177
|
-
[9] => 1418
|
178
|
-
|
179
|
-
[10] => アメリカ
|
180
|
-
|
181
|
-
[11] => トランプ.1
|
182
|
-
|
183
|
-
[12] => Thanks
|
184
|
-
|
185
|
-
[13] =>
|
186
|
-
|
187
|
-
[14] =>
|
188
|
-
|
189
|
-
[15] => 2020-03-17 13:42:24
|
190
|
-
|
191
|
-
[16] => 2020-03-26 10:28:31
|
192
|
-
|
193
|
-
[17] => 2020-03-26 10:28:31
|
194
|
-
|
195
|
-
)
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
[3] => Array
|
200
|
-
|
201
|
-
(
|
202
|
-
|
203
|
-
[id] => 1418
|
204
|
-
|
205
|
-
[0] => 1422
|
206
|
-
|
207
|
-
[name] => アメリカ
|
208
|
-
|
209
|
-
[1] => コロラド
|
210
|
-
|
211
|
-
[subject] => トランプ.1
|
212
|
-
|
213
|
-
[2] => ジャレド
|
214
|
-
|
215
|
-
[body] => Thanks
|
216
|
-
|
217
|
-
[3] => Thanks
|
218
|
-
|
219
|
-
[parent_id] =>
|
220
|
-
|
221
|
-
[4] => 1418
|
222
|
-
|
223
|
-
[post_user] =>
|
224
|
-
|
225
|
-
[5] =>
|
226
|
-
|
227
|
-
[created_at] => 2020-03-17 13:42:24
|
228
|
-
|
229
|
-
[6] => 2020-03-17 13:42:24
|
230
|
-
|
231
|
-
[updated_at] => 2020-03-26 10:28:31
|
232
|
-
|
233
|
-
[7] => 2020-03-18 14:28:50
|
234
|
-
|
235
|
-
[deleted_at] => 2020-03-26 10:28:31
|
236
|
-
|
237
|
-
[8] =>
|
238
|
-
|
239
|
-
[9] => 1418
|
240
|
-
|
241
|
-
[10] => アメリカ
|
242
|
-
|
243
|
-
[11] => トランプ.1
|
244
|
-
|
245
|
-
[12] => Thanks
|
246
|
-
|
247
|
-
[13] =>
|
248
|
-
|
249
|
-
[14] =>
|
250
|
-
|
251
|
-
[15] => 2020-03-17 13:42:24
|
252
|
-
|
253
|
-
[16] => 2020-03-26 10:28:31
|
254
|
-
|
255
|
-
[17] => 2020-03-26 10:28:31
|
256
|
-
|
257
|
-
)
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
[4] => Array
|
262
|
-
|
263
|
-
(
|
264
|
-
|
265
|
-
[id] => 1418
|
266
|
-
|
267
|
-
[0] => 1423
|
268
|
-
|
269
|
-
[name] => アメリカ
|
270
|
-
|
271
|
-
[1] => フロリダ
|
272
|
-
|
273
|
-
[subject] => トランプ.1
|
274
|
-
|
275
|
-
[2] => ロン
|
276
|
-
|
277
|
-
[body] => Thanks
|
278
|
-
|
279
|
-
[3] => Thanks
|
280
|
-
|
281
|
-
[parent_id] =>
|
282
|
-
|
283
|
-
[4] => 1418
|
284
|
-
|
285
|
-
[post_user] =>
|
286
|
-
|
287
|
-
[5] =>
|
288
|
-
|
289
|
-
[created_at] => 2020-03-17 13:42:24
|
290
|
-
|
291
|
-
[6] => 2020-03-17 13:42:24
|
292
|
-
|
293
|
-
[updated_at] => 2020-03-26 10:28:31
|
294
|
-
|
295
|
-
[7] => 2020-03-18 14:28:50
|
296
|
-
|
297
|
-
[deleted_at] => 2020-03-26 10:28:31
|
298
|
-
|
299
|
-
[8] =>
|
300
|
-
|
301
|
-
[9] => 1418
|
302
|
-
|
303
|
-
[10] => アメリカ
|
304
|
-
|
305
|
-
[11] => トランプ.1
|
306
|
-
|
307
|
-
[12] => Thanks
|
308
|
-
|
309
|
-
[13] =>
|
310
|
-
|
311
|
-
[14] =>
|
312
|
-
|
313
|
-
[15] => 2020-03-17 13:42:24
|
314
|
-
|
315
|
-
[16] => 2020-03-26 10:28:31
|
316
|
-
|
317
|
-
[17] => 2020-03-26 10:28:31
|
318
|
-
|
319
|
-
)
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
```
|
324
|
-
|
325
|
-
[(親レコード1)id name subject body post_user updated_at]
|
326
|
-
|
327
|
-
└[(子レコード5)id name subject body post_user updated_at]
|
328
|
-
|
329
|
-
└[(子レコード4)id name subject body post_user updated_at]
|
330
|
-
|
331
|
-
└[(子レコード3)id name subject body post_user updated_at]
|
332
|
-
|
333
|
-
└[(子レコード2)id name subject body post_user updated_at]
|
334
|
-
|
335
|
-
└[(子レコード1)id name subject body post_user updated_at]
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
上記のようなツリーを出力するにはどうHTML、phpを書いたらよろしいのでしょうか?
|
340
|
-
|
341
|
-
<li></li>または<table><tr></tr></table>を使って表示してみたいと思っています。
|
342
|
-
|
343
|
-
↓アメリカと中国が親です
|
344
|
-
|
345
|
-
![イメージ説明](df8337bc0dedd1cc8f6c94f7c5f7c723.png)
|
346
|
-
|
347
|
-
下記が現在のsample.phpコードです。コメントアウトを外した場合構文エラーとなってしまいます。
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
```php
|
352
|
-
|
353
|
-
$stmt=$db->prepare("select * from t_bbs as t1 left join t_bbs as t2 on t1.parent_id = t2.id where t2.deleted_at is not null order by t2.id desc;");
|
354
|
-
|
355
|
-
$stmt->execute();
|
356
|
-
|
357
|
-
$row = $stmt->fetchAll();
|
358
|
-
|
359
|
-
// foreach($rows as $row){
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
// }
|
364
|
-
|
365
|
-
// 再帰で関数を呼び出します。
|
366
|
-
|
367
|
-
// RecursionArray($row, 0, $rows, 0);
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
// 実行結果表示
|
372
|
-
|
373
|
-
echo('<pre>');
|
374
|
-
|
375
|
-
print_r($row);
|
376
|
-
|
377
|
-
echo('</pre>');
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
// 再帰処理用の関数です。
|
382
|
-
|
383
|
-
// function RecursionArray($row, $ParentId, &$rows, $hierarchy) {
|
384
|
-
|
385
|
-
//
|
386
|
-
|
387
|
-
// foreach($row as $v) {
|
388
|
-
|
389
|
-
// if ($v['ParentId'] == $ParentId) {
|
390
|
-
|
391
|
-
// $rows[] = row_repeat('-', $hierarchy).$v['name'];
|
392
|
-
|
393
|
-
// RecursionArray($row, $v['Id'], $rows, $hierarchy+1);
|
394
|
-
|
395
|
-
// }
|
396
|
-
|
397
|
-
// }
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
// }
|
402
|
-
|
403
|
-
// while ($rows = $stmt->fetchAll()) {
|
404
|
-
|
405
|
-
// $rows[] = $row;
|
406
|
-
|
407
|
-
// var_dump($rows);
|
408
|
-
|
409
|
-
// }
|
410
|
-
|
411
|
-
```
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
下記index.phpでは親だけの出力はできています。汚いですが、
|
416
|
-
|
417
|
-
```php
|
418
|
-
|
419
|
-
<?php
|
420
|
-
|
421
|
-
require('dbconnect.php');
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
$subject = $_POST['subject'];
|
426
|
-
|
427
|
-
$body = $_POST['body'];
|
428
|
-
|
429
|
-
$post_user = $_POST['post_user'];
|
430
|
-
|
431
|
-
?>
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
<?php
|
436
|
-
|
437
|
-
// create.phpから送信されてきた場合
|
438
|
-
|
439
|
-
if (isset($_POST["subject"], $_POST["post_user"])) {
|
440
|
-
|
441
|
-
//件名が空のとき
|
442
|
-
|
443
|
-
if ($_POST["subject"] == '') {
|
444
|
-
|
445
|
-
$isSubject = false;
|
446
|
-
|
447
|
-
}else {
|
448
|
-
|
449
|
-
$isSubject = true;
|
450
|
-
|
451
|
-
}
|
452
|
-
|
453
|
-
//投稿ユーザーが空のとき
|
454
|
-
|
455
|
-
if ($_POST["post_user"] == '') {
|
456
|
-
|
457
|
-
$isPost_user = false;
|
458
|
-
|
459
|
-
} else {
|
460
|
-
|
461
|
-
$isPost_user = true;
|
462
|
-
|
463
|
-
}
|
464
|
-
|
465
|
-
}
|
466
|
-
|
467
|
-
// データベースに追加する
|
468
|
-
|
469
|
-
if (isset($isSubject, $isPost_user)) {
|
470
|
-
|
471
|
-
if ($isSubject && $isPost_user) {
|
472
|
-
|
473
|
-
try {
|
474
|
-
|
475
|
-
// トランザクション開始
|
476
|
-
|
477
|
-
// $pdo->beginTransaction();
|
478
|
-
|
479
|
-
$sql = "insert into t_bbs (subject, post_user) VALUES (:subject, :post_user)";
|
480
|
-
|
481
|
-
// $sql = "insert into t_bbs (subject, body, post_user) values (:subject, :body, :post_user)";
|
482
|
-
|
483
|
-
$stmt = $db->prepare($sql);
|
484
|
-
|
485
|
-
$stmt->bindValue(":subject", $_POST["subject"], PDO::PARAM_STR);
|
486
|
-
|
487
|
-
// $stmt->bindValue(":body", $_POST["body"], PDO::PARAM_STR);
|
488
|
-
|
489
|
-
$stmt->bindValue(":post_user", $_POST["post_user"], PDO::PARAM_STR);
|
490
|
-
|
491
|
-
$stmt->execute();
|
492
|
-
|
493
|
-
// $pdo->commit();
|
494
|
-
|
495
|
-
}catch (PDOException $e){
|
496
|
-
|
497
|
-
print('Error:'.$e->getMessage());
|
498
|
-
|
499
|
-
die();
|
500
|
-
|
501
|
-
}
|
502
|
-
|
503
|
-
}
|
504
|
-
|
505
|
-
}
|
506
|
-
|
507
|
-
//ここまでcreate.phpの処理
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
// $params = array(':subject => $subject', ':body => $body', ':post_user => $post_user');
|
512
|
-
|
513
|
-
// $stmt->execute($params);
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
$sql = "select * from t_bbs where parent_id is null and deleted_at is null order by id desc";
|
518
|
-
|
519
|
-
$stmt = $db->prepare($sql);
|
520
|
-
|
521
|
-
$stmt->execute();
|
522
|
-
|
523
|
-
while($row = $stmt->fetch()){
|
524
|
-
|
525
|
-
$rows[] = $row;
|
526
|
-
|
527
|
-
}
|
528
|
-
|
529
|
-
/*
|
530
|
-
|
531
|
-
foreach ($statement as $row) {
|
532
|
-
|
533
|
-
$rows[] = $row;
|
534
|
-
|
535
|
-
}
|
536
|
-
|
537
|
-
*/
|
538
|
-
|
539
|
-
//データベース接続切断
|
540
|
-
|
541
|
-
$db = null;
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
?>
|
546
|
-
|
547
|
-
<!DOCTYPE html>
|
548
|
-
|
549
|
-
<html>
|
550
|
-
|
551
|
-
<head>
|
552
|
-
|
553
|
-
<title>一覧表示</title>
|
554
|
-
|
555
|
-
<meta charset="utf-8">
|
556
|
-
|
557
|
-
</head>
|
558
|
-
|
559
|
-
<body>
|
560
|
-
|
561
|
-
<h1>一覧表示</h1>
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
<table border='1'>
|
566
|
-
|
567
|
-
<tr>
|
568
|
-
|
569
|
-
<td><input type="checkbox"></td><td>ID</td><td>件名</td><td>投稿ユーザー</td><td>投稿日時</td>
|
570
|
-
|
571
|
-
</tr>
|
572
|
-
|
573
|
-
<?php
|
574
|
-
|
575
|
-
foreach($rows as $row){
|
576
|
-
|
577
|
-
?>
|
578
|
-
|
579
|
-
<tr>
|
580
|
-
|
581
|
-
<td>
|
582
|
-
|
583
|
-
<form action="delete.php" method="GET" onSubmit="return check()" id="delete">
|
584
|
-
|
585
|
-
<input form="delete" type="checkbox" name="id[]" value="<?php print($row['id']); ?>">
|
586
|
-
|
587
|
-
</form>
|
588
|
-
|
589
|
-
</td>
|
590
|
-
|
591
|
-
<td><?php print($row['id']); ?>
|
592
|
-
|
593
|
-
</td>
|
594
|
-
|
595
|
-
<td>
|
596
|
-
|
597
|
-
<form action="edit.php" method="POST">
|
598
|
-
|
599
|
-
<a href="edit.php?id=<?php echo $row['id'];?>">
|
600
|
-
|
601
|
-
<?php echo $row['subject']; ?>
|
602
|
-
|
603
|
-
</a>
|
604
|
-
|
605
|
-
</form>
|
606
|
-
|
607
|
-
</td>
|
608
|
-
|
609
|
-
<td><?php echo $row['post_user']; ?></td>
|
610
|
-
|
611
|
-
<td><?php echo $row['created_at']; ?></td>
|
612
|
-
|
613
|
-
</tr>
|
614
|
-
|
615
|
-
<?php
|
616
|
-
|
617
|
-
}
|
618
|
-
|
619
|
-
?>
|
620
|
-
|
621
|
-
</table>
|
622
|
-
|
623
|
-
<input form="delete" type="submit" value="削除する">
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
<script type="text/javascript">
|
628
|
-
|
629
|
-
function check(){
|
630
|
-
|
631
|
-
if(window.confirm('本当に削除しますか?')){
|
632
|
-
|
633
|
-
return true;
|
634
|
-
|
635
|
-
var elements = document.getElementsByName('id[]') ;
|
636
|
-
|
637
|
-
for(var i = 0 ; i < elements.length ; i ++){
|
638
|
-
|
639
|
-
if(elements[i].checked == true){
|
640
|
-
|
641
|
-
// alert(elements[i].value);
|
642
|
-
|
643
|
-
location.href = "delete.php";
|
644
|
-
|
645
|
-
}
|
646
|
-
|
647
|
-
}
|
648
|
-
|
649
|
-
}
|
650
|
-
|
651
|
-
else{
|
652
|
-
|
653
|
-
window.alert('キャンセルされました');
|
654
|
-
|
655
|
-
return false;
|
656
|
-
|
657
|
-
}
|
658
|
-
|
659
|
-
}
|
660
|
-
|
661
|
-
</script>
|
662
|
-
|
663
|
-
<p>
|
664
|
-
|
665
|
-
<a href="create.php">新規投稿へ</a>
|
666
|
-
|
667
|
-
</p>
|
668
|
-
|
669
|
-
</body>
|
670
|
-
|
671
|
-
<!-- <form action="delete.php" method="GET" onSubmit="return check()">
|
672
|
-
|
673
|
-
<input type="checkbox" name="id[]" value="<?php echo $row['id']; ?>">
|
674
|
-
|
675
|
-
<?php echo $row['id']; ?>
|
676
|
-
|
677
|
-
<input type="submit" value="削除する">
|
678
|
-
|
679
|
-
</form> -->
|
680
|
-
|
681
|
-
</html>
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
```
|
686
|
-
|
687
629
|
![イメージ説明](69b7345c14708b7ccd9425b15b84c8d8.png)
|
688
630
|
|
689
631
|
上に子供をツリー上に入れてみたいです。
|