#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0

    Display csv as form, edit and save


    Hello,

    I have a form that reads a csv file:

    Code:
    ?php $fp = fopen('ventes.csv','r+') or die("can't open file");?> <form action="store.php" method="post"> <?php while($csv_line = fgetcsv($fp,1024)) {     for ($i = 0, $j = count($csv_line); $i < $j; $i++) {         print '<input name="txt_form" type="text" value="'.$csv_line[$i].'" style="width:300px"/><br>';     } } print '<input name="Soumettre" type="submit" /> </form></form>'; $myArray =array (utf8_decode($_POST['text'])); fputcsv($fp, $myArray, ";"); fclose($fp) or die("can't close file"); ?>
    I would, at first, that each field (separated by a is displayed in a separate input. Secondly, I would like to modify an input and save the entire form in the same csv file.

    Currently my backup code does not record anything.

    Code:
    <?php $fn = $_POST['txt_form']; $cvsData = $fn; $fp = fopen("ventes.csv","a"); if($fp){ fwrite($fp,$cvsData);  fclose($fp);  } ?>
    Can you help me?

    thank you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    PHP has some built in functions for handling csv files, fgetcsv and fputcsv.

    fgetcsv will take a file handle and some other details about the csv file (such as the delimiter, which you'll need to specify since it defaults to comma, not semicolon) and returns a two-dimensional array of the csv data broken into rows and columns.

    Once you've got this array, just loop through it, creating an input for each item in the array, something maybe like this:
    PHP Code:
    $csvdata fgetcsv(fopen("csvfile.csv""r"), 0';');
    echo 
    "<table>";
    foreach (
    $csvdata as $rownum=>$row){
      echo 
    "<tr>";
      foreach(
    $row as $colnum=>$col){
        echo 
    "<td><input name=csvdata[$rownum][$colnum] value="$col"></td>";
      }
      echo 
    "</tr>";
    }
    echo 
    "</table>"
    That form should produce an array in $_POST["csvdata"] that can be fed (perhaps with a little massaging) to fputcsv().

    I'm oversimplifying things a bit here, as you'll need to spend some time making sure your data is properly escaped/sanitized, but this is the general idea of what needs to be done.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    if your file is very big then you might not want to put all of the data into PHP's memory.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0
    I try this but it telling me "Warning: Invalid argument supplied for foreach()"

    Code:
    <form action="store.php" method="post"> <?php $file = fopen('ventes.csv', 'r'); while (($line = fgetcsv($file)) !== FALSE) { 	echo "<table>";  	foreach ($csvdata as $rownum=>$row){ 		  echo "<tr>";   foreach($row as $colnum=>$col){    //$line is an array of the csv elements   print '<input name="txt_form" type="text" value="'.$csv_line[$i][$i].'" style="width:300px"/><br>';   echo "</tr>"; } echo "</table>";  } } fclose($file); ?> </form>
    Thanks
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    My code was very barebones, you will want to check that the file was sucessfully opened and that fgetcsv has pulled data from it successfully. Probably this failed for some reason and thus $csvdata is empty.

    I find it difficult to parse through your code when it's all on one line like that. If you want more detailed help, it would be better to put it between PHP tags and make sure it's showing up on multiple lines.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0
    Here is my code in php tag (I'm sorry, it is the first time on this forum and I don't know the tage I need to use).

    The form:
    [PHPNET=""]
    <form action="store.php" method="post">
    <?php $file = fopen('ventes.csv', 'r');
    while (($line = fgetcsv($file)) !== FALSE) {
    echo "<table>";
    foreach ($csvdata as $rownum=>$row){
    echo "<tr>";
    foreach($row as $colnum=>$col){
    //$line is an array of the csv elements
    print '<input name="txt_form" type="text" value="'.$csv_line[$i][$i].'" style="width:300px"/><br>';
    echo "</tr>"; }
    echo "</table>"; } }
    fclose($file); ?>
    </form>[/PHPNET]

    Store.php:
    [PHPNET=""]
    <?php $fn = $_POST['csvdata'];
    $cvsData = $fn;
    $fp = fopen("ventes.csv","a");
    if($fp){
    echo $cvsData;
    fclose($fp); } ?>[/PHPNET]

    Thanks
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Originally Posted by eiffel74
    Here is my code in php tag (I'm sorry, it is the first time on this forum and I don't know the tage I need to use).
    You just need to put your code between these tags, without the spaces in the brackets:

    [ PHP ]
    (your code goes here)
    [ /PHP ]

    It looks like you never assign $csvdata a value.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0
    How I asign a value to $csvdata and what value?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Originally Posted by eiffel74
    How I asign a value to $csvdata and what value?
    The answer to both of those questions is already in my first post. Study it and see if you can come up with that answer on your own.

IMN logo majestic logo threadwatch logo seochat tools logo