gmc1103 Posted April 27, 2015 Share Posted April 27, 2015 Hi I have an for examplo {1,2,3,4,5,6,7,8,9,10}and another one from a fprm {1,4,7,9,10} , i would like to compafe both and create a new one with null where there are differences.. any help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/ Share on other sites More sharing options...
blacknight Posted April 27, 2015 Share Posted April 27, 2015 http://php.net/manual/en/function.array-diff.php Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510079 Share on other sites More sharing options...
Barand Posted April 27, 2015 Share Posted April 27, 2015 try $a = [1,2,3,4,5,6,7,8,9,10]; $b = [1,4,7,9,10]; $c = array_fill_keys(array_keys(array_diff($a,$b)),null) + $a; ksort($c); result: $c = Array ( [0] => 1 [1] => [2] => [3] => 4 [4] => [5] => [6] => 7 [7] => [8] => 9 [9] => 10 ) Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510080 Share on other sites More sharing options...
gmc1103 Posted April 27, 2015 Author Share Posted April 27, 2015 Hi Thank you for your answer, i'm foloowing this example and it seems to be what i need but i'm having an issue $a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]; $c = array_fill_keys(array_keys(array_diff($a,$hora)),null) + $a; ksort($c); foreach ($c as $key => $val) { $stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(mysql_error($mysqli)); $horas = $val; echo $mysqli->error, PHP_EOL; $stmt->bind_param('ssi', $dia, $sala, $horas); if(!$stmt->execute()) echo $stmt->error; $response = "Registo gravado"; echo json_encode($response); $stmt->close(); But instead of writing to the bd the values of teh array $c....the loop is executed more than 1000 times.. This is what a i get from my debuger http://postimg.org/image/739yp0qfr/ What's wrong after the ksort? Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510087 Share on other sites More sharing options...
Barand Posted April 27, 2015 Share Posted April 27, 2015 What does $hora (or $_POST['hora']) contain? Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510088 Share on other sites More sharing options...
gmc1103 Posted April 27, 2015 Author Share Posted April 27, 2015 (edited) $hora (or $_POST['hora']) contain in this example Array[4] [0] String = "1" [1] String = "2" [2] String = "5" [3] String = "6" I have tested in php online with this code $a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]; $hora = [1,4,7,9,10]; $c = array_fill_keys(array_keys(array_diff($a,$hora)),null) + $a; ksort($c); foreach ($c as $key => $val) { echo "$val\n"; } And i receive the correct values So the problem should be here inside the loop $horas = $val; because i just get one value Thanks Edited April 27, 2015 by gmc1103 Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510090 Share on other sites More sharing options...
Barand Posted April 27, 2015 Share Posted April 27, 2015 I can't see a reason for 1000+ writes in the code posted. Is that code inside another loop? Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510096 Share on other sites More sharing options...
gmc1103 Posted April 27, 2015 Author Share Posted April 27, 2015 Hi Barand Thanks again for your help I have fixed now, the code is now like this <?php header('Content-type: application/json'); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); $mysqli = new mysqli('localhost', 'root', '', 'ebspma'); // Works as of PHP 5.2.9 and 5.3.0. if ($mysqli->connect_error) { die('Connect Error: ' . $mysqli->connect_error); } $num= $_POST['num']; $dia = $_POST['dia']; $sala = $_POST['sala']; $hora = explode(",", $_POST['hora']); $a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]; $c = array_fill_keys(array_keys(array_diff($a,$hora)),null) + $a; ksort($c); for ($i=0; $i<17; $i++){ $stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(mysql_error($mysqli)); $horas = $c[$i]; echo $mysqli->error, PHP_EOL; $stmt->bind_param('ssi', $dia, $sala, $horas); if(!$stmt->execute()) echo $stmt->error; $response = "Registo gravado"; echo json_encode($response); $stmt->close(); } I'm sure the code can be improved I just have another small issue I have no json_encode response in my javascript, can you see why? <script> function postData(){ var dia = document.getElementById('dia').value; var sala = document.getElementById('sala').value; var tempos = []; var s = document.getElementById('hora'); for (var i = 0; i < s.options.length; i++) { if (s.options[i].selected == true) { var valores = s.options[i].value; tempos.push(valores); } } console.log(tempos); var num = document.getElementById('num').value; var data = 'dia='+ dia + '&sala='+ sala + '&hora='+ tempos + '&num='+ num; $.ajax({ type: "POST", url: "registerBd.php", data: data, cache: false, success: function(data) { y = data.y; console.log(y); document.getElementById('ajaxDivOk').style.display = "block"; document.getElementById('ajaxDivOk').innerHTML = "Registo(s) gravado(s) com sucesso"; alert("Form submitted successfully.\nReturned json: " + y ); } }); } </script> Thanks again for your help, Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510097 Share on other sites More sharing options...
Barand Posted April 27, 2015 Share Posted April 27, 2015 $response = "Registo gravado"; echo json_encode($response); Why json_encode plain text? As you are already using jquery, why use getElementById() and not $('#ajaxDivOk').css("display", "block"); $('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso"); Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510108 Share on other sites More sharing options...
gmc1103 Posted April 27, 2015 Author Share Posted April 27, 2015 Hi Barnad With this code <?php header('Content-type: application/json'); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); $mysqli = new mysqli('localhost', 'root', '', 'ebspma'); // Works as of PHP 5.2.9 and 5.3.0. if ($mysqli->connect_error) { die('Connect Error: ' . $mysqli->connect_error); } $num= $_POST['num']; $dia = $_POST['dia']; $sala = $_POST['sala']; $hora = explode(",", $_POST['hora']); $a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]; $c = array_fill_keys(array_keys(array_diff($a,$hora)),null) + $a; ksort($c); for ($i=0; $i<17; $i++){ $stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(mysql_error($mysqli)); $horas = $c[$i]; echo $mysqli->error, PHP_EOL; $stmt->bind_param('ssi', $dia, $sala, $horas); if(!$stmt->execute()) echo $stmt->error; $stmt->close(); } and this function <script> function postData(){ var dia = document.getElementById('dia').value; var sala = document.getElementById('sala').value; var tempos = []; var s = document.getElementById('hora'); for (var i = 0; i < s.options.length; i++) { if (s.options[i].selected == true) { var valores = s.options[i].value; tempos.push(valores); } } console.log(tempos); var num = document.getElementById('num').value; var data = 'dia='+ dia + '&sala='+ sala + '&hora='+ tempos + '&num='+ num; $.ajax({ type: "POST", url: "registerBd.php", data: data, cache: false, success: function(data) { $('#ajaxDivOk').css("display", "block"); $('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso"); } }); return false; } </script> Nothing is show...in my div...what i missing?? Regards and many thanks Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510119 Share on other sites More sharing options...
Barand Posted April 27, 2015 Share Posted April 27, 2015 Your PHP doesn't echo anything to go into the response (unless you have a query error). Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510120 Share on other sites More sharing options...
gmc1103 Posted April 27, 2015 Author Share Posted April 27, 2015 No the query is working because the data is being inserted into the db. using echo "Gravado"; My function is not getting the echo $.ajax({ type: "POST", url: "registerBd.php", data: data, cache: false, dataType: 'html', success: function(data) { var dataR = data; console.log(data); console.log(dataR); $('#ajaxDivOk').css("display", "block"); $('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso"); } }); the data variable doesn't receive anything....the console log is empty...i'm confused Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510122 Share on other sites More sharing options...
Barand Posted April 27, 2015 Share Posted April 27, 2015 You removed the echo. It used to be echo json_encode($response); So it should still echo $response; Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510123 Share on other sites More sharing options...
gmc1103 Posted April 27, 2015 Author Share Posted April 27, 2015 Hi Barand Sorry...not even with this, i have changed the php...to <?php header('Content-type: application/json'); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); $mysqli = new mysqli('localhost', 'root', '', 'ebspma'); // Works as of PHP 5.2.9 and 5.3.0. if ($mysqli->connect_error) { die('Connect Error: ' . $mysqli->connect_error); } $num= $_POST['num']; $dia = $_POST['dia']; $sala = $_POST['sala']; $hora = explode(",", $_POST['hora']); $a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]; $c = array_fill_keys(array_keys(array_diff($a,$hora)),null) + $a; ksort($c); for ($i=0; $i<17; $i++){ $stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(mysql_error($mysqli)); $horas = $c[$i]; echo $mysqli->error, PHP_EOL; $stmt->bind_param('ssi', $dia, $sala, $horas); if($stmt->execute()){ $response = "Registo gravado"; echo ($response); } else{ echo $stmt->error; } $stmt->close(); } and the javascript is $.ajax({ type: "POST", url: "registerBd.php", data: data, cache: false, dataType:'html', success: function(data) { var dataR = data; console.log(dataR); console.log(data); $('#ajaxDivOk').css("display", "block"); $('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso"); } }); Nothing is show...not even in console Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510126 Share on other sites More sharing options...
gmc1103 Posted April 27, 2015 Author Share Posted April 27, 2015 Hi Barand Sorry...not even with this, i have changed the php...to <?php header('Content-type: application/json'); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); $mysqli = new mysqli('localhost', 'root', '', 'ebspma'); // Works as of PHP 5.2.9 and 5.3.0. if ($mysqli->connect_error) { die('Connect Error: ' . $mysqli->connect_error); } $num= $_POST['num']; $dia = $_POST['dia']; $sala = $_POST['sala']; $hora = explode(",", $_POST['hora']); $a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]; $c = array_fill_keys(array_keys(array_diff($a,$hora)),null) + $a; ksort($c); for ($i=0; $i<17; $i++){ $stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(mysql_error($mysqli)); $horas = $c[$i]; echo $mysqli->error, PHP_EOL; $stmt->bind_param('ssi', $dia, $sala, $horas); if($stmt->execute()){ $response = "Registo gravado"; echo ($response); } else{ echo $stmt->error; } $stmt->close(); } and the javascript is $.ajax({ type: "POST", url: "registerBd.php", data: data, cache: false, dataType:'html', success: function(data) { var dataR = data; console.log(dataR); console.log(data); $('#ajaxDivOk').css("display", "block"); $('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso"); } }); Nothing is show...not even in console Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510127 Share on other sites More sharing options...
gmc1103 Posted April 27, 2015 Author Share Posted April 27, 2015 Sorry.....duplicate And i have echo json_encode($response); Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510128 Share on other sites More sharing options...
Barand Posted April 27, 2015 Share Posted April 27, 2015 Is the div visible? Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510133 Share on other sites More sharing options...
gmc1103 Posted April 27, 2015 Author Share Posted April 27, 2015 <tr> <td colspan="2" align="center" valign="middle" class="tg-6997"> <div class="col-sm-6"> <div id="ajaxDivOk" style="display:none" class="alert alert-success"></div> </div> </td> </tr> Well...now or i'm stupid or i don't see anything In netbeans if i run normally....nothing is showed... In netbeans if i run in debug mode...the div comes to light... check the image in debug mode http://postimg.org/image/qf6huodjb/ What's is going wrong??, does the main page do a quick refresh or something else?? Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510135 Share on other sites More sharing options...
gmc1103 Posted April 27, 2015 Author Share Posted April 27, 2015 Well after some experience i getting nut... i receive the error alert in my function but the data is inserted into database <script> function postData(){ var dia = document.getElementById('dia').value; var sala = document.getElementById('sala').value; var tempos = []; var s = document.getElementById('hora'); for (var i = 0; i < s.options.length; i++) { if (s.options[i].selected == true) { var valores = s.options[i].value; tempos.push(valores); } } console.log(tempos); var num = document.getElementById('num').value; var data = 'dia='+ dia + '&sala='+ sala + '&hora='+ tempos + '&num='+ num; $.ajax({ type: "POST", dataType: "json", url: "registerBd.php", data: data, cache: false, success: function () { console.log("Aqui"); $('#ajaxDivOk').css("display", "block"); $('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso"); }, error:function(){ console.log("Aqui 2"); alert("something went wrong"); } }); return false; } </script> So...is not receiving the json_encode from my php....but why? <?php header('Access-Control-Allow-Origin: *'); header('Content-type: application/json'); ini_set('max_execution_time', 300); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); $mysqli = new mysqli('localhost', 'root', '', 'ebspma'); // Works as of PHP 5.2.9 and 5.3.0. if ($mysqli->connect_error) { die('Connect Error: ' . $mysqli->connect_error); } $num= $_POST['num']; $dia = $_POST['dia']; $sala = $_POST['sala']; $hora = explode(",", $_POST['hora']); $a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]; $c = array_fill_keys(array_keys(array_diff($a,$hora)),null) + $a; ksort($c); for ($i=0; $i<17; $i++){ $horas = $c[$i]; $stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(mysql_error($mysqli)); $stmt->bind_param('ssi', $dia, $sala, $horas); if(!$stmt->execute()) echo $stmt->error; $stmt->close(); } $response = "Registo gravado"; echo json_encode($response); ?> Any help?? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510143 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.