如何在整个文本文件中查找重复的数据/文本/数字,并导出每个单词的重复数量

时间:2017-04-07 00:52:55

标签: php arrays

我正在尝试找到一种方法来检索从文本文件中存储的数据,特别是人们从下面的表单中提交的输入日期。然后查找日期的重复项(如果有的话),如果已经两次输入日期,我想使用“if”函数来禁用该日期,以便人们不再提交日期。

这是我的完整代码:

<?php
 $path = 'bp.txt';
 $nextmonth = date('M Y', strtotime('+1 month'));
 if (isset($_POST['field1']) && isset($_POST['field2']) && isset($_POST['field3'])) {
    $name = $_POST['field1'];
    $date = $_POST['field2'];
    $message = $_POST['field3'];
    $string = $name.' - '.$date.' - '.$message."\n\r";
    $fh = fopen($path,"a+") or die("Unable to open file!");
    fwrite($fh,$string); // Write information to the file
    fclose($fh); // Close the file

 }

//This code is for required fields. Not perfect, but works for the time being
$nameErr = $dateErr = "";
$name = $date = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["field1"])) {
    $nameErr = "<font color=\"red\">Name(s) required</font>";
  }

  if (empty($_POST["field2"])) {
    $dateErr = "<font color=\"red\">Date(s) required</font>";
  }

}


// This code is to prune the text file that saves our previous data
    if(isset($_GET['delete']))
    {
//        unlink(__FILE__);
file_put_contents($path, "");

    }
?>


<html lang="en">
    <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta charset="utf-8">

        <title>Tii Cup Time Of Request Form</title>
    </head>
     <body>


<!-- Special version of Bootstrap that is isolated to content wrapped in .bootstrap-iso -->
<link rel="stylesheet" href="https://formden.com/static/cdn/bootstrap-iso.css" />

<!--Font Awesome (added because you use icons in your prepend/append)-->
<link rel="stylesheet" href="https://formden.com/static/cdn/font-awesome/4.4.0/css/font-awesome.min.css" />

<!-- Inline CSS based on choices in "Settings" tab -->
<style>
    .bootstrap-iso .formden_header h2, .bootstrap-iso .formden_header p, .bootstrap-iso form{
        font-family: Arial, Helvetica, sans-serif; color: black}
    .bootstrap-iso form button, .bootstrap-iso form button:hover{
        color: white !important;} .asteriskField{color: red;}
    .error {color: #FF0000;}
    #wrapper form { display: none; }
         </style>
         <div class="bootstrap-iso">
              <div class="container-fluid">
  <div class="row">
       <form action="#" method="post" id="textarea" class="form-horizontal">
          Time Off Requests for <?php echo $nextmonth; ?><br>
          Name: <input type="text" name="field1"><span class="error">* <?php echo $nameErr;?></span><br>
          Date(s): <input type="text" name="field2" id="date"><span class="error">* <?php echo $dateErr;?></span><br>
            Additional Notes: <textarea name="field3" form="textarea"></textarea><br>
           <input name="submit" type="submit" value="Submit">  
           <!-- <button>Reload</button> -->
        </form>
         </div>
             </div>
         </div>


          <!-- Include jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

<!-- Include Date Range Picker -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>

<script>
    $(document).ready(function(){
        var date_input=$('input[name="field2"]'); //our date input has the name "field2"
        var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
        date_input.datepicker({
            format: 'dd',
            container: container,
            todayHighlight: true,
            multidate: true,
            multidateSeparator: ", ",
        })
    })
</script>


<table width="100%"><tr>
<td width="30%">Name(s)</td>
<td width="30%">Date(s)</td>
<td width="40%">Notes</td>

    <?php
$file = fopen($path, "r") or die("Unable to open file!");
while (!feof($file)){     
    $data = fgets($file);
list($names, $dates, $messages) = array_pad(explode(' - ', $data, 3), 3, null);
 ?>

    <tr>
        <td width="30%"><?=$names ?></td>
        <td width="30%"><?=$dates ?></td>
        <td width="40%"><?=$messages ?></td>
    </tr>
    <?php
        }
        fclose($file);
   ?>
                                   </table>
         <hr>
<h3><a font="red" href="?delete=1">Erase Data!</a></h3>                                 
     </body>

    </html>

0 个答案:

没有答案