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

Asked

Viewed 104 times

0

I have the following Chart combo:

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


<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="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, example: As today are 23 I need to display from the line:

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

To the line:

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

How could I do that?

1 answer

1


I would advise changing the variables to array, it is much easier to work with loops, but if there is no way to change it now, below is a way to do:

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


<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="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";
?>
  • It worked out the way it needed, only one more doubt,example,the day 23 that is today was without launching,with that was returning this error,by has no information and Chart be blank, could pass some condition ? to deal with this type of error?

  • <br /> <b>Notice</b>: Undefined variable: var_d23 in <b>C: xampp htdocs big bigbi vendas_acum.php</b> on line <b>80</b><br /> [',102.995854,73.333333333326]

  • you can put the echo inside a if isset(${'var_d'.str_pad($i, 2,'0', STR_PAD_LEFT) }) which will only print if the variable exists...

  • Perfect, Thank you again

Browser other questions tagged

You are not signed in. Login or sign up in order to post.