August 16th, 2013, 08:53 AM
-
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
August 16th, 2013, 11:00 AM
-
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.
August 16th, 2013, 11:05 AM
-
if your file is very big then you might not want to put all of the data into PHP's memory.
August 16th, 2013, 02:36 PM
-
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
August 16th, 2013, 02:42 PM
-
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.
August 16th, 2013, 03:07 PM
-
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
August 16th, 2013, 04:30 PM
-
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.
August 16th, 2013, 06:27 PM
-
How I asign a value to $csvdata and what value?
August 16th, 2013, 11:13 PM
-
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.