Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to figure out how to parse the SQL data I pulled using PHP to a javascript file.

I need to store the PHP array into java because I have a graph that uses a javascript array to populate the data.

Right now I have a button that refreshes my graph data, and when it is clicked it calls this javascript that's included in my main HTML file:

button.js

$(function(){
$('#refreshchart').click(function() {
        var chart = $('#chart').highcharts();
        chart.series[0].setData(dataarray);
    });
}); 

The Java array "dataarray" needs to be pulled from a SQL database.

I have a PHP script that pulls the required data from the database.

Here is my PHP script:

query.php

<?php
include 'dbcon.php';
$query0 = "SELECT count(*) FROM test WHERE ans=0";
$query1 = "SELECT count(*) FROM test WHERE ans=1";
$query2 = "SELECT count(*) FROM test WHERE ans=2";
$query3 = "SELECT count(*) FROM test WHERE ans=3";
$query4 = "SELECT count(*) FROM test WHERE ans=4";
$query5 = "SELECT count(*) FROM test WHERE ans=5";
$query6 = "SELECT count(*) FROM test WHERE ans=6";
$query7 = "SELECT count(*) FROM test WHERE ans=7";
$query8 = "SELECT count(*) FROM test WHERE ans=8";
$query9 = "SELECT count(*) FROM test WHERE ans=9";

$result0 = $mysqli->query($query0) or die($mysqli->error.__LINE__);
$result1 = $mysqli->query($query1) or die($mysqli->error.__LINE__);
$result2 = $mysqli->query($query2) or die($mysqli->error.__LINE__);
$result3 = $mysqli->query($query3) or die($mysqli->error.__LINE__);
$result4 = $mysqli->query($query4) or die($mysqli->error.__LINE__);
$result5 = $mysqli->query($query5) or die($mysqli->error.__LINE__);
$result6 = $mysqli->query($query6) or die($mysqli->error.__LINE__);
$result7 = $mysqli->query($query7) or die($mysqli->error.__LINE__);
$result8 = $mysqli->query($query8) or die($mysqli->error.__LINE__);
$result9 = $mysqli->query($query9) or die($mysqli->error.__LINE__);

$row0 = $result0->fetch_row();
$row1 = $result1->fetch_row();
$row2 = $result2->fetch_row();
$row3 = $result3->fetch_row();
$row4 = $result4->fetch_row();
$row5 = $result5->fetch_row();
$row6 = $result6->fetch_row();
$row7 = $result7->fetch_row();
$row8 = $result8->fetch_row();
$row9 = $result9->fetch_row();

echo "Number of people that chose A: ", $row1[0];
echo "<br>Number of people that chose B: ", $row2[0];
echo "<br>Number of people that chose C: ", $row3[0];
echo "<br>Number of people that chose D: ", $row4[0];
echo "<br>Number of people that chose E: ", $row5[0];
echo "<br>Number of people that chose F: ", $row6[0];
echo "<br>Number of people that chose G: ", $row7[0];
echo "<br>Number of people that chose H: ", $row8[0];
echo "<br>Number of people that chose I: ", $row9[0];
echo "<br>Number of people that chose J: ", $row0[0];
$array = array($row1[0],$row2[0],$row3[0],$row4[0],$row5[0],$row6[0],$row7[0],$row8[0],$row9[0],$row0[0]);
echo json_encode($array);
?>

Now I know that I am going to have to encode the PHP array using json so that it is properly formatted and that I am probably going to have to use AJAX to call the PHP script to get the data.

This is where I am getting stuck, I am not sure how to grab the results and parse them into the java array so that the graph can be updated with the results in the SQL database.

Any help would be much appreciated!

Thanks, Christopher

share|improve this question

4 Answers 4

up vote 1 down vote accepted

If you want to call your php-code via ajax try:

$(function(){
var chart = $('#chart').highcharts();
$('#refreshchart').click(function() {
        $.ajax({
           type: "GET",
           url: "some.php",
           success: function(data) {
               chart.series[0].setData($.parseJSON(data));
           }
        });
    });
});

Just set "some.php" to your php-filename.

share|improve this answer
    
This is exactly what I need to do, for the some.php file, how would it be formatted? Could I just keep it the way I have query.php? –  Christopher Jul 25 at 2:09
    
overlooked that you gave the filename... the json encoded array of your query.php file should work ;) –  jhinzmann Jul 25 at 2:14
    
but you have to leave out all the echo parts that come before your array –  jhinzmann Jul 25 at 2:17
    
the ajaxcall gets all the output by your php-file, which should only be json encoded array, as string and calls the success function afterwards. the string that was generated by your php file will be passed as first parameter to the successfunction. the name of the parameter which i called "data" is completely irrelevant. you just have to make sure that you take the same name in the parseJSON() part. –  jhinzmann Jul 25 at 2:26
    
Looks like it's kinda working, but it's not loading the data into highcharts correctly. Hmm. –  Christopher Jul 25 at 2:26
<script>
var contants = <?php echo json_encode($array); ?>
</script>

And then transform it into an array. jQuery's $.parseJSON(string) might help.

share|improve this answer
    
I have an HTML file that draws the buttons, that HTML file has the javascript included. The PHP file is not linked to anything, so therefore there is no way for the javascript file to talk to the PHP file. I need to make the javascript file (the included .js) to talk to the PHP file. I would assume I need to use AJAX. –  Christopher Jul 25 at 2:04

If possible, avoid writing dynamic JavaScript in your code, use webservices for this, you can get this data through AJAX call using jQuery for example.

$.ajax({
  url: "query.php",
  data: { json: "on" },
}).done(function(data) {
  console.log(data);
  alert("found " + data.length + " elements" + "\n" + "first one: " + data[0].name + " = " + data[0].total);
});

I would prefer to write like this:

<?php
include 'dbcon.php';

$query =
    "SELECT 'A' AS name,count(*) AS total FROM test WHERE ans=0 UNION " .
    "SELECT 'B' AS name,count(*) AS total FROM test WHERE ans=1 UNION " .
    "SELECT 'C' AS name,count(*) AS total FROM test WHERE ans=2 UNION " .
    "SELECT 'D' AS name,count(*) AS total FROM test WHERE ans=3 UNION " .
    "SELECT 'E' AS name,count(*) AS total FROM test WHERE ans=4 UNION " .
    "SELECT 'F' AS name,count(*) AS total FROM test WHERE ans=5 UNION " .
    "SELECT 'G' AS name,count(*) AS total FROM test WHERE ans=6 UNION " .
    "SELECT 'H' AS name,count(*) AS total FROM test WHERE ans=7 UNION " .
    "SELECT 'I' AS name,count(*) AS total FROM test WHERE ans=8 UNION " .
    "SELECT 'J' AS name,count(*) AS total FROM test WHERE ans=9";

$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

$rows = $result->fetch_all(MYSQLI_ASSOC);

$print_json = !empty($_GET['json']) && $_GET['json']=='on'; // your content negotiation logic here

if ($print_json) {
    header('Content-Type: application/json');
    echo json_encode($rows);
} else {
    foreach ($rows as $row) {
        echo "Number of people that chose {$row['name']}: {$row['total']}";
    }
}

This way you have more control and a cleaner code. I think you code improve SQL, though.

share|improve this answer
    
Yeah, that is much more optimized, however I still need to make the javascript file (the .js included in my HTML page) to talk to the PHP file to grab the results and allow the array to be used in the included JS. I would assume I need to use AJAX. –  Christopher Jul 25 at 2:08
    
@Christopher Exactly. I've updated the code to use json variable and a AJAX call sample. –  dvm Jul 25 at 2:09
    
Hmm, What's the difference in using what you coded, and the suggested post by @jhinzmann - My only question now is, what format does the test.php need to be? How does the ajax function get the var from the test.php? I am trying to understand the way it works. –  Christopher Jul 25 at 2:13
    
The AJAX call I've done just shows how it's made a simple call, his code shows exactly how to integrate in your Highcharts use. –  dvm Jul 25 at 2:37

this code is working

var str_array=[];

function something()
{
    str_array= <?php echo json_encode($array); ?>;
    alert(str_array);`
}
share|improve this answer
    
My javascript file is a .js that is included in my main HTML file, therefore I cannot use PHP tags inside of my .js file since JS is run client side. –  Christopher Jul 25 at 1:58
1  
just put it in your php file after the select statement lols –  CodeSlayer Jul 25 at 2:00
    
I guess I am a bit confused. I have an HTML file that draws the buttons, that HTML file has the javascript included. The PHP file is not linked to anything, so therefore there is no way for the javascript file to talk to the PHP file. –  Christopher Jul 25 at 2:03
    
no..i am the one that confused..if your php is in the other file then you don't need your java script yet.. and after you call your php..at the end of ?> type that code and you can access that variable.. i gave you the syntax you are asking..it's not my problem anymore –  CodeSlayer Jul 25 at 3:06
    
Problem has been solved by using AJAX to use GET for the query.php - Thanks for your input. –  Christopher Jul 25 at 3:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.