0

All I want is to be able to add this simple PHP array to display in the X axis. It's not wanting to display my content at all and I'm not understanding why.

<!DOCTYPE HTML>
<html>
    <head>
        <?php 
        $array = array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
        ?>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"type="text/javascript"></script> 
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">


$(function () {
    js_array = new Array(<?php echo json_encode($array) ?>);
        $('#container').highcharts({
            title: {
                text: 'Monthly Average Temperature',
                x: -20 //center
            },
            subtitle: {
                text: 'Source: WorldClimate.com',
                x: -20
            },
            xAxis: {
                categories: js_array
            }
4
  • won't solve your problem, but you should use [] instead of new Array - the latter creates an unneeded object wrapper Commented Aug 5, 2013 at 17:40
  • 1
    Try js_array = JSON.parse('<?php echo json_encode($array) ?>'); Commented Aug 5, 2013 at 17:40
  • I'm getting no luck with that. I'm still getting a blank screen. Commented Aug 5, 2013 at 17:46
  • did you check the error log? Commented Aug 5, 2013 at 17:59

5 Answers 5

4

We seem to be overcomplicating matters here...

change this:

           js_array = new Array(<?php echo json_encode($array) ?>);

           ...

           xAxis: {
                categories: js_array
            }

to this:

xAxis: {
   categories: <?php echo json_encode($array); ?>
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this.

var js_array = <?php echo json_encode($array); ?>;

7 Comments

It wont help, you cant use JSON-encoded strings directly. You need to parse them first
No luck, still getting a blank screen.
after adding the php array into js did you try to check the value inside your array ? it can be some other issue as well. try console.log(js_array) and see weather the values are there
Yeah that seems to be an issue as well. It's not wanting to print out the values.
There is a major issue going on. I've commented out everything and tried just echoing out the php. Not even that works.
|
0

An JSON Object is not the same as an js array. You would need to parse the code first. JSON.parse() or eval() might help parsing.

js_array = JSON.parse('<?php echo json_encode($array) ?>'); 

as @user1477388 commented correctly

4 Comments

+1 but eval is unnecessary. use JSON.parse, which was designed for this kind of thing.
I know, but eval is not as bad as everybody thinks. Just in case JSON.parse() is not availible (its only supported in most browser), eval() works the same way.
if JSON.parse is not available, standard practice is to just include crockford's json2.js library. eval will work fine for this case (as there's no user input), but recommending it as a general solution is not a good idea.
there's no need to parse anything - you can use the json_encoded array directly as the categories array.
0

Take look at the article http://docs.highcharts.com/#preprocessing-data-from-a-database which describe hwo to use array.

Comments

0

it worked with me like this:

for categories :

categories: [<?php for($i = 0; $i < count($name); $i++){ echo "'".$name[$i]."'"; echo ",";} ?>],

for data:

data: [<?php echo join($age, ',') ?>]

Note that $name is a string array and $age is an integer array. Also keep in mind that this code worked with me when I imbedded the php,Sql query in the same page.

Comments

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.