Read 220 times | Created 2013-01-29 02:31:21 | Updated 2013-09-13 06:39:22 | | |

 

<?php  
/*********************************    
FILENAME  : flat.php    
CREATE BY  : cahya dsn    
PURPOSE   : flat database (write and read)  
CREATE DATE : 2013-01-28    
**********************************/  
$filename="dbkomentar.txt";  
//menulis data ke dalam file
if(isset($_POST['submit'])){  
  $fh=fopen($filename,'a+');  
  $dt=$_POST['nama']."|".$_POST['email']."|".nl2br(htmlentities($_POST['komentar'],ENT_HTML5,"UTF-8"))."n";  
  $w=fwrite($fh, $dt);  
  fclose($fh);  
}  
//membaca isi file
if(file_exists($filename)){  
  $data=file($filename);  
  $lines=array();  
  foreach($data as $dt){  
    $lines=explode("|",$dt);  
  }  
}  
?>  
<!DOCTYPE hmtl>  
<html>  
  <head>  
    <title>  
    </title>  
  </head>  
  <body>  
    <fieldset>  
      <legend>FORM INPUT</legend>  
      <div>  
        <?php echo isset($w)?('<b>pesan</b>:<i>'.($w!=false?'data berhasil disimpan':'data gagal disimpan').'</i>'):'';?>  
      </div>  
      <form name="frm" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">  
        <table>  
          <tr>  
            <td><label>Nama</label></td>  
            <td><input type="text" name="nama" /></td>  
          </tr>  
          <tr>  
            <td><label>Email</label></td>  
            <td><input type="text" name="email" /></td>  
          </tr>  
          <tr>  
            <td><label>Komentar</label></td>  
            <td><textarea name="komentar"></textarea></td> 
          </tr> 
          <tr> 
            <td colspan="2"> 
              <input type="submit" name="submit" value="kirim" /> 
            </td> 
          </tr> 
        </table> 
      </form> 
    </fieldset> 
    <fieldset> 
      <legend>RESULT</legend> 
      <?php 
      if(empty($lines)){ 
        echo "no data found"; 
      }else{ 
        echo "<table>n" 
             ."<tr><th>Nama</th><th>Email</th><th>Komentar</th></tr>n"; 
        foreach($lines as $dt)
        { 
          echo "<tr><td>".$dt[0]."</td><td>".$dt[1]."</td><td>".$dt[2]."</td></tr>n"; 
        } 
        echo "</table>n"; 
      } 
      ?>    
    </fieldset> 
  </body> 
</html>