user982853
user982853

Reputation: 2488

Highchart Array Xaxis

I have a php page that uses highcharts. I can't seem to get the xaxis to populate using an array.

I have a php array that was made using:

$x = array(); 
while ($data = mysql_fetch_assoc($results)){ 
    $x[]= $data['sold_date'];  
}

when I print_r I get

Array ( [0] => 2009-01-20 [1] => 2009-04-17 [2] => 2009-09-15 [3] => 2009-10-16 [4] => 2010-01-04 [5] => 2010-04-01 [6] => 2010-07-23 [7] => 2010-10-20 [8] => 2011-01-07 [9] => 2011-05-27 [10] => 2011-07-01 [11] => 2011-10-14 [12] => 2012-01-27 [13] => 2012-04-25 [14] => 2012-07-24 [15] => 2012-11-07 [16] => 2013-01-18 )

Now in highcharts I want the above array to be the values of the xaxis. I don't know what I am doing wrong. I have tried:

xAxis: {
  categories: ["<?php echo $x;?>"]
},

but it returns: the word array 1 2 4 5 6 7... instead of it listing the dates in the array. Please help.

Upvotes: 1

Views: 785

Answers (2)

Sebastian Bochan
Sebastian Bochan

Reputation: 37588

Use join() function to array

categories: ['<?php echo join($categories, "', '") ?>']

Example categories array:

<?php
   $categories[] = 'Jan';
   $categories[] = 'Feb';
   $categories[] = 'Mar';
   $categories[] = 'Apr';
?>

Upvotes: 1

Insyte
Insyte

Reputation: 2236

<?php
$i = 0;
$num = count($x);
?>


xAxis: {
  categories: [<?php foreach($x as $key) { 
                         if(++$i === $num) { // this will remove the comma if last in array.
                             $comma = ''; 
                         }
                         else {
                             $comma = ',';
                         }
                         echo "'" . $key . "'" . $comma . ""; } ?>]
},

This is untested.

Upvotes: 0

Related Questions