How to create a loop between records and the current php date

0

I have the following combo chart:

    <div id="vendas_diarias" style="height: 500px;"></div>


<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script><scripttype="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawVisualization);


      function drawVisualization() {
        // Create the data table for Sarah's pizza.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'DATA');
        data.addColumn('number', 'VENDAS');
        data.addColumn('number', 'METAS');
        data.addRows([
         ['<?php echo"$var_d01";?>',<?php echo"$var_vda_d01";?>,<?php echo"$var_mta_d01";?>],
         ['<?php echo"$var_d02";?>',<?php echo"$var_vda_d02";?>,<?php echo"$var_mta_d02";?>],
         ['<?php echo"$var_d03";?>',<?php echo"$var_vda_d03";?>,<?php echo"$var_mta_d03";?>],
         ['<?php echo"$var_d04";?>',<?php echo"$var_vda_d04";?>,<?php echo"$var_mta_d04";?>],
         ['<?php echo"$var_d05";?>',<?php echo"$var_vda_d05";?>,<?php echo"$var_mta_d05";?>],
         ['<?php echo"$var_d06";?>',<?php echo"$var_vda_d06";?>,<?php echo"$var_mta_d06";?>],
         ['<?php echo"$var_d07";?>',<?php echo"$var_vda_d07";?>,<?php echo"$var_mta_d07";?>],
         ['<?php echo"$var_d08";?>',<?php echo"$var_vda_d08";?>,<?php echo"$var_mta_d08";?>],
         ['<?php echo"$var_d09";?>',<?php echo"$var_vda_d09";?>,<?php echo"$var_mta_d09";?>],
         ['<?php echo"$var_d10";?>',<?php echo"$var_vda_d10";?>,<?php echo"$var_mta_d10";?>],
         ['<?php echo"$var_d11";?>',<?php echo"$var_vda_d11";?>,<?php echo"$var_mta_d11";?>],
         ['<?php echo"$var_d12";?>',<?php echo"$var_vda_d12";?>,<?php echo"$var_mta_d12";?>],
         ['<?php echo"$var_d13";?>',<?php echo"$var_vda_d14";?>,<?php echo"$var_mta_d13";?>],
         ['<?php echo"$var_d14";?>',<?php echo"$var_vda_d14";?>,<?php echo"$var_mta_d14";?>],
         ['<?php echo"$var_d15";?>',<?php echo"$var_vda_d15";?>,<?php echo"$var_mta_d15";?>],
         ['<?php echo"$var_d16";?>',<?php echo"$var_vda_d16";?>,<?php echo"$var_mta_d16";?>],
         ['<?php echo"$var_d17";?>',<?php echo"$var_vda_d17";?>,<?php echo"$var_mta_d17";?>],
         ['<?php echo"$var_d18";?>',<?php echo"$var_vda_d18";?>,<?php echo"$var_mta_d18";?>],
         ['<?php echo"$var_d19";?>',<?php echo"$var_vda_d19";?>,<?php echo"$var_mta_d19";?>],
         ['<?php echo"$var_d20";?>',<?php echo"$var_vda_d20";?>,<?php echo"$var_mta_d20";?>],
         ['<?php echo"$var_d21";?>',<?php echo"$var_vda_d21";?>,<?php echo"$var_mta_d21";?>],
         ['<?php echo"$var_d22";?>',<?php echo"$var_vda_d22";?>,<?php echo"$var_mta_d22";?>],
         ['<?php echo"$var_d23";?>',<?php echo"$var_vda_d23";?>,<?php echo"$var_mta_d23";?>],
         ['<?php echo"$var_d24";?>',<?php echo"$var_vda_d24";?>,<?php echo"$var_mta_d24";?>],
         ['<?php echo"$var_d25";?>',<?php echo"$var_vda_d25";?>,<?php echo"$var_mta_d25";?>],
         ['<?php echo"$var_d26";?>',<?php echo"$var_vda_d26";?>,<?php echo"$var_mta_d26";?>],
         ['<?php echo"$var_d27";?>',<?php echo"$var_vda_d27";?>,<?php echo"$var_mta_d27";?>],
         ['<?php echo"$var_d28";?>',<?php echo"$var_vda_d28";?>,<?php echo"$var_mta_d28";?>],
         ['<?php echo"$var_d29";?>',<?php echo"$var_vda_d29";?>,<?php echo"$var_mta_d29";?>],
         ['<?php echo"$var_d30";?>',<?php echo"$var_vda_d30";?>,<?php echo"$var_mta_d30";?>],
         ['<?php echo"$var_d31";?>',<?php echo"$var_vda_d31";?>,<?php echo"$var_mta_d31";?>]
        ]);


    var options = {
      title : 'VENDAS DIARIAS',
      vAxis: {title: 'PERCENTUAL'},
      hAxis: {title: 'DIAS'},
      seriesType: 'bars',
      series: {1: {type: 'line'}}
    };

    var chart = new google.visualization.ComboChart(document.getElementById('vendas_diarias'));
    chart.draw(data, options);
  }
    </script>   

Each line represents 1 day of the month, so it is necessary to display the lines until the current day, for example:  As I am 23 today I need to display the line:

     ['<?php echo"$var_d01";?>',<?php echo"$var_vda_d01";?>,<?php echo"$var_mta_d01";?>],

Up the line:

     ['<?php echo"$var_d23";?>',<?php echo"$var_vda_d23";?>,<?php echo"$var_mta_d23";?>],

How could I do this?

    
asked by anonymous 23.09.2016 / 08:15

1 answer

1

I would advise changing the variables to array, it is much easier to work with loops, but if you can not change it now, here's a way to do it:

<div id="vendas_diarias" style="height: 500px;"></div>


<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script><scripttype="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawVisualization);


      function drawVisualization() {
        // Create the data table for Sarah's pizza.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'DATA');
        data.addColumn('number', 'VENDAS');
        data.addColumn('number', 'METAS');
        data.addRows([
<?php
for ($i=1; $i<=date('d'); $i++) {
    echo "            ['".${'var_d'.str_pad($i, 2,'0', STR_PAD_LEFT) }."',".${'var_vda_d'.str_pad($i, 2,'0', STR_PAD_LEFT) }.",".${'var_mta_d'.str_pad($i, 2,'0', STR_PAD_LEFT) }."]"
    . ($i<date('d')?',':'') . "\r\n";
}
?>      
        ]);


    var options = {
      title : 'VENDAS DIARIAS',
      vAxis: {title: 'PERCENTUAL'},
      hAxis: {title: 'DIAS'},
      seriesType: 'bars',
      series: {1: {type: 'line'}}
    };

    var chart = new google.visualization.ComboChart(document.getElementById('vendas_diarias'));
    chart.draw(data, options);
  }
    </script>   

If you switch to array, the variable would have this structure:

$var = array(
    array('d'=>$var_d01, 'vda_d'=>$var_vda_d01, 'mta_d'=>$var_mta_d01),
    array('d'=>$var_d02, 'vda_d'=>$var_vda_d02, 'mta_d'=>$var_mta_d02),
    array('d'=>$var_d03, 'vda_d'=>$var_vda_d03, 'mta_d'=>$var_mta_d03),
...
);

and would be used like this:

<?php
$dias = array();
foreach($var as $key => $dia) {
    if ($key == date('d')) break;
    $dias[] = "            ['".$dia['d']."',".$dia['vda_d'].",".$dia['mta_d']."]";
}
echo implode(",\r\n", $dias). "\r\n";
?>
    
23.09.2016 / 12:03