Web Analytics Made Easy -
StatCounter csv form problem - CodingForum

Announcement

Collapse
No announcement yet.

csv form problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • csv form problem

    hi im using a csv file to stroe information and iv managed to make the code to display it but im having trouble making a form to edit it.

    Example of csv file contents
    optigrab;2001
    primevil;2000
    happy hour;2001

    does any one know how i can make a form which will display
    e.g optigrab and then a tex box where the year can be edited for all the lines in the csv file which can then have any changes made written to the file (it will be a smallish file 40-50 lines so it does not matter if it re-writed the whole csv file)

    thanks in advance

  • #2
    if that your file's exact formatting = name;value
    PHP Code:
    <?php
    $path 
    '/path/to/file.txt';
    $file file($path) or exit('error opening ' $path);

    if (!isset(
    $_POST['submit']))
    {
        echo 
    '<form action="" method="POST">';
        
        foreach (
    $file as $key => $value)
        {
            
    $explode explode(';'trim($value));
            
    $explode array_map('trim'$explode);
            echo 
    $explode[0] . ' = <input type="text" name="line[' $explode[0] . ']" value="' $explode[1] . '"><br>';
        }
        
        echo 
    '
        <input type="submit" name="submit" value="update">
        </form>
        '
    ;
    }
    else
    {
        
    $contents '';
        foreach (
    $_POST['line'] as $key => $value)
        {
            
    $contents .= $key ';' stripslashes(trim($value)) . "\r\n";
        }
        
        
    $bytes file_put_contents($path$contents) or exit('error writing to ' $path);
        echo 
    $bytes ' successfully written to ' $path;
    }
    ?>

    Comment


    • #3
      thanks for the code it looks like what i need i have the code to right the contents to the csv file

      <?php
      @extract($_POST);
      if(is_writable('file.csv'))
      {
      $fp = fopen('file.csv','w');
      $content = "$var1,$var2,\n";
      fwrite($fp,$content);
      fclose($fp);
      }
      else
      {
      echo'File is not writable';
      }
      ?>

      but im not to sure what the name of the varables are that are being passed on to the script could you tell me what they would be.

      thanks

      Comment

      Working...
      X