• Good afternoon, colleagues!
    Need advice, newbie. There is an array from which the data goes through "return $ dab".
  • The table is built wonderfully, but the schedule does not work. Instead of static data, it is necessary to pull the data out of the array through the loop. We need a schedule for the last 5 days for cars and trucks. How to display data from an array correctly?

`

google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ ['Дата', 'Легковые', 'Грузовики', 'Всего'], /* ['9.11.2016', 45, 34, 79], ['10.11.2016', 78, 47, 125], ['11.11.2016', 20, 40, 60], ['12.11.2016', 56, 74, 130], ['13.11.2016', 118, 122, 240], */ <?php foreach($dab as $dt){ $i=0; $num=0; $dab = date('dmY', mktime(0, 0, 0, date('m'), date('d')-$i, date('y')));?> <?php echo [$dab, $dt['car'], $dt['truck'], $dt['all']]; ?>, <?php if($i>=5){ break; } $i++;?> <?php } ?> ]); var options = { title: 'Статистика продажи машин за последние 5 дней', curveType: 'function', legend: { position: 'bottom' } }; var chart = new google.visualization.LineChart(document.getElementById('curve_chart')); chart.draw(data, options); } </script> 

`

    2 answers 2

    <?php $data = [['Дата', 'Легковые', 'Грузовики', 'Всего'],[...],[...]]; $dataJSON = json_encode($data) <?php $data = [['Дата', 'Легковые', 'Грузовики', 'Всего'],[...],[...]]; $dataJSON = json_encode($data) json_encode ($ arr) - convert an array to json

     <script> var data = JSON.parse('<?=$dataJOSN?>'); </script> 

    and all variable is ready

    • thanks of course! But I did not understand how to remake my code according to your recommendations. And so tried and syk. Nothing works well. Can you explain with my example ?! - Aibek Sydygaliev
    • one
      @Aibek Sydygaliev See, first simply collect the data array you need in php, then use the json_encode($data) function to create a json string, then you can put this code in a variable in js and substitute it into google.visualization.arrayToDataTable(data) - Orange_shadow
    • Thank. I'll try. - Aibek Sydygaliev

    Correct answer:

    1. Need to do

      • 2 functions.
      • a) The function of constructing data in tabular form (in this table, dates go from top to bottom, starting from the date that is needed today);
      • b) Function No. 2 of data construction, in tabular form (for a graph);
    2. The lower example considers data for 5 days (3 days + 0 day + today)

    `function visits2 ($ db) {

      for($i=3; $i>=0; $i--){ $later_date = mktime(0, 0, 0, date('m'), date('d')-$i, date('y')); $early_date = mktime(0, 0, 0, date('m'), date('d')-($i+1), date('y')); $db->where('date_reg',$early_date,'>'); $db->where('date_reg',$later_date,'<'); $visitors[$i] = $db->get('visitors2'); $db->where('date_reg',$early_date,'>'); $db->where('date_reg',$later_date,'<'); $db->where('visit_type',1); $visitors2[$i] = $db->get('visitors2'); $db->where('date_reg',$early_date,'>'); $db->where('date_reg',$later_date,'<'); $db->where('visit_type',0); $visitors3[$i] = $db->get('visitors2'); $out[$i]['all']=count($visitors[$i]); $out[$i]['first']=count($visitors2[$i]); $out[$i]['order']=count($visitors3[$i]); } $today = mktime(0, 0, 0, date('m'), date('d'), date('y')); $db->where('date_reg',$today,'>'); $out['today']['all']=count($db->get('visitors2')); $db->where('visit_type',1); $db->where('date_reg',$today,'>'); $out['today']['first']=count($db->get('visitors2')); $db->where('visit_type',0); $db->where('date_reg',$today,'>'); $out['today']['order']=count($db->get('visitors2')); return $out; } $out2 = visits2($db); // echo "<pre>"; print_r($out2); echo "</pre>"; die; // echo $out[3]; function show_visit_chart($data,$num){ $i=0; $g=-4; $num=$num-1; foreach($data as $dt){ $date = date('d/m/Y', mktime(0, 0, 0, date('m'), date('d')+($g++), date('y'))); $pl=(int)$dt['all']-(int)$dt['first']; echo '["'.$date.'",'.$dt['first'].', '.$pl.'],'; if($i>=$num){ break; } $i++; } } function show_visit2($data,$num){ $i=0; $g=-4; $num=$num-1; echo '<table class="table table-bordered">'; echo '<thead class="thead-default"><tr><th>Дата</th><th>Первичные</th><th>По записи</th><th>Всего</th></tr></thead>'; foreach($data as $dt){ $date = date('dmY', mktime(0, 0, 0, date('m'), date('d')+($g++), date('y'))); echo '<tr><td>'.$date.'</td><td> '.$dt['first'].'</td><td> '.$dt['order'].'</td><td> '.$dt['all'].'</td></tr>'; if($i>=$num){ break; } $i++; } echo '</table>'; } <script type="text/javascript"> google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ ['День', 'Первичные', 'По записи'], <?php show_visit_chart($out2,5);?> ]); var options = { // title: 'Статистика пациентов за последние 5 дней', curveType: 'function', legend: {position: 'bottom'} }; var chart = new google.visualization.LineChart(document.getElementById('curve_chart')); chart.draw(data, options); } </script> <section class="col-xs-12 col-md-10"> <h1><?php echo ""; ?></h1> <h3>График визитов</h3> <div id="curve_chart" style="width: 1200px; height: 500px; margin-left:-100px"></div> </section> ` 

    Do not forget to insert another connection to
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">

    1. I hope this example will help other newbies! Good luck, colleagues! :-)