I need a bit of help with a simple code that I can't get to work.
The issue is that I want to get some data from a mysql database into my javascript. I know that I need to pull it via php, I have done that and placed it in an array, that I read via json_encode, it seems to get most data that way but some return undefined.
Below the code:
php
$sql_kunder = "SELECT * FROM Kunder";
$rows_kunder = array();
$result_kunder = $conn ->query($sql_kunder);
while($row_kunder = $result_kunder->fetch_assoc())
{
$rows_kunder[] = $row_kunder;
$ordre_key = $row_kunder["Kunde_id"];
$sql_ordre = "SELECT * FROM Ordre WHERE Order_Key = $ordre_key";
$result_ordre = $conn ->query($sql_ordre);
While($row_ordre = $result_ordre->fetch_assoc())
{
$rows_kunder[] = $row_ordre;
}
}
$phpArray_kunder = $rows_kunder;
That should place the 2 databases into an array (and it does)
JS
<script type="text/javascript">
var JsonKunder= <?php echo json_encode($phpArray_kunder ); ?>;
console.log(JsonKunder);
for (var key in JsonKunder)
{
if (JsonKunder[key].Kunde_id = JsonKunder[key].Order_Key)
{
console.log(JsonKunder[key].Start_dato);
console.log(JsonKunder[key].End_dato);
console.log(JsonKunder[key].Ordre_nr);
console.log(JsonKunder[key].Ordre_id);
console.log(JsonKunder[key].Kunde_navn);
console.log(JsonKunder[key].Kunde_cvr);
console.log(JsonKunder[key].Kunde_id);
}
}
</script>
output
Array[6]
0: Object
Kunde_cvr: "25659191"
Kunde_id: undefined
Kunde_navn: "Karens bix"
__proto__: Object
1: Object
End_dato: "1485561600"
Kunde_id: "1"
Order_Key: "1"
Ordre_id: "1"
Ordre_nr: "1111"
Start_dato: "1484697600"
__proto__: Object
2: Object
End_dato: "1486684800"
Kunde_id: "1"
Order_Key: "1"
Ordre_id: "2"
Ordre_nr: "1112"
Start_dato: "1485993600"
__proto__: Object
3: Object
Kunde_cvr: "65917878"
Kunde_id: undefined
Kunde_navn: "Bygmarked"
__proto__: Object
4: Object
End_dato: "1485302400"
Kunde_id: "2"
Order_Key: "2"
Ordre_id: "3"
Ordre_nr: "2222"
Start_dato: "1484870400"
__proto__: Object
5: Object
End_dato: "1487980800"
Kunde_id: "2"
Order_Key: "2"
Ordre_id: "4"
Ordre_nr: "2223"
Start_dato: "1486771200"
__proto__: Object
length: 6
__proto__: Array[0]
As you can see, there is a bit of info misplaced.
For instance, in object 0, there is a kunde_id Undefined while in object 1, there is a kunde:id = 1. Don't really know why it does that, should not be there.
Next up the for loop spitting out the console.log, it reads as follows:
1484697600 testarray.php:30 <- from ordre ( start dato )
1485561600 testarray.php:31 <- from ordre ( end dato )
1111 testarray.php:32 <- from ordre ( ordre nr )
1 testarray.php:33 <- from ordre ( ordre id )
undefined testarray.php:34 <- Should be Kunde navn
undefined testarray.php:35 <- should be kunde cvr
1 testarray.php:36 <- From kunde id
And then loops that 4 times, when I only wanted it to loop twice (number of costumers in kunde database)
Now I don't know what to do, I'm quite new to js and this is probably a simple fix... but I really need some guidance from you guys.
=is for value assignment, and==(and===) are for comparison.