Как получить данные из нескольких таблиц БД MySQL и обработать их с помощью PHP для отображения графиков?

Я хочу сделать график для годового отчета о продажах, покупках и переводах. MySQL Quires и PHP-коды и графические коды приведены ниже.

MySQL Quires

ПРОДАЖА ЗАПРОС

SELECT COUNT(id) AS sales, DATE_FORMAT( date, '%Y-%M' ) AS dataofmonth FROM sales

WHERE `date` LIKE '2015%'

GROUP BY DATE_FORMAT(date, '%Y-%M')
ORDER BY DATE_FORMAT(date, '%Y-%M') ASC

ПОКУПКА ЗАПРОС

SELECT COUNT(id) AS num, DATE_FORMAT( date, '%Y-%M' ) AS dataofmonth FROM purchases

WHERE `date` LIKE '2015%'

GROUP BY DATE_FORMAT(date, '%Y-%M')
ORDER BY DATE_FORMAT(date, '%Y-%M') ASC

ТРАНСФЕРЫ ЗАПРОС

SELECT COUNT(id) AS num, DATE_FORMAT( date, '%Y-%M' ) AS dataofmonth FROM transfers

WHERE `date` LIKE '2015%'

GROUP BY DATE_FORMAT(date, '%Y-%M')
ORDER BY DATE_FORMAT(date, '%Y-%M') ASC

Теперь запрос на продажу вернул следующие данные

sales, dataofmonth
1   2015-December
3   2015-January

В базе данных доступны только данные за январь и декабрь. Итак, я хочу показать 0 для других месяцев.

ДИАГРАММА / ГРАФ

Я использую высокие графики для отображения страницы графика, используя следующий код, который отлично работает, но я не могу сделать его динамичным с помощью PHP.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

<script type="text/javascript">
jQuery.noConflict();

var example = 'line-basic',
theme = 'default';

(function($){ // encapsulate jQuery$(function () {
$('#container').highcharts({
title: {
text: 'Monthly Average Report',
x: -20 //center
},/*
subtitle: {
text: 'Source: WorldClimate.com',
x: -20
},*/
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: '<?=$_SESSION['mcs_name']?>'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},/*
tooltip: {
valueSuffix: '°C'
},*/
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [{
name: 'Purchases',
data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}, {
name: 'Sales',
data: [<?=@$janS?>, <?=@$febS?>, <?=@$marS?>, <?=@$aprS?>, <?=@$mayS?>, <?=@$junS?>, <?=@$julS?>, <?=@$augS?>, <?=@$sepS?>, <?=@$octS?>, <?=@$novS?>, <?=@$decS?>]
}, {
name: 'Transfers',
data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
}]
});
});

})(jQuery);
</script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

PHP-код

Мой PHP-код имеет много проблем, так как я новичок в создании графиков.

<?php
$purchases = $db->query("SELECT COUNT(id) AS num, DATE_FORMAT( date, '%Y-%M' ) AS dataofmonth FROM purchases WHERE `date` LIKE '2015%' GROUP BY DATE_FORMAT(date, '%Y-%M') ORDER BY DATE_FORMAT(date, '%Y-%M') ASC");
$sales = $db->query("SELECT COUNT(id) AS num, DATE_FORMAT( date, '%Y-%M' ) AS dataofmonth FROM sales WHERE `date` LIKE '2015%' GROUP BY DATE_FORMAT(date, '%Y-%M') ORDER BY DATE_FORMAT(date, '%Y-%M') ASC");
$transfers = $db->query("SELECT COUNT(id) AS num, DATE_FORMAT( date, '%Y-%M' ) AS dataofmonth FROM transfers WHERE `date` LIKE '2015%' GROUP BY DATE_FORMAT(date, '%Y-%M') ORDER BY DATE_FORMAT(date, '%Y-%M') ASC");

while($objP = $purchases->fetch(PDO::FETCH_OBJ))
{
if($objP->dataofmonth == "".date('Y')."-January")
$janP = $objP->num;
if($objP->dataofmonth == "".date('Y')."-February")
$febP = $objP->num;
if($objP->dataofmonth == "".date('Y')."-March")
$marP = $objP->num;
if($objP->dataofmonth == "".date('Y')."-April")
$aprP = $objP->num;
if($objP->dataofmonth == "".date('Y')."-May")
$mayP = $objP->num;
if($objP->dataofmonth == "".date('Y')."-June")
$junP = $objP->num;
if($objP->dataofmonth == "".date('Y')."-July")
$julP = $objP->num;
if($objP->dataofmonth == "".date('Y')."-August")
$augP = $objP->num;
if($objP->dataofmonth == "".date('Y')."-September")
$sepP = $objP->num;
if($objP->dataofmonth == "".date('Y')."-October")
$octP = $objP->num;
if($objP->dataofmonth == "".date('Y')."-November")
$novP = $objP->num;
if($objP->dataofmonth == "".date('Y')."-December")
$decP = $objP->num;
}
while($objS = $sales->fetch(PDO::FETCH_OBJ))
{
if($objS->dataofmonth == "".date('Y')."-January")
$janS = $objS->num;
else
$janS = 0;

if($objS->dataofmonth == "".date('Y')."-February")
$febS = $objS->num;
else
$febS = 0;

if($objS->dataofmonth == "".date('Y')."-March")
$marS = $objS->num;
else
$marS = 0;

if($objS->dataofmonth == "".date('Y')."-April")
$aprS = $objS->num;
else
$aprS = 0;

if($objS->dataofmonth == "".date('Y')."-May")
$mayS = $objS->num;
else
$mayS = 0;

if($objS->dataofmonth == "".date('Y')."-June")
$junS = $objS->num;
else
$junS = 0;

if($objS->dataofmonth == "".date('Y')."-July")
$julS = $objS->num;
else
$julS = 0;

if($objS->dataofmonth == "".date('Y')."-August")
$augS = $objS->num;
else
$augS = 0;

if($objS->dataofmonth == "".date('Y')."-September")
$sepS = $objS->num;
else
$sepS = 0;

if($objS->dataofmonth == "".date('Y')."-October")
$octS = $objS->num;
else
$octS = 0;

if($objS->dataofmonth == "".date('Y')."-November")
$novS = $objS->num;
else
$novS = 0;

if($objS->dataofmonth == "".date('Y')."-December")
$decS = $objS->num;
else
$decS = 0;

}
while($objT = $transfers->fetch(PDO::FETCH_OBJ))
{
if($objT->dataofmonth == "".date('Y')."-January")
$janT = $objT->num;
if($objT->dataofmonth == "".date('Y')."-February")
$febT = $objT->num;
if($objT->dataofmonth == "".date('Y')."-March")
$marT = $objT->num;
if($objT->dataofmonth == "".date('Y')."-April")
$aprT = $objT->num;
if($objT->dataofmonth == "".date('Y')."-May")
$mayT = $objT->num;
if($objT->dataofmonth == "".date('Y')."-June")
$junT = $objT->num;
if($objT->dataofmonth == "".date('Y')."-July")
$julT = $objT->num;
if($objT->dataofmonth == "".date('Y')."-August")
$augT = $objT->num;
if($objT->dataofmonth == "".date('Y')."-September")
$sepT = $objT->num;
if($objT->dataofmonth == "".date('Y')."-October")
$octT = $objT->num;
if($objT->dataofmonth == "".date('Y')."-November")
$novT = $objT->num;
if($objT->dataofmonth == "".date('Y')."-December")
$decT = $objT->num;
}
?>

В приведенном выше php-коде я пытался поместить данные в следующую строку кодов старшей диаграммы, но не получилось.

{
name: 'Sales',
data: [<?=@$janS?>, <?=@$febS?>, <?=@$marS?>, <?=@$aprS?>, <?=@$mayS?>, <?=@$junS?>, <?=@$julS?>, <?=@$augS?>, <?=@$sepS?>, <?=@$octS?>, <?=@$novS?>, <?=@$decS?>]
}

1

Решение

Если я правильно понял вашу цель и рабочий процесс, вы должны заменить:

 while($objS = $sales->fetch(PDO::FETCH_OBJ))
{
if($objS->dataofmonth == "".date('Y')."-January")
$janS = $objS->num;
else
$janS = 0;

if($objS->dataofmonth == "".date('Y')."-February")
$febS = $objS->num;
else
$febS = 0;

if($objS->dataofmonth == "".date('Y')."-March")
$marS = $objS->num;
else
$marS = 0;

if($objS->dataofmonth == "".date('Y')."-April")
$aprS = $objS->num;
else
$aprS = 0;

if($objS->dataofmonth == "".date('Y')."-May")
$mayS = $objS->num;
else
$mayS = 0;

if($objS->dataofmonth == "".date('Y')."-June")
$junS = $objS->num;
else
$junS = 0;

if($objS->dataofmonth == "".date('Y')."-July")
$julS = $objS->num;
else
$julS = 0;

if($objS->dataofmonth == "".date('Y')."-August")
$augS = $objS->num;
else
$augS = 0;

if($objS->dataofmonth == "".date('Y')."-September")
$sepS = $objS->num;
else
$sepS = 0;

if($objS->dataofmonth == "".date('Y')."-October")
$octS = $objS->num;
else
$octS = 0;

if($objS->dataofmonth == "".date('Y')."-November")
$novS = $objS->num;
else
$novS = 0;

if($objS->dataofmonth == "".date('Y')."-December")
$decS = $objS->num;
else
$decS = 0;

}

к:

$salesArr = array_fill(1,12,0);
while($objS = $sales->fetch(PDO::FETCH_OBJ))
{
if($objS->dataofmonth == "".date('Y')."-January")
$salesArr[1] = $objS->num;
if($objS->dataofmonth == "".date('Y')."-February")
$salesArr[2] = $objS->num;
if($objS->dataofmonth == "".date('Y')."-March")
$salesArr[3] = $objS->num;
if($objS->dataofmonth == "".date('Y')."-April")
$salesArr[4] = $objS->num;
if($objS->dataofmonth == "".date('Y')."-May")
$salesArr[5] = $objS->num;
if($objS->dataofmonth == "".date('Y')."-June")
$salesArr[6] = $objS->num;
if($objS->dataofmonth == "".date('Y')."-July")
$salesArr[7] = $objS->num;
if($objS->dataofmonth == "".date('Y')."-August")
$salesArr[8] = $objS->num;
if($objS->dataofmonth == "".date('Y')."-September")
$salesArr[9] = $objS->num;
if($objS->dataofmonth == "".date('Y')."-October")
$salesArr[10] = $objS->num;
if($objS->dataofmonth == "".date('Y')."-November")
$salesArr[11] = $objS->num;
if($objS->dataofmonth == "".date('Y')."-December")
$salesArr[12] = $objS->num;

}

и измените свой:

{
name: 'Sales',
data: [<?=@$janS?>, <?=@$febS?>, <?=@$marS?>, <?=@$aprS?>, <?=@$mayS?>, <?=@$junS?>, <?=@$julS?>, <?=@$augS?>, <?=@$sepS?>, <?=@$octS?>, <?=@$novS?>, <?=@$decS?>]
}

чтобы:

{
name: 'Sales',
data: [<?=implode(',',$salesArr) ?>]
}

Я надеюсь, что мое предположение правильно и полезно 🙂

0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]