Correct answer:
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);
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">
- I hope this example will help other newbies! Good luck, colleagues! :-)