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
.
问题是我想从mysql数据库中获取一些数据到我的javascript中。我知道我需要通过php拉它,我已经完成了并将它放在一个数组中,我通过json_encode读取,它似乎以这种方式获得大多数数据,但有些返回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)
这应该将2个数据库放入一个数组(它确实如此)
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.
例如,在对象0中,有一个kunde_id未定义,而在对象1中,有一个kunde:id = 1.不知道它为什么这样做,不应该在那里。
Next up the for
loop spitting out the console.log
, it reads as follows:
接下来,for循环吐出console.log,其内容如下:
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)
然后循环4次,当我只想循环两次时(kunde数据库中的客户数量)
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.
现在我不知道该怎么做,我对js很新,这可能是一个简单的修复......但我真的需要你们的一些指导。
2 个解决方案
#1
2
You could try to update if statement to
您可以尝试将if语句更新为
if (JsonKunder[key].Kunde_id == JsonKunder[key].Order_Key)
= is an assignment. It's not compare operator.
=是一项任务。它不是比较运算符。
#2
0
Well that was a embarssing mistake, how ever it didn't solve my issue ( but did get me a bit closer i think )
那是一个令人尴尬的错误,但它怎么都没能解决我的问题(但我觉得这让我更接近了)
what my issue is, is that i can't seem to get the IF to run when i expect it to run..
我的问题是,当我期望它运行时,我似乎无法运行IF。
my new code is like this
我的新代码是这样的
var i = 0 /* this is to see loops */
for (var key in JsonKunder) {
var k_id = JsonKunder[key].Kunde_id;
var o_key = JsonKunder[key].Order_Key;
i++
console.log('Counts loops ' + i );
console.log('kunde id ' + k_id );
console.log('ordre key ' + o_key );
if( k_id == o_key )
{
console.log('in if ');
}
}
console.log('total loops ' +i );
But it never returns any thing in the if loop, so it dosn't do what i thought it would... I know i have 6 obj. in my array, 4 ordres and 2 costumers. i thought i could tell in the If loop, that when a costumer ( k_id ) matches a Orders ( o_key ) then it should return the info in the if loop X times until they k_id and o_key no longer match.
但它永远不会返回if循环中的任何东西,所以它不会做我想的那样...我知道我有6个obj。在我的阵列中,4个ordres和2个客户。我想我可以在If循环中判断,当一个客户(k_id)匹配一个Orders(o_key)时,它应该在if循环中返回X次信息,直到它们k_id和o_key不再匹配。
But it do not do that... so i am a noob and can't seam to sort the data in the array like i want to.
但它没有这样做...所以我是一个菜鸟,不能接缝来排序数组中的数据,就像我想要的那样。
#1
2
You could try to update if statement to
您可以尝试将if语句更新为
if (JsonKunder[key].Kunde_id == JsonKunder[key].Order_Key)
= is an assignment. It's not compare operator.
=是一项任务。它不是比较运算符。
#2
0
Well that was a embarssing mistake, how ever it didn't solve my issue ( but did get me a bit closer i think )
那是一个令人尴尬的错误,但它怎么都没能解决我的问题(但我觉得这让我更接近了)
what my issue is, is that i can't seem to get the IF to run when i expect it to run..
我的问题是,当我期望它运行时,我似乎无法运行IF。
my new code is like this
我的新代码是这样的
var i = 0 /* this is to see loops */
for (var key in JsonKunder) {
var k_id = JsonKunder[key].Kunde_id;
var o_key = JsonKunder[key].Order_Key;
i++
console.log('Counts loops ' + i );
console.log('kunde id ' + k_id );
console.log('ordre key ' + o_key );
if( k_id == o_key )
{
console.log('in if ');
}
}
console.log('total loops ' +i );
But it never returns any thing in the if loop, so it dosn't do what i thought it would... I know i have 6 obj. in my array, 4 ordres and 2 costumers. i thought i could tell in the If loop, that when a costumer ( k_id ) matches a Orders ( o_key ) then it should return the info in the if loop X times until they k_id and o_key no longer match.
但它永远不会返回if循环中的任何东西,所以它不会做我想的那样...我知道我有6个obj。在我的阵列中,4个ordres和2个客户。我想我可以在If循环中判断,当一个客户(k_id)匹配一个Orders(o_key)时,它应该在if循环中返回X次信息,直到它们k_id和o_key不再匹配。
But it do not do that... so i am a noob and can't seam to sort the data in the array like i want to.
但它没有这样做...所以我是一个菜鸟,不能接缝来排序数组中的数据,就像我想要的那样。