views:

325

answers:

2

Hi, I need help with this code,I manage to extract data from a mysql database and convert them to a format required by Highcharts.

<?php
$query =mysql_query("select
               date_format(connect_time,'%Y-%m-%d %H %i') AS date,
               Customers.name as customer,
               Sum(duration) as secondes
               from CDR_Vendors
               inner join Customers on (CDR_Vendors.i_customer = Customers.i_customer)
               where
               i_vendor='32'
               and
               connect_time between '2010-09-01 00:00:00' and '2010-09-01 00:10:00'
               group by date
               ORDER BY date", $link) or die(mysql_error());
$row = mysql_fetch_assoc($query);
$customer[] = $row['customer'];
$json_secondes = array();
$json_date = array();
do{
$secondes[] = $row['secondes'];
array_push($json_secondes, $row['secondes']);
array_push($json_date, $row['date']);
}
while($row = mysql_fetch_assoc($query));
//echo json_encode($json_secondes,$row);
//echo json_encode($json_date,$row);
//echo join($secondes, ', ');
?>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>Highcharts Example</title>


      <!-- 1. Add these JavaScript inclusions in the head of your page -->
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
      <script type="text/javascript" src="../js/highcharts.js"></script>

      <!-- 1a) Optional: the exporting module -->
      <script type="text/javascript" src="../js/modules/exporting.js"></script>


      <!-- 2. Add the JavaScript to initialize the chart on document ready -->
      <script type="text/javascript">

         var chart;
         $(document).ready(function() {
            chart = new Highcharts.Chart({
               chart: {
                  renderTo: 'container',
                  defaultSeriesType: 'column'
               },
               title: {
                  text: 'Monthly Average Rainfall'
               },
               subtitle: {
                  text: 'Source: WorldClimate.com'
               },
               xAxis: {
                  categories: <?php echo json_encode($json_date,$row);?>
               },
               yAxis: {
                  min: 0,
                  title: {
                     text: 'Rainfall (mm)'
                  }
               },
               legend: {
                  layout: 'vertical',
                  backgroundColor: '#FFFFFF',
                  align: 'center',
                  verticalAlign: 'top',
                  x: 100,
                  y: 70
               },
               tooltip: {
                  formatter: function() {
                     return ''+
                        this.x +': '+ this.y +' Min';
                  }
               },
               plotOptions: {
                  column: {
                     pointPadding: 0.2,
                     borderWidth: 0
                  }
               },
                    series: [{
                  name: '<?php echo join($customer, ', ');?>',
                  data: [<?php echo join($secondes, ', ');?>]

               }]
            });


         });

      </script>

   </head>
   <body>

      <!-- 3. Add the container -->
      <div id="container" style="width: 1300px; height: 500px; margin: 0 auto"></div>


   </body>
</html>

The problem with this code is that it's only displays data from a single customer, so that the query returns data from multiple Customers.

this method is good or there is another easier way to do that?

Sorry if my code is poorly designed because I am beginner ;)

Thank you

A: 

There is another method with a csv file File format should be as follows:

Categories,date1,date2,date3,Date4,date5,.........
Customer1 ,value1,value2,value3,Value4, value5,.....
Customer2 ,value1,value2,value3,Value4, value5,.....
Customer3 ,value1,value2,value3,Value4, value5,.....
Customer4 ,value1,value2,value3,Value4, value5,.....

Demo here

Any script to generate this file ?

zexus
A: 

Hello, after several attempts using Json,no result. But it works if you use 2 pages, one for the query and a second for the JS and Include the first one.

Query page :

   <?php
$n=5;
$i=0;
for ($t=$n;$t>0;$t--)
{
$i=$i+1;
$categories[$i]=date("Y-m-d", strtotime("-".$t." day"));

}
$query="select
   date(connect_time) as date,
   sum(duration)/60 as minutes,
   Customers.name as customer
   from CDR_Vendors
   inner join Customers on (Customers.i_customer=CDR_Vendors.i_customer)
   where
   Customers.name like 'En-%'
   and (connect_time like ''";
   for($t=1;$t<=$n;$t++){$query.=" or connect_time like '".$categories[$t]."%'";} $query.=")
   group by
   customer,date";

$donnee=mysql_query($query,$link) or exit(mysql_error());

$i=0;
$products[0]=a;
while ($row = mysql_fetch_assoc($donnee)) {
    $product=$row['customer'];
   $x=$row['date'];
    $data[$product][$x]=$row['minutes'];
   if($product!=$products[$i])

   {

   $i=$i+1;$products[$i]=$product;
   }

   }

for ($i=1;$i<count($products);$i++)
{

for ($j=1;$j<=count($categories);$j++)
{
if(isset($data[$products[$i]][$categories[$j]]))
$valeur[$products[$i]][$categories[$j]]=$data[$products[$i]][$categories[$j]];
else $valeur[$products[$i]][$categories[$j]]=0;

}
}
?>

Query result & Js :

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>Highcharts Example</title>
      <!-- 1. Add these JavaScript inclusions in the head of your page -->
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
      <script type="text/javascript" src="../js/highcharts.js"></script>
      <!-- 1a) Optional: the exporting module -->
      <script type="text/javascript" src="../js/modules/exporting.js"></script>
      <!-- ***récupération des données*** -->
      <?php include("query_queryinc.php");?>
      <!-- 2. Add the JavaScript to initialize the chart on document ready -->
      <script type="text/javascript">
         var chart;
         $(document).ready(function() {
            chart = new Highcharts.Chart({
               chart: {
                  renderTo: 'container',
                  defaultSeriesType: 'bar'
               },
               title: {
                  text: 'Monthly Average Rainfall'
               },
               subtitle: {
                  text: 'Source: portaone'
               },
               xAxis: {
                  categories: [<?php for ($j=1;$j<=count($categories);$j++){echo "'".$categories[$j]."',";}?>]
               },
               yAxis: {
                  min: 0,
                  title: {
                     text: 'Minutes (mm)'
                  }
               },
               legend: {
                  layout: 'horizontal',
                  backgroundColor: '#FFFFFF',
                  align: 'center',
                  verticalAlign: 'bottom',
               },
               tooltip: {
                  formatter: function() {
                     return ''+
                        this.x +': '+ this.y +' mm';
                  }
               },
               plotOptions: {
                  column: {
                     pointPadding: 0.2,
                     borderWidth: 0
                  }
               },
               <?php  echo " series: [";
                  for ($i=1; $i<count($products);$i++)
                  {
                  echo"{
                  name: ' ".$products[$i]."  ',
                  data: [";
                  for ($j=1;$j<=count($categories);$j++) {echo $valeur[$products[$i]][$categories[$j]].",";}
                  echo "]},";
                  }
                  echo "
                  ]";?>
            });
         });
   </script>
   </head>
   <body>
      <!-- 3. Add the container -->
      <div id="container" style="width: 500px; height: 400px; margin: 0 auto"></div>
   </body>
</html>

I manage to get a result with this method. Is it possible to optimize the code or convert it to Highcharts required format ?

Regards.

zexus