Nihvel Nihvel - 1 year ago 48
PHP Question

Populating a multi dimensional array, dinamically, and print to a csv

I have a php page that is working but I set a limit on the rows a user can add because writing the code for each row is crazy.
I read, tested, but can't think of how to use arrays in my code.

The user must be free to add as many rows in the table as he wants, and have all of them printed on a csv file.

Please see this JSFIDDLE as it will clarify everything better.

My question is, how do I "convert" the following code, in a array-ready code?
The php part is below, and "you got the point" means that, for every row, I have to copy and paste that block N times. Same for the

content .=

$name = addslashes(strip_tags($_POST["name"]));
$surname = addslashes(strip_tags($_POST["surname"]));
$email = addslashes(strip_tags($_POST["email"]));
$telephone = addslashes(strip_tags($_POST["telephone"]));

$books0 = addslashes(strip_tags(@$_POST['books0'])) ?: '';
$tv-series0 = addslashes(strip_tags(@$_POST['tv-series0'])) ?: '';
$movies0 = addslashes(strip_tags(@$_POST['movies0'])) ?: '';
$hobbies0 = addslashes(strip_tags(@$_POST['hobbies0'])) ?: '';
$skills0 = addslashes(strip_tags(@$_POST['skills0'])) ?: '';

$books1 = addslashes(strip_tags(@$_POST['books1'])) ?: '';
$tv-series1 = addslashes(strip_tags(@$_POST['tv-series1'])) ?: '';
$movies1 = addslashes(strip_tags(@$_POST['movies1'])) ?: '';
$hobbies1 = addslashes(strip_tags(@$_POST['hobbies1'])) ?: '';
$skills1 = addslashes(strip_tags(@$_POST['skills1'])) ?: '';

/* [ You got the point ...] */

$content = "Name;$name\n";
$content .= "Surname;$surname\n";
$content .= "Email;$email\n";
$content .= "Telephone;$telephone\n\n";

//set the data of the CSV
$content .= "Books;Tv-series;Movies;Hobbies;Skills\n";
$content .= "$books0;$tv-series0;$movies0;$hobbies0;$skills0\n";
$content .= "$books1;$tv-series1;$movies1;$hobbies1;$skills1\n";
$content .= "$books2;$tv-series2;$movies2;$hobbies2;$skills2\n";
$content .= "$books3;$tv-series3;$movies3;$hobbies3;$skills3\n";
$content .= "$books4;$tv-series4;$movies4;$hobbies4;$skills4\n";
$content .= "$books5;$tv-series5;$movies5;$hobbies5;$skills5\n";

# Create the file and redirect to index page
$date = date("Ymd");
$target_dir = "C:/$name_".$date.".csv";
file_put_contents($target_dir, $content);


UPDATE: I was asked to clarify my question.
This code gets the values from the form and write them to a csv.
What if I add (from jsfiddle) many rows? I should assign many values for every row.
I wish to achieve a better result, using arrays, and possibly using less code.

With an array, I do not need to limit the rows a user can add. The opposite I am forced to do know.

So again, if someone is capable of writing an array that would match my case, this would help me a lot.
I am sorry if I do not know much of arrays or developing in general

Answer Source

Create your form fields with such names:

<input name="prefs[0][book]">
<input name="prefs[0][tv]">
<input name="prefs[0][movie]">

<input name="prefs[1][book]">
<input name="prefs[1][tv]">
<input name="prefs[1][movie]">

This will give you exactly the array format you need without any additional work. Then just dump it to CSV:

$csv = fopen("C:/{$name}_{$date}.csv", 'w');

foreach ($_POST['prefs'] as $pref) {
    fputcsv($csv, $pref);

And that's pretty much all you need to do. Don't strip tags or add slashes, fputcsv will take care of the correct escaping. Of course you may want to do some data validation or such, but I'll leave this up to you.