I read this other question too: Missing index in a multidimensional array PHP
and I searched for missing keys and there are not.
My goal is to create a csv file with the information inside the arrays. I'm able to do that when I am processing it in the same server, but due to upgrade in the environment and division of the csv files, I must use a second server therefore the CURL request.
The problem is that I am able to call the receiver.php
and this, in fact, creates the file.csv , but this file is empty (0 bytes).
And the only Log I have is the one below..
Please find below the result of the php after it ran:
Array
(
[0] => Array
(
[0] => name
[1] => frank
)
[1] => Array
(
[0] => address
[1] => none
)
[2] => Array
(
[0] => email
[1] => some@email
)
[3] => Array
(
[0] => job
[1] => none
)
[4] => Array
(
[0] =>
)
[5] => Array
(
[0] => options
[1] => status
[2] => link
[3] => time
[4] => note
[5] => check
)
[6] => Array
(
[options] => none
[status] => active
[link] => http://something
[time] => 2018-01-11 10:00
[note] => none
[check] => OK
)
)
Array key exist for 0
Array key exist for 1
Array key exist for 2
Array key exist for 3
Array key exist for 4
Array key exist for 5
Array key exist for 6
I have a form with many <input>
fields; 4 of them are static (name, email etc), the others are generated each time the user presses a button.
At each click a row appears and the user can enter more data.
This data's <input name=...>
is:
at row0:
name='prefs[0][options]'
name='prefs[0][status]'
name='prefs[0][link]'
name='prefs[0][time]'
name='prefs[0][note]'
name='prefs[0][check]'
If the user clicks the button, the row appears in the table and the new <input> fields will be:
name='prefs[1][options]'
name='prefs[1][status]'
name='prefs[1][link]'
name='prefs[1][time]'
name='prefs[1][note]'
name='prefs[1][check]'
.. and so on.
When the form is submitted, the sender.php
processes the data.
I will have 2 arrays:
A - $list, which contain the static fields
B - $_POST['prefs'] containing each row
I merge the 2 arrays together:
$final_array = array_merge($list, $_POST['prefs']);
The log says:
PHP Notice: Undefined index: fields_string in /var/www/report/receiver.php on line 4
PHP Warning: Invalid argument supplied for foreach() in /var/www/report/receiver.php on line 4
sender.php
<?php
if (isset($_POST['submit'])){
require_once('config.php');
$name = addslashes(strip_tags($_POST["name"]));
$address = addslashes(strip_tags($_POST["address"]));
$email = addslashes(strip_tags($_POST["email"]));
$job = addslashes(strip_tags($_POST["job"]));
$list = array (
array('name', "$name"),
array('address', "$address"),
array('email', "$email"),
array('job', "$job"),
array(''),
array('options', 'status', 'link','time', 'note','check' )
);
// Merge the 2 arrays and split to variables
$final_array = array_merge($list, $_POST['prefs']);
echo '<pre>'; print_r($final_array); echo '</pre>';
sleep(2);
// Check for missing keys
$firstkey = key($final_array); // get first index of array
end($final_array);
$lastkey = key($final_array); // get last index of array
for($i = $firstkey;$i <= $lastkey;$i++) {
if(!array_key_exists($i,$final_array)) { // check key exist or not
echo "Missing Array key is ".$i."<br/>";
} else echo "Array key exist for ".$i."<br>";
}
$fields_string = "";
foreach($final_array as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
// Send through CURL
$ch = curl_init('http://192.168.10.10/receiver.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
header("Location:index.php");
exit;
}
?>
receiver.php
<?php
$csv = fopen( "/home/user/file.csv", 'w+' );
foreach ($_POST["fields_string"] as $fields) {
fputcsv($csv, $fields, ";");
}
fclose($csv);
?>