-1

I have 2 php Files. [Insert.php & Output.php]

Insert.php: Catch 2 Dates and send it to the Output.php

Output.php: The 2 Dates go to an mysql Query. The result i'll save to an array.

while($myArray = mysqli_fetch_array($sqlresult)) {}

Now i want use this Array (myArray) in an separate js file to color the results.

 while($myArray = mysqli_fetch_array($sqlresult)) { 
  echo $myArray["res1"];
  echo "<script type='text/javascript'> 
  var stats = new Array(";
  echo '"' . implode('","', $myArray) . '");';
  echo "\n</script>";}

in the js file i have:

for (var x = 0; x < stats.length; x++) {

    $(".class#" + stats[x]).css("background-color", "#e74c3c");
    $(".class2#" + stats[x]).css("background-color", "#e74c3c");
    alert(stats);
}

In my SQL Table i have insert 3 cols. [a1, a2 and a3]. He should color me this 3 ids [a1, a2, a3]. But he only take the first and alert me 2 times:

a1,a1

What i made wrong?

user3676560
  • 151
  • 2
  • 15

2 Answers2

0

why don't you use proper way.

//php function
function test(){
     ...
     $array = mysqli_fetch_array($sqlresult);
     ...
     //after fetching your array
     echo json_encode($array)
}

<script>
    //retrieve from js
    var jsArray = JSON.parse(<?php echo test(); ?>);
    //Or
    var jsArray = jQuery.parseJSON(<?php echo test(); ?>);

</script>
Fury
  • 4,643
  • 5
  • 50
  • 80
0

this may work (with some modification):

$a = array();
while($myArray = mysqli_fetch_array($sqlresult)) { 
  array_push($a, $myArray);
}

echo "<script type='text/javascript'> 
var stats =" . json_encode($a) . ';';
echo "\n</script>";
nur
  • 161
  • 2
  • 11