Jump to content

Recommended Posts

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
)
Link to comment
https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510080
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510087
Share on other sites

$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 by gmc1103
Link to comment
https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510090
Share on other sites

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,

Link to comment
https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510097
Share on other sites

 

$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");
Link to comment
https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510108
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510119
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510122
Share on other sites

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 

Link to comment
https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510126
Share on other sites

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 

Link to comment
https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510127
Share on other sites

<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??

Link to comment
https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510135
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/295896-array-compare/#findComment-1510143
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.