回答編集履歴

5

ちょうせい

2019/10/04 08:05

投稿

yambejp
yambejp

スコア114784

test CHANGED
@@ -470,7 +470,7 @@
470
470
 
471
471
  $no =get_post("no");
472
472
 
473
- $fname ="x.txt";
473
+ $fname ="keijiban.txt";
474
474
 
475
475
  if(!file_exists($fname)) die("no file");
476
476
 

4

調整

2019/10/04 08:05

投稿

yambejp
yambejp

スコア114784

test CHANGED
@@ -443,3 +443,221 @@
443
443
 
444
444
 
445
445
  ```
446
+
447
+
448
+
449
+ # むちゃくちゃ古くても動く書き方
450
+
451
+ ```PHP
452
+
453
+ <?php
454
+
455
+ header('content-type:text/html;charset=UTF8');
456
+
457
+ session_start();
458
+
459
+ function get_post($val){
460
+
461
+ return isset($_POST[$val])?$_POST[$val]:null;
462
+
463
+ }
464
+
465
+ $s =get_post("s");
466
+
467
+ $name =get_post("name");
468
+
469
+ $comment=get_post("comment");
470
+
471
+ $no =get_post("no");
472
+
473
+ $fname ="x.txt";
474
+
475
+ if(!file_exists($fname)) die("no file");
476
+
477
+ if($s==="編集"){
478
+
479
+ $fp=fopen($fname,"r");
480
+
481
+ while(($row=fgetcsv($fp,1024))!==false){
482
+
483
+ $rows[]=$row;
484
+
485
+ }
486
+
487
+ fclose($fp);
488
+
489
+ foreach($rows as $val){
490
+
491
+ if($val[0]===$no) $rec[]=$val;
492
+
493
+ }
494
+
495
+ if(count($rec)>0){
496
+
497
+ $rec=array_shift($rec);
498
+
499
+ $_SESSION["name"] =$rec[1];
500
+
501
+ $_SESSION["comment"] =$rec[2];
502
+
503
+ }else{
504
+
505
+ $_SESSION["name"] ="";
506
+
507
+ $_SESSION["comment"] ="";
508
+
509
+ }
510
+
511
+ }
512
+
513
+ if($s==="投稿"){
514
+
515
+ $fp=fopen($fname,"r");
516
+
517
+ while(($row=fgetcsv($fp,1024))!==false){
518
+
519
+ $rows[]=$row;
520
+
521
+ }
522
+
523
+ fclose($fp);
524
+
525
+ foreach($rows as $index=>$val){
526
+
527
+ if($val[0]===$no){
528
+
529
+ $key=$index;
530
+
531
+ break;
532
+
533
+ }
534
+
535
+ }
536
+
537
+
538
+
539
+ if($key>=0){
540
+
541
+ $row=&$rows[$key];
542
+
543
+ }else{
544
+
545
+ $row=&$rows[];
546
+
547
+ }
548
+
549
+ $row[0]=$no;
550
+
551
+ $row[1]=$name;
552
+
553
+ $row[2]=$comment;
554
+
555
+ unset($row);
556
+
557
+ $fp=fopen($fname,"w");
558
+
559
+ foreach ($rows as $row) {
560
+
561
+ foreach ($row as $key=>$val) {
562
+
563
+ if(!is_numeric($val)){
564
+
565
+ $val=str_replace("\"","\"\"",$val);
566
+
567
+ $val="\"".$val."\"";
568
+
569
+ $row[$key]=$val;
570
+
571
+ }
572
+
573
+ }
574
+
575
+ fwrite($fp,implode(",",$row)."\r\n");
576
+
577
+ }
578
+
579
+ fclose($fp);
580
+
581
+ }
582
+
583
+
584
+
585
+
586
+
587
+ if (!is_null($name) and !is_null($comment) ) {
588
+
589
+ $_SESSION["name"] =$name;
590
+
591
+ $_SESSION["comment"] =$comment;
592
+
593
+ header('Location: '.$_SERVER["SCRIPT_NAME"]);
594
+
595
+ exit;
596
+
597
+ }elseif (!is_null($no)) {
598
+
599
+ $_SESSION["no"] =$no;
600
+
601
+ header('Location: '.$_SERVER["SCRIPT_NAME"]);
602
+
603
+ exit;
604
+
605
+ }else{
606
+
607
+ $name =isset($_SESSION["name"])? $_SESSION["name"] :null;
608
+
609
+ $comment=isset($_SESSION["comment"])?$_SESSION["comment"]:null;
610
+
611
+ $no =isset($_SESSION["no"])? $_SESSION["no"] :null;
612
+
613
+ }
614
+
615
+ $h_name =htmlspecialchars($name);
616
+
617
+ $h_comment=htmlspecialchars($comment);
618
+
619
+ $h_no =htmlspecialchars($no);
620
+
621
+ ?>
622
+
623
+ <style>
624
+
625
+ .required{color:red;}
626
+
627
+ </style>
628
+
629
+
630
+
631
+ <form method="post" >
632
+
633
+ <input type="text" name="no" value="<?=$h_no;?>" readonly><br>
634
+
635
+ お名前<span class="required">【必須】</span>
636
+
637
+ <input type="text" name="name" id="name-field" required="required" value="<?=$h_name;?>"><br>
638
+
639
+ コメント<span class="required">【必須】</span><br>
640
+
641
+ <textarea name="comment" cols="30" rows="3" id="comment" required="required">
642
+
643
+ <?=$h_comment;?>
644
+
645
+ </textarea>
646
+
647
+ <input type="submit" name="s" value="投稿">
648
+
649
+ </form>
650
+
651
+
652
+
653
+ <form method="post">
654
+
655
+ 編集対象番号
656
+
657
+ <input type="number" name="no" value="<?=$h_no;?>" min="1" required>
658
+
659
+ <input type="submit" name="s" value="編集">
660
+
661
+ </form>
662
+
663
+ ```

3

chousei

2019/10/04 08:02

投稿

yambejp
yambejp

スコア114784

test CHANGED
@@ -217,3 +217,229 @@
217
217
  3,piyo,うううう
218
218
 
219
219
  ```
220
+
221
+
222
+
223
+ # 下位互換(とりあえず)
224
+
225
+ ```PHP
226
+
227
+ <?php
228
+
229
+ session_start();
230
+
231
+ $s =filter_input(INPUT_POST,"s");
232
+
233
+ $name =filter_input(INPUT_POST,"name");
234
+
235
+ $comment=filter_input(INPUT_POST,"comment");
236
+
237
+ $no =filter_input(INPUT_POST,"no");
238
+
239
+ $fname ="keijiban.txt";
240
+
241
+ if(!file_exists($fname)) die("no file"); // 追記
242
+
243
+ if($s==="編集"){
244
+
245
+ $fp=fopen($fname,"r");
246
+
247
+ while(($row=fgetcsv($fp,1024))!==false){
248
+
249
+ $rows[]=$row;
250
+
251
+ }
252
+
253
+ fclose($fp);
254
+
255
+ foreach($rows as $val){
256
+
257
+ if($val[0]===$no) $rec[]=$val;
258
+
259
+ }
260
+
261
+ //$rec=array_filter($rows,function($x) use($no){
262
+
263
+ // return $x[0]===$no;
264
+
265
+ //});
266
+
267
+ if(count($rec)>0){
268
+
269
+ /* 修正
270
+
271
+ $rec=array_values($rec)[0];
272
+
273
+ */
274
+
275
+ $rec=array_shift($rec);
276
+
277
+ $_SESSION["name"] =$rec[1];
278
+
279
+ $_SESSION["comment"] =$rec[2];
280
+
281
+ }else{
282
+
283
+ $_SESSION["name"] ="";
284
+
285
+ $_SESSION["comment"] ="";
286
+
287
+ }
288
+
289
+ }
290
+
291
+ if($s==="投稿"){
292
+
293
+ $fp=fopen($fname,"r");
294
+
295
+ while(($row=fgetcsv($fp,1024))!==false){
296
+
297
+ $rows[]=$row;
298
+
299
+ }
300
+
301
+ fclose($fp);
302
+
303
+ /* 修正
304
+
305
+ $key=array_keys(array_filter($rows,function($x) use($no){
306
+
307
+ return $x[0]===$no;
308
+
309
+ }))[0];
310
+
311
+ */
312
+
313
+ foreach($rows as $index=>$val){
314
+
315
+ if($val[0]===$no){
316
+
317
+ $key=$index;
318
+
319
+ break;
320
+
321
+ }
322
+
323
+ }
324
+
325
+ //$key=array_shift(array_keys(array_filter($rows,function($x) use($no){
326
+
327
+ // return $x[0]===$no;
328
+
329
+ //})));
330
+
331
+
332
+
333
+ if($key>=0){
334
+
335
+ $row=&$rows[$key];
336
+
337
+ }else{
338
+
339
+ $row=&$rows[];
340
+
341
+ }
342
+
343
+ $row[0]=$no;
344
+
345
+ $row[1]=$name;
346
+
347
+ $row[2]=$comment;
348
+
349
+ unset($row);
350
+
351
+ $fp=fopen($fname,"w");
352
+
353
+ foreach ($rows as $row) {
354
+
355
+ fputcsv($fp, $row);
356
+
357
+ }
358
+
359
+ fclose($fp);
360
+
361
+ }
362
+
363
+
364
+
365
+
366
+
367
+ if (!is_null($name) and !is_null($comment) ) {
368
+
369
+ $_SESSION["name"] =$name;
370
+
371
+ $_SESSION["comment"] =$comment;
372
+
373
+ header('Location: '.$_SERVER["SCRIPT_NAME"]);
374
+
375
+ exit;
376
+
377
+ }elseif (!is_null($no)) {
378
+
379
+ $_SESSION["no"] =$no;
380
+
381
+ header('Location: '.$_SERVER["SCRIPT_NAME"]);
382
+
383
+ exit;
384
+
385
+ }else{
386
+
387
+ $name =isset($_SESSION["name"])? $_SESSION["name"] :null;
388
+
389
+ $comment=isset($_SESSION["comment"])?$_SESSION["comment"]:null;
390
+
391
+ $no =isset($_SESSION["no"])? $_SESSION["no"] :null;
392
+
393
+ }
394
+
395
+ $h_name =htmlspecialchars($name);
396
+
397
+ $h_comment=htmlspecialchars($comment);
398
+
399
+ $h_no =htmlspecialchars($no);
400
+
401
+ ?>
402
+
403
+ <style>
404
+
405
+ .required{color:red;}
406
+
407
+ </style>
408
+
409
+
410
+
411
+ <form method="post" >
412
+
413
+ <input type="text" name="no" value="<?=$h_no;?>" readonly><br>
414
+
415
+ お名前<span class="required">【必須】</span>
416
+
417
+ <input type="text" name="name" id="name-field" required="required" value="<?=$h_name;?>"><br>
418
+
419
+ コメント<span class="required">【必須】</span><br>
420
+
421
+ <textarea name="comment" cols="30" rows="3" id="comment" required="required">
422
+
423
+ <?=$h_comment;?>
424
+
425
+ </textarea>
426
+
427
+ <input type="submit" name="s" value="投稿">
428
+
429
+ </form>
430
+
431
+
432
+
433
+ <form method="post">
434
+
435
+ 編集対象番号
436
+
437
+ <input type="number" name="no" value="<?=$h_no;?>" min="1" required>
438
+
439
+ <input type="submit" name="s" value="編集">
440
+
441
+ </form>
442
+
443
+
444
+
445
+ ```

2

ちょうせい

2019/10/04 05:10

投稿

yambejp
yambejp

スコア114784

test CHANGED
@@ -16,6 +16,8 @@
16
16
 
17
17
  $fname ="keijiban.txt";
18
18
 
19
+ if(!file_exists($fname)) die("no file"); // 追記
20
+
19
21
  if($s==="編集"){
20
22
 
21
23
  $fp=fopen($fname,"r");

1

chousei

2019/10/03 10:24

投稿

yambejp
yambejp

スコア114784

test CHANGED
@@ -36,8 +36,14 @@
36
36
 
37
37
  if(count($rec)>0){
38
38
 
39
+ /* 修正
40
+
39
41
  $rec=array_values($rec)[0];
40
42
 
43
+ */
44
+
45
+ $rec=array_shift($rec);
46
+
41
47
  $_SESSION["name"] =$rec[1];
42
48
 
43
49
  $_SESSION["comment"] =$rec[2];
@@ -64,12 +70,24 @@
64
70
 
65
71
  fclose($fp);
66
72
 
73
+ /* 修正
74
+
67
75
  $key=array_keys(array_filter($rows,function($x) use($no){
68
76
 
69
77
  return $x[0]===$no;
70
78
 
71
79
  }))[0];
72
80
 
81
+ */
82
+
83
+ $key=array_shift(array_keys(array_filter($rows,function($x) use($no){
84
+
85
+ return $x[0]===$no;
86
+
87
+ })));
88
+
89
+
90
+
73
91
  if($key>=0){
74
92
 
75
93
  $row=&$rows[$key];