PHP
1<!DOCTYPE html> 2<html lang="ja"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>keijiban.php</title> 6 </head> 7 <body> 8 <?php 9 $filename="keijiban.txt"; 10 $date=date("Y/m/d H:i:s"); 11 if(!empty($_POST["str"] && $_POST["name"]) && empty($_POST["delete"]) && empty($_POST["edit"])){ 12 $sentence = file($filename, FILE_IGNORE_NEW_LINES); 13 $number = 1; 14 if(!empty($sentence)){ 15 foreach($sentence as $lines){ 16 $pieces = explode("<>",$lines); 17 $number = $pieces[0] + 1; 18 continue; 19 } 20 } 21 $str = $number."<>".$_POST["name"]."<>".$_POST["str"]."<>".$date.PHP_EOL; 22 $fp = fopen($filename,"a"); 23 fwrite($fp,$str); 24 fclose($fp); 25 echo"書き込みました。<br><br>"; 26 } 27 if(!empty($_POST["delete"]) && empty($_POST["name"] && $_POST["str"])){ 28 $lines = file($filename, FILE_IGNORE_NEW_LINES); 29 $tmp = ""; 30 foreach($lines as $line){ 31 $pieces = explode("<>",$line); 32 if($pieces[0] != $_POST["delete"]){ 33 $tmp = $tmp.$line.PHP_EOL; 34 continue; 35 } 36 $fp = fopen($filename,"w"); 37 fwrite($fp,$tmp); 38 fclose($fp); 39 echo"削除しました。<br><br>"; 40 } 41 if(!empty($_POST["delete"]) && !empty($_POST["name"] || $_POST["str"])){ 42 echo"書き込みと削除は同時にできません。<br><br>"; 43 } 44 if(empty($_POST["delete"] && $_POST["name"] && $_POST["str"]) && !empty($_POST["change"])){ 45 $editnumber = $_POST["change"]; 46 echo"編集モードです。<br><br>"; 47 } 48 if(empty($_POST["delete"]) && !empty($_POST["name"] && $_POST["str"] && $_POST["edit"])){ 49 $correct = "<>".$_POST["name"]."<>".$_POST["str"]."<>".$date.PHP_EOL; 50 $lines = file($filename,FILE_IGNORE_NEW_LINES); 51 $tmp = ""; 52 foreach($lines as $line){ 53 $pieces = explode("<>",$line); 54 if($pieces[0] != $_POST["edit"]){ 55 $tmp = $tmp.$line.PHP_EOL; 56 } 57 else if($pieces[0] == $_POST["edit"]){ 58 $tmp = $tmp.$pieces[0].$correct; 59 } 60 continue; 61 } 62 $fp = fopen($filename,"w"); 63 fwrite($fp,$tmp); 64 fclose($fp); 65 echo "編集しました。<br><br>"; 66 } 67 ?> 68 } 69 <form> 70 <input type = "text" name = "name" placeholder = "名前" value = 71 <?php 72 if(!empty($_POST["change"])){ 73 echo $editname; 74 } 75 ?>><br> 76 <input type = "text" name = "str" placeholder = "コメント" value = 77 <?php 78 if(!empty($_POST["change"])){ 79 echo $editline; 80 } 81 ?>> 82 <button type = "submit" name = "submit">投稿</button><br> 83 <input type = "hidden" name = "edit" value = 84 <?php 85 if(!empty($_POST["change"])){ 86 echo $editnumber; 87 } 88 ?>><br> 89 <input type = "number" name = "delete" placeholder = "削除対象番号"> 90 <button type = "submit" name = "submit">削除</button><br><br> 91 <input type = "number" name = "change" placeholder = "編集対象番号"> 92 <button type = "submit" name = "submit">編集</button> 93 </form> 94 <?php 95 if(file_exists($filename)){ 96 $lines = file($filename,FILE_IGNORE_NEW_LINES); 97 foreach($lines as $line){ 98 $pieces = explode("<>",$line); 99 echo $pieces[0]."".$pieces[1]."".$pieces[2]."".$pieces[3]."<br>"; 100 } 101 } 102 ?> 103 </body> 104</html>
回答1件
あなたの回答
tips
プレビュー