PHP-提取数据问题,提取时间过长
你好,我在获取数据时遇到了问题,需要很长时间,实际上需要10秒才能将数据获取到网格中,有人解决了这个问题吗?请多多指教,谢谢 请参阅下面的代码 在这个for循环中,当我删除sql语句并将其放在外部时,表立即显示,但无法正确获取 SQL语句代码中断行 还有这段代码,我认为是出现了问题,当我删除了这段sql代码并将其带到for循环之外时,数据无法正确获取,但表会立即显示PHP-提取数据问题,提取时间过长,php,mysql,Php,Mysql,你好,我在获取数据时遇到了问题,需要很长时间,实际上需要10秒才能将数据获取到网格中,有人解决了这个问题吗?请多多指教,谢谢 请参阅下面的代码 在这个for循环中,当我删除sql语句并将其放在外部时,表立即显示,但无法正确获取 SQL语句代码中断行 还有这段代码,我认为是出现了问题,当我删除了这段sql代码并将其带到for循环之外时,数据无法正确获取,但表会立即显示 您似乎调用了18个月内某个月的所有数据,使用了18种不同的查询。我建议获取18个月期间的所有数据,然后在获取数据后每月对其进行解析
您似乎调用了18个月内某个月的所有数据,使用了18种不同的查询。我建议获取18个月期间的所有数据,然后在获取数据后每月对其进行解析。 这样,您就不会要求数据库查找相同的数据18次 将SQL置于for循环之外,只需根据18个月窗口的开始和结束日期调用数据 编辑: 我写了一些关于如何解决你的问题的代码。我要远离可怕的sql查询。我甚至不能开始理解它是怎样的,为什么是这样的
//Write a query that calls for all 18 months of data.
$sql = "SELECT Data for all 18 months";
//Call your Database
$sqlData = queryDatabase($sql, $stVal, $stLimit);
//Parse the Data into 18 seperate months
$gData = ParseData($sqlData);
$tableID = 'gridTable';
$merge = 'th,td';
echo json_encode(array(
'datenow' => date('M', strtotime($dateposted)),
'tableID' => $tableID,
'merge' => $merge,
'dates' => $thisDate,
'mheader' => $mheader,
'dheader' => $dheader,
'date' => $date,
'gridData' => $gData
));
//This function slices your sql data into pieces divided by month.
function ParseData($sqlData){
$dataInMonths = [];
//This depends on how you select your data from the DB, I'm going to use assoc
//arrays right now.
$i = -1;
foreach($sqlData as $dataRow){
$currentDate = $dataRow['date'];
$currentTime = strtotime($currentDate);
$currentMonth = date("F-Y", $currentTime);
if($currentMonth != $previousMonth){
$i++;
$dataInMonths[$i] = [];
}
$dataInMonths[$i][] = $dataRow;
$previousMonth = $currentMonth;
}
//Now your data is an array of 18 items, each with a month of their own data.
//You can now continue to parse it to fit within your grid.
return $dataInMonths;
}
你好,先生!如果我删除for循环,代码的结构是什么?请给我一个首选项的源代码?谢谢你在原文中添加了一个编辑。由于我没有完整的代码库,我添加了一些伪代码,这些代码调用程序中其他地方的输入。您好!在我的代码中,我应该把functionParseData放在哪里?我还想知道,在我的原始代码之前,我应该把变量放在for循环中的什么地方?请给我更多的建议,先生!非常感谢你!该函数可以在当前正在执行的脚本中的任何位置执行。
for($x=0;$x<18;$x++){
$thisDate = date('Y-m-d', strtotime($gridDate.'+'.$x.' months'));
$thisDate2 = date('Y-m-d', strtotime($gridfDate.'+'.$x.' months'));
$mheader[$x] = date('Y', strtotime($gridDate.'+'.$x.' months'));
$dheader[$x] = date('M', strtotime($gridDate.'+'.$x.' months'));
$date = $thisDate AND $thisDate2;
//See code below for this break line of codes
$sql .=",(SELECT CASE WHEN COUNT(tc.`name`) = 0 THEN '<td data-attribute=\"".$thisDate2."\"></td>' WHEN DATE_FORMAT(min(pd.`duedate`), '%Y-%m') <= DATE_FORMAT('$thisDate', '%Y-%m') AND DATE_FORMAT(max(pd.`duedate`), '%Y-%m') >= DATE_FORMAT('$thisDate', '%Y-%m') THEN '<td class=\"paid\" data-attribute=\"".$thisDate2."\">Paid Month</td>' ELSE CONCAT('<td data-attribute=\"".$thisDate2."\" class=\"', CASE WHEN min(p.`duedate`) < '$dateposted' THEN 'occ' WHEN str_to_date(t.`due_date`, '%M %d,%Y') < '$dateposted' AND t.`months` = 0 THEN 'fins' ELSE 'ten' END,'\">', tc.`name`,' #', tc.`transID`,'</td>') END AS gdisp FROM `transaction_con` tc LEFT JOIN `transaction` t ON t.`id` = tc.`transID` LEFT JOIN (select sum(pde.amount) as total, pde.net, pde.pay_type, pde.duedate, pde.transID, pde.item, pt.status from payments_depot pde left join payment_type pt on pt.id = pde.pay_type where pde.`item` = 'House Rate' AND pt.status between 1 and 2 group by pde.paymentid HAVING total = net) pd ON t.`id` = pd.`transID` LEFT JOIN payment_type pt ON pt.`id` = pd.`pay_type` LEFT JOIN payments p ON p.transID = t.id WHERE tc.`apartmentID` = a.`id` AND DATE_FORMAT(tc.`date`, '%m-%Y') = DATE_FORMAT('$thisDate', '%m-%Y')) AS '$thisDate'";
}
$gData = $gen->gridData($sql,$stVal,$stLimit);
$tableID = 'gridTable';
$merge = 'th,td';
echo json_encode(array(
'datenow' => date('M', strtotime($dateposted)),
'tableID' => $tableID,
'merge' => $merge,
'dates' => $thisDate,
'mheader' => $mheader,
'dheader' => $dheader,
'date' => $date,
'gridData' => $gData
));
(SELECT CASE WHEN COUNT(tc.`name`) = 0 THEN '<td data-attribute=\"".$thisDate2."\"></td>'
WHEN DATE_FORMAT(min(pd.`duedate`), '%Y-%m') <= DATE_FORMAT('$thisDate', '%Y-%m') AND DATE_FORMAT(max(pd.`duedate`), '%Y-%m') >= DATE_FORMAT('$thisDate', '%Y-%m') THEN
'<td class=\"paid\" data-attribute=\"".$thisDate2."\">Paid Month</td>'
ELSE CONCAT('<td data-attribute=\"".$thisDate2."\" class=\"', CASE WHEN min(p.`duedate`) < '$dateposted' THEN 'occ' WHEN str_to_date(t.`due_date`, '%M %d,%Y') < '$dateposted' AND t.`months` = 0 THEN 'fins' ELSE 'ten' END,'\">', tc.`name`,' #', tc.`transID`,'</td>')
END AS gdisp FROM `transaction_con` tc LEFT JOIN `transaction` t ON t.`id` = tc.`transID` LEFT JOIN (select sum(pde.amount) as total, pde.net, pde.pay_type, pde.duedate, pde.transID, pde.item, pt.status from payments_depot pde left join payment_type pt on pt.id = pde.pay_type where pde.`item` = 'House Rate'
AND pt.status between 1 and 2 group by pde.paymentid HAVING total = net) pd ON t.`id` = pd.`transID`
LEFT JOIN payment_type pt ON pt.`id` = pd.`pay_type` LEFT JOIN payments p ON p.transID = t.id
WHERE tc.`apartmentID` = a.`id` AND DATE_FORMAT(tc.`date`, '%m-%Y') = DATE_FORMAT('$thisDate', '%m-%Y')) AS '$thisDate'";
//Write a query that calls for all 18 months of data.
$sql = "SELECT Data for all 18 months";
//Call your Database
$sqlData = queryDatabase($sql, $stVal, $stLimit);
//Parse the Data into 18 seperate months
$gData = ParseData($sqlData);
$tableID = 'gridTable';
$merge = 'th,td';
echo json_encode(array(
'datenow' => date('M', strtotime($dateposted)),
'tableID' => $tableID,
'merge' => $merge,
'dates' => $thisDate,
'mheader' => $mheader,
'dheader' => $dheader,
'date' => $date,
'gridData' => $gData
));
//This function slices your sql data into pieces divided by month.
function ParseData($sqlData){
$dataInMonths = [];
//This depends on how you select your data from the DB, I'm going to use assoc
//arrays right now.
$i = -1;
foreach($sqlData as $dataRow){
$currentDate = $dataRow['date'];
$currentTime = strtotime($currentDate);
$currentMonth = date("F-Y", $currentTime);
if($currentMonth != $previousMonth){
$i++;
$dataInMonths[$i] = [];
}
$dataInMonths[$i][] = $dataRow;
$previousMonth = $currentMonth;
}
//Now your data is an array of 18 items, each with a month of their own data.
//You can now continue to parse it to fit within your grid.
return $dataInMonths;
}