Witam, mam taki kod, problem mam taki, że echo json_encode wykonuje się kilka razy, drugi problem, to nie wiem jak porównać tablice, chodzi o to, żeby w obu tablicach było dokładnie to samo, ni mniej ni więcej. Jak w tablicy a mam ['x','y'] w tablicy b mam ['y','z'] to w wyniku chciałbym mieć ['x','y','z']
<?php
$array = $_POST['list'];
$userID = $_SESSION['log'];
$result = null;
function getData($userID, $array){
function compare($a, $b){
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
try{
$stmt = null;
$conn = new PDO("mysql:host=localhost;dbname=list", 'root', '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->query("SELECT noteText FROM notes WHERE userID='$userID'");
$row = $stmt->fetch();
$result = array_diff_uassoc($row, $array, 'compare');
foreach ($result as $note) {
$stmt = $conn->prepare("INSERT INTO notes (noteText, userID) VALUES (:note, '$userID')");
$stmt->execute(array(':note' => $note));
}
echo json_encode
($result); $conn = null;
}
catch(PDOException $e)
{
return false;
$e->getMessage();
}
}
getData($userID,$array);
?>
A tu kod js
function synchronize(){
var listArray = [];
if (typeof li[0] != 'undefined') {
for (let i = 0; i< li.length; i++){
listArray[i] = li[i].textContent;
}
$.post(
"scripts/synchronize.php",
{
list: listArray
},
function(data){
let a = Object.keys(data).map(function(key) { return data });
alert(a);
}
);
}
}
Ten post edytował Dukov 2.12.2017, 12:56:33