0

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.

1
  • = is for value assignment, and == (and ===) are for comparison. Commented Jan 22, 2017 at 15:25

2 Answers 2

2

You could try to update if statement to

if (JsonKunder[key].Kunde_id == JsonKunder[key].Order_Key)

= is an assignment. It's not compare operator.

Sign up to request clarification or add additional context in comments.

Comments

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

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.

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 Comment

I have after a bit of sleep and some time away .. realised my stupid mistake, that is, that the array will never get the K_id and the O_key in the same loop, i have build the array wrong form the start. Thanks for the help though with the = and == and === :) and sorry for the wasted time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.