PHPを使って簡易掲示板を作成しております。
下記コードにて1行投稿は行えるのですが、
削除番号を指定して、投稿を削除できる機能がうまく書けません。
どのように書き直したらよいか、ご回答をお願いします。
lang
1コード 2<?php 3 4$dataFile = 'bbs.dat'; 5 6function h ($s) { 7 return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); 8} 9 10 11if(isset($_POST['toukou'])){ 12 13 $lines = file('bbs.dat'); 14 $cnt = count($lines); 15 $cnt += 1; 16 17 $message = $_POST['message']; 18 $user = $_POST['user']; 19 $time = date("Y/m/d H:i:s"); 20 21 22 $newData = "{番号}"."<".$cnt.">"."\t"."{名前}"."<".$user.">"."\t"."{コメント}"."\n"; 23 24 $fp = fopen($dataFile, 'a'); 25 fwrite($fp, $newData); 26 fclose($fp); 27 28 } 29 30$posts = file($dataFile, FILE_IGNORE_NEW_LINES); 31$posts = array_reverse($posts); 32 33if (isset($_POST['delete'])) { 34 for ($i = 0; $i < count($lines); $i++) { 35 $items = explode("\t", $lines[$i]); 36 if ($items[0] == $_POST['delno']) { 37 array_splice($lines, $i, 1); 38 } 39 } 40} 41 42 43?> 44 45<!DOCTYPE html> 46 <html lang="ja"> 47 <head> 48 <meta charset="UTF-8"> 49 <title>簡易掲示板</title> 50 </head> 51 <body> 52 <h1>簡易掲示板</h1> 53 <form action="" method="post"> 54 名前:<input type="text" name="user"> 55 コメント:<input type="text" name="message"> 56 <input type="submit" name="toukou" value="投稿"> 57 </form> 58 <form method="post" action=""> 59 削除指定番号:<input type="text" name="delno"> <input type="submit" name="delete" value="削除"> 60 </form> 61 <h2>投稿一覧 (<?php echo count($posts); ?>件)</h2> 62 <ul> 63 <?php if (count($posts)): ?> 64 <?php foreach ($posts as $post): ?> 65 <?php list($cnt, $user, $message, $time) = explode("\t", $post); ?> 66 <li><?php echo h($cnt); ?> <?php echo h($user); ?></li> 67 <?php endforeach ?> 68 <?php else: ?> 69 <li>まだ投稿はありません。</li> 70 <?php endif; ?> 71 </ul> 72 </body> 73 </html>
回答3件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2015/03/04 10:09
退会済みユーザー
2015/03/04 10:43
2015/03/04 11:05