CSVデータを表示する
サンプル
testData.csv
Yes, Mike, Thank you! Yes, Gin, test comment... No, Mika, No Thank you, mate... Yes, Tom, AIUEO
ShowCsvData.php
<?php header("Content-Type:text/html;charset=Shift_JIS"); header("Content-Language:ja"); $fileName = "./testData.csv"; if (file_exists($fileName)) { $fp = fopen($fileName, "r"); echo "<html><body>"; while (!feof($fp)) { $line = fgets($fp); $dataSet = explode(", ", $line); if (strcasecmp($dataSet[0], "No") != 0) { echo $dataSet[1]; echo "<br />"; echo $dataSet[2]; echo "<br />"; } } echo "</body></html>"; fclose($fp); ?php>
CSVデータを更新する
サンプル
EditCsvData.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=shift_jis" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <script language="javascript"> function checkData() { if (document.getElementById("ShowData").value == "") { alert("表示するかどうかを「Yes」または「No」で入力して下さい"); document.getElementById("custmerName").focus(); return false; } else if (document.getElementById("Name").value == "") { alert("名前を入力して下さい"); document.getElementById("Name").focus(); return false; } else if (document.getElementById("Comment").value == "") { alert("コメントを入力して下さい。"); document.getElementById("Comment").focus(); return false; } return true; } </script> </head> <body> <form id="form" name="form" method="post" action="EditCsvData.php"> <p>Show Data *</p><input type="text" name="ShowData" id="ShowData" /> <p>Name *</p><input type="text" name="Name" id="Name" /> <p>Comment *</p><textarea name="Comment" id="Comment" cols="30" rows="20"/></textarea> <button type="submit" onclick="return checkData()">Edit</button> </form> </body> </html>
EditCsvData.php
$fileName = "./testData.csv"; if (!file_exists($fileName)) { header("Location:error.html"); exit(); } // 登録データ取得 $showData = htmlspecialchars($_POST["ShowData"]); $name = nl2br(htmlspecialchars($_POST["Name"])); $comment = nl2br(htmlspecialchars($_POST["Comment"])); $comment = ereg_replace("\r|\n", "", $comment); if (get_magic_quotes_gpc()) { $comment = stripslashes($comment); } $contents = "$showData,\t$name,\t$comment,\t\n"; $dataSet = file($fileName); array_splice($dataSet, $index, 1, $contents); // データ更新 $updateData = join("", $dataSet); // データ更新 file_put_contents($fileName, $updateData); $allData = rtrim(file_get_contents($fileName), "\n"); // 最後の改行削除 file_put_contents($fileName, $allData); // 最後の改行削除を削除した状態で再度保存 header("Location:ShowCsvData.php"); ?>
error.html
<html> <body> <p>エラー</p></body> </html>
CSVデータの一部を削除する
サンプル
DeleteCsvData.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=shift_jis" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <script language="javascript"> function checkData() { if (document.getElementById("line").value == "") { alert("削除するデータを入力して下さい(0番から)"); document.getElementById("line").focus(); return false; } return true; } </script> </head> <body> <form id="form" name="form" method="post" action="EditCsvData.php"> <p>削除するデータを入力して下さい(0番から。半角数字で) *</p><input type="text" name="line" id="line" /> </textarea> <button type="submit" onclick="return checkData()">Edit</button> </form> </body> </html>
DeleteCsvData.php
<?php $fileName = "./testData.csv"; $index = $_GET["line"]; if ($index < 0 || !file_exists($fileName)) { header("LOCATION:error.html"); exit(); } $dataSet = file($fileName); $deletedData = array_splice($dataSet, $index, 1); $uploadData = join("", $dataSet); $fp = fopen($fileName, "w"); if (flock($fp, LOCK_EX)) { fwrite($fp, $uploadData); flock($fp, LOCK_UN); } else { fclose($fp); header("LOCATION:error.html"); exit(); } fclose($fp); ?>