0
Good afternoon, you guys. I’m trying to pass an array that retrieves a JSON to the BD, but I’m not able to identify the objects of the Array, I think I’m doing something wrong, I did some tests with foreach but I was not successful, follow the Cod’s.
HTML
<?php include('header.php'); ?>
<script type="text/javascript" src="script/script.js"></script>
<title>DataTable</title>
<style>
tfoot input {
width: 100%;
padding: 0;
box-sizing: border-box;
color: #FFFFFF;
}
tfoot {
display: table-footer-group;
}
</style>
<?php include('container.php'); ?>
<div class="container">
<h2>DataTable</h2>
<div class="row">
<table id="example" class="display" width="100%" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>Data</th>
<th>Horário</th>
<th>Nº Telefone</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th class="selectedDate" id="1">Data</th>
<th></th>
<th></th>
</tr>
</tfoot>
</table>
</div>
</div>
<?php include('footer.php'); ?>
script js.
// _START_ DataTable
var table = $('#example').DataTable({
"bProcessing": true,
"sAjaxSource": "data.php",
"bPaginate": true,
"sPaginationType": "full_numbers",
"iDisplayLength": 5,
"autoWidth": true,
"aoColumns": [
{mData: [0]},
{type: 'date-br',
targets: 1},
{ "orderable": false, "targets": 2 }, // remove a ordenação
{ "orderable": false, "targets": 3 },
]
});
// função para retornar o dados da tela em json
setInterval(function () {
var table3 = $('#example').tableToJSON();
var request = $.ajax({
method: "POST",
url: "teste.php",
data: { array: table3},
dataType: "html"
})
request.done(function(resposta) {
//resposta servidor
console.log(resposta)
});
}, 6000); // está em segundos
php test.
<?php
$array = ($_POST['array']);
print_r($array);
Array
Array
(
[0] => Array
(
[ID] =>
[Data] =>
[Horário] =>
[Nº Telefone] =>
)
[1] => Array
(
[ID] => 8010
[Data] => 27/12/2017
[Horário] => 14:58:27
[Nº Telefone] => 1231530337
)
[2] => Array
(
[ID] => 8010
[Data] => 27/12/2017
[Horário] => 14:56:52
[Nº Telefone] => 1231530337
)
)
Need to do a foreach on
$array
then access the keys you want.– rray
I had done it before but I ended up passing wrong value and I thought it was not going right, I reviewed the structure again and it worked .-.
foreach($array as $key => $value)
{
 var_dump($key." - ".$value["ID"]);
 var_dump($key." - ".$value["Data"]);
 var_dump($key." - ".$value["Horário"]);
 var_dump($key." - ".$value["Nº Telefone"]);
}
– Diogo Moura
RESULT
string(4) "0 - "
string(4) "0 - "
string(4) "0 - "
string(4) "0 - "
string(8) "1 - 8010"
string(14) "1 - 27/12/2017"
string(12) "1 - 14:58:27"
string(14) "1 - 1231530337"
string(8) "2 - 8010"
string(14) "2 - 27/12/2017"
string(12) "2 - 14:56:52"
string(14) "2 - 1231530337"
string(8) "3 - 8010"
string(14) "3 - 27/12/2017"
string(12) "3 - 10:53:14"
string(15) "3 - 11993229733"
in this case I can already do the data Insert in the database ? Thank you– Diogo Moura
@Diogomoura Da to see nothing here in the comments, better edit your question.
– Francisco
@Diogomoura, I studied this Postgresql, I created a database with a table links but it was missing to create the table callcent_queuecalls so I can test. If you can provide sql to create this table and a
ìnsert
with the data, would save me time because I will have to study the types of data to stay in line. However, I edited my reply by publishing the test.php page that is working round.– user60252
I just updated GIT with the Insert, you can send me the SQL of the table you created please, I tried to insert here with this code that you updated in my test database and it didn’t work, it may be that my test comic is wrong, thanks.
– Diogo Moura
As I don’t know the Postgresql data types yet, I created 4 fields, Cod, date, time and phone all type
character(15)
. I changed the array in the answer and I believe it stayed as you put in your question.– user60252
Create a test table with 4 fields
cod, data, hora e telefone
and run my answer code to see if you enter.– user60252
@Leocaracciolo I had done the tests before with the manual array and they insert normally, I again with your Cód and ta OK sent to the database, but then when I pull the data from my table as array it n sends to the database and the array is equal to your Cod gave a
print_r
and the structure is the same as what is in my question, I put to take the data for a certain time in the script, is it not activating my test.php page that does the sending of data to the BD ? at least theprint_r
php page is returning in the console.– Diogo Moura
@Leocaracciolo CONSEGUI!! I did the following:,
$array = ($_POST['array']);
 $json_e = json_encode($array, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
 $json_d = json_decode($json_e, true);
 print_r ($json_d); //teste
– Diogo Moura
Beauty, success there for you!!!
– user60252
@Leocaracciolo thanks a lot for the help, success for you too.
– Diogo Moura