質問編集履歴

2

改善した

2020/02/28 07:56

投稿

dandandannda
dandandannda

スコア6

test CHANGED
File without changes
test CHANGED
@@ -245,3 +245,271 @@
245
245
  </body>
246
246
 
247
247
  </html>
248
+
249
+
250
+
251
+
252
+
253
+ →ここから追加項目
254
+
255
+ 書き換えてみたのですがうまく作動してくれません。
256
+
257
+ また編集ボタンを押すとその番号のコメントと名前が書き込み欄に表示されるようにしたいです。
258
+
259
+ ご指導お願いします。
260
+
261
+ </body>
262
+
263
+ </html>
264
+
265
+
266
+
267
+ <html>
268
+
269
+ <html lang= "ja">
270
+
271
+ <head>
272
+
273
+ <meta charset = "UTF-8">
274
+
275
+ </head>
276
+
277
+ <body>
278
+
279
+ <!-- 投稿用フォーム -->
280
+
281
+ <form method="POST" action="mission_3-4-1.php">
282
+
283
+ <label>名前:</label>
284
+
285
+ <input type="text" name="namae" placeholder="名前" value=""<?= $ex[1] ?>""><br>
286
+
287
+ <label>コメント:</label>
288
+
289
+ <input type="text" name="name" placeholder="コメント" value=""<?= $ex[2] ?>""><br>
290
+
291
+ <input type="hidden" name="number" value="">
292
+
293
+ <input type="submit" value="送信"><br>
294
+
295
+ <input type="hidden" name="syori" value="post" />
296
+
297
+ </form>
298
+
299
+ <!-- 削除用フォーム -->
300
+
301
+ <form method="POST" action="mission_3-4-1.php">
302
+
303
+ <input type="text" name="sakuzyo" placeholder="削除対象番号"><br>
304
+
305
+ <input type="submit" value="削除"><br>
306
+
307
+ <input type="hidden" name="syori" value="delete" />
308
+
309
+ </form>
310
+
311
+ <!-- 編集用フォーム -->
312
+
313
+ <form method="POST" action="mission_3-4-1.php">
314
+
315
+ <input type="hidden" name="syori" value="edit" />
316
+
317
+ <input type="text" name="edit" placeholder="編集対象番号"><br>
318
+
319
+ <input type="submit" value="編集">
320
+
321
+ </form>
322
+
323
+
324
+
325
+ <?php
326
+
327
+ $filename='mission_3-4-1.txt';
328
+
329
+ if ((isset($_POST['edit']) && ($_POST['edit'] !== '')) {
330
+
331
+ // 編集用番号が有効なら、編集の処理を実行
332
+
333
+ $ret_array=file($filename);//ファイル読み込み
334
+
335
+ for($i=0; $i<count($ret_array);$i++){//ループ処理
336
+
337
+ $ex=explode("<>",$ret_array[$i]);
338
+
339
+ if($i==$edit-1){//投稿番号と編集番号が一致したら
340
+
341
+ $editbango=$ex[0];
342
+
343
+ $editnamae=$ex[1];
344
+
345
+ $editname=$ex[2];
346
+
347
+ }
348
+
349
+ $eded=$editbango."<>".$editnamae."<>".$editname;
350
+
351
+ }
352
+
353
+ elseif ((isset($_POST['sakuzyo']) && ($_POST['sakuzyo'] !== '')) {
354
+
355
+ // 削除用番号が有効なら、削除の処理を実行
356
+
357
+ $deletenumber = $_POST["sakujo"];
358
+
359
+ //ファイルが存在するかを確認し
360
+
361
+ if (file_exists($filename)) {
362
+
363
+ //ファイルを開き
364
+
365
+ $fp2 = fopen($filename, "a");
366
+
367
+ //ファイルを配列として読み取り
368
+
369
+ $text2 = file($filename);
370
+
371
+ ftruncate($fp2,0);
372
+
373
+ //配列を1行ずつ読み取り
374
+
375
+ foreach ($text2 as $line2){
376
+
377
+ $simpleline2 =explode ("<>", $line2);
378
+
379
+ if ($simpleline2[0] !== $deletenumber){
380
+
381
+ fwrite($fp2, $line2);
382
+
383
+ echo $simpleline2[0]." ".$simpleline2[1]." ".$simpleline2[2]." ".$simpleline2[3]."<br>";
384
+
385
+ }
386
+
387
+ fclose($fp2);
388
+
389
+ }
390
+
391
+ elseif ((isset($_POST['namae'])) && (isset($_POST['name']))) {
392
+
393
+ // 名前とコメントを受信したなら、投稿の処理を実行
394
+
395
+ $postedAt = date("Y年m月d日 H:i:s");
396
+
397
+ //ファイルの存在がある場合は投稿番号+1、なかったら1を指定する
398
+
399
+ if (file_exists($filename)) {
400
+
401
+ $lines = file($filename);
402
+
403
+ $lastline = $lines[count($lines)-1];
404
+
405
+ $lastlinearray = explode ("<>", $lastline);
406
+
407
+ $number = $lastlinearray[0] + 1;
408
+
409
+ }else {
410
+
411
+ $number = 1;
412
+
413
+ }
414
+
415
+ if (file_exists($filename)) {
416
+
417
+ //入力フォームのデータを受け取る
418
+
419
+ $name= ($_POST["namae"]);
420
+
421
+ $comment = ($_POST["namae"]);
422
+
423
+ //書き込む文字列を組み合わせた変数
424
+
425
+ $newData = $number."<>".$name."<>".$comment."<>".$postedAt;
426
+
427
+
428
+
429
+
430
+
431
+ //もしフォームが空じゃなかったら書き込みをする
432
+
433
+ if (!empty($name)||!empty($comment)){
434
+
435
+ $fp = fopen($filename,'a');
436
+
437
+ fwrite($fp, $newData."\n");
438
+
439
+
440
+
441
+ $array = file($filename);
442
+
443
+ foreach($array as $word){
444
+
445
+ $newData = explode("<>",$word);
446
+
447
+ echo "<p>",$newData[0].$newData[1].$newData[2].$newData[3],"</p>";
448
+
449
+ }
450
+
451
+ }
452
+
453
+ ?>
454
+
455
+ ///編集機能
456
+
457
+ <?php
458
+
459
+ if(empty($_POST["delete"])==false&&empty($_POST["comment"])==false&&empty($_POST["comment"])==false){//削除番号と$namaeと$namaeが空っぽじゃなかったら(
460
+
461
+ $editel=file($filename);//ファイル読み込み
462
+
463
+ $kakikomi=fopen($filename,"w");//上書き書き込み
464
+
465
+ for($i=0; $i<count($editel);$i++){//ループ処理
466
+
467
+ $plode=explode("<>",$editel);
468
+
469
+ if($i==$edit-1){//一致したら
470
+
471
+ $d=$editel === false ? 1 : count($editel)+1;//2-1の要素の数を数える
472
+
473
+ $a=$d."<>".$namae."<>".$name."<>".$date;
474
+
475
+ fwrite($kakikomi,$a.PHP_EOL);//差し替え
476
+
477
+ }//if($i==$edit-1)閉じ
478
+
479
+ else{//一致しなかったら
480
+
481
+ fwrite($kakikomi, $eded);//新規投稿
482
+
483
+ }//else閉じ
484
+
485
+ }//ループ処理閉じ
486
+
487
+ fclose($kakikomi);
488
+
489
+ }//if(empty($sakuzyo)==false&&empty($namae)==false&&empty($name)==false)閉じ
490
+
491
+ }
492
+
493
+ //編集機能終わり
494
+
495
+ ?>
496
+
497
+ <?php
498
+
499
+ //表示機能
500
+
501
+ if (!empty($name)||!empty($comment)){
502
+
503
+ $ret_array=file($filename);//テキストファイルを読み込み
504
+
505
+ for($i=0; $i<count($ret_array);$i++){//ループ処理
506
+
507
+ $ex=explode("<>",$ret_array[$i]);//投稿番号取得、それを読み込んだファイル
508
+
509
+ echo $ex[0].$ex[1].$ex[2].$ex[3]."<br>";//分解して投稿番号を取得、表示
510
+
511
+ }
512
+
513
+ }
514
+
515
+ ?>

1

初心者マーク付け忘れ

2020/02/28 07:56

投稿

dandandannda
dandandannda

スコア6

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  編集フォームを作っているのですがボタンを押しても反映されません。(送信ボタンも)
4
4
 
5
- どこがダメなのかわからないのでアドバイスお願いします!
5
+ どこがダメなのかわからないのでアドバイスよろしくお願いします!
6
6
 
7
7
 
8
8