1

The php code is:

$sql="SELECT `Variant`, COUNT(`Variant`) AS tsold,
      MONTHNAME(sold_date) AS mname
      FROM `vehicle_sold`
      GROUP BY MONTH(`sold_date`), `Variant` ";
while($row = mysqli_fetch_array($result)) {
    echo $row["Variant"],"---",$row["mname"],"---",$row["tsold"],"<br/>";     
}

I get the following required result and it shows me how many times each variant has been sold out in each month.

GLI A/T---January---1
GLI A/T---February---1
XLI M/T---February---1
GLI A/T---March---2
GLI M/T---March---2
Grande---March---1
XLI M/T---March---2
GLI A/T---April---2
GLI M/T---April---6
XLI M/T---April---3
GLI A/T---May---1
GLI M/T---May---4
Grande---May---1
GLI A/T---July---1

Now the problem is i want to shift this data to javascript

2
  • instead of echoing out that line just push it into an array; then print it out in your JavaScript with something like var jsArray = <?= json_encode($phpArray); ?>; Commented Jul 16, 2015 at 10:08
  • A nice way of doing this would be to use AJAX, but I don't know if that's an option for you? Generally speaking you don't want to mix PHP and Javascript. Commented Jul 16, 2015 at 10:20

2 Answers 2

3

If you want the result as a JS array, you could do this:

<?php
    $sql="...";
    /* Don't forget to execute the $sql query here */
    while($row = mysqli_fetch_array($result)) {
        $myData[] = $row["Variant"]."---".$row["mname"]."---".$row["tsold"];     
    }
?>

<script>
    var myData = <?php echo json_encode($myData); ?>;
    alert( JSON.stringify(myData) );
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

would you pls tell me how to echo data() in php and how to extract and print in javascript???
2

PHP code:

$sql="SELECT `Variant`, COUNT(`Variant`) AS tsold,
      MONTHNAME(sold_date) AS mname
      FROM `vehicle_sold`
      GROUP BY MONTH(`sold_date`), `Variant` ";
$data = array();
while($row = mysqli_fetch_array($result)) {
    $data[] = $row;    
}

And in JavaScript:

<script>
var data = <?php echo json_encode($data); ?>;
//your JavaScript code with data
</script>

1 Comment

would you pls tell me how to echo data() in php and how to extract and print in javascript???

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.