tags:

views:

73

answers:

2

Im working on a php script to take an existing csv file (we'll call this one the template) and open it in a page and populate editable text fields with it's data between 1 and 50 times(user decides how many), which once edited to the users wishes, can then all be saved into a single combined csv file, separate from the template.

I included a link to a diagram of how i need this to work. I can open a csv, i can edit and save the csv, but i dont know how to dynamically take the number of clones the user needs, and render the template that many times in a way that when user is done, all fields are joined as rows of a single csv and saved to a new file.

Diagram Link

The area highlighted in red, is the area im having trouble with.

The templates contain approx 25 fields, so each instance of the template (clone) will have 25 editable fields that are populated with the original data. Im confident that i can do this myself, but i need pointed in the right direction as to how to dynamically set the number of clones of the original template, and then combine said clones as rows in a single csv file once finished.

A: 

You can name your fields as arrays:

<input type='text' name='field1[]'>
<input type='text' name='field2[]'>
...
<input type='text' name='field25[]'>

Then keep track of how many fields you have and how many rows you have:

<input type='hidden' name='rowcount' value='50'>
<input type='hidden' name='fieldcount' value='25'>

Then in the server side, $_POST['field1'] will be an array you can easily manipulate:

$fields = $_POST['fieldcount'];
$rows = $_POST['rowcount'];

$csv = array();
for($y = 0; $y < $rows; $y++) {
    $row = array();
    for($x = 0; $x < $count; $x++) {
        $row[$x] = $_POST['field' . ++$x];
    }
    $csv[] = $row;
}
// use fputcsv here.

That's a rough estimate, but you can take it from there...

Paolo Bergantino
A: 

I could be totally misunderstanding what you're trying to do but im going to guess.

Take the number of clones the user chose and loop over the editable form. Use arrays as the name of the form/fields

<?php foreach( range(1, NUMBER_OF_CLONES ) as $clone_number ): ?>
<form name="form[<?= $clone_number ?>]">
<input type="text" name="form[<?= $clone_number ?>]['name']">
</form>
<?php endif; ?>

That will display NUMBER_OF_CLONES forms.

To save it all to one file loop over the $_POST['form'] array which will have NUMBER_OF_FORMS items(forms) and create the csv.

If im completely missing your point im sorry

Galen