这是我创建的一个函数,用于在单个文件中创建 100 多个工作簿。代码工作正常,但完成整个过程需要更长的时间。我想知道如何让这些事情变得更快。感谢大家阅读这篇文章。
function allStats() {
$data['total'] = $this->poster_model->numberTotalPiosters();
$data['allPosters'] = $this->poster_model->getAllPosters();
require(APPPATH.'third_party/gapi.php');
$ga = new gapi('xxx@xxxx', 'xxxxx');
require(APPPATH.'third_party/PHPExcel.php');
require(APPPATH.'third_party/PHPExcel/Writer/Excel2007.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("xxxxxx");
$objPHPExcel->getProperties()->setLastModifiedBy("xxxxxx");
$objPHPExcel->getProperties()->setTitle("xxxxxxx");
$objPHPExcel->getProperties()->setSubject("xxxxxx");
$objPHPExcel->getProperties()->setDescription("xxxxx (".$data['allPosters'][$i]['PDF_Name'].")");
$i=0;
while ($i < 150)
{
if($data['allPosters'][$i]['Translations']==0)
{
if(time() < strtotime($data['allPosters'][$i]['Expiry_Date'])){
$expiry = date('Y-m-d');
$limit = round((strtotime($data['allPosters'][$i]['Expiry_Date']) - time()) / 86400);
}else if(strtotime($data['allPosters'][$i]['Expiry_Date']) == strtotime($data['allPosters'][$i]['Presentation_Date'])){
$expiry = strtotime($data['allPosters'][$i]['Presentation_Date']) + 86400;
$limit = 1;
}else{
$expiry = date('Y-m-d', strtotime($data['allPosters'][$i]['Expiry_Date']));
$limit = round((strtotime($data['allPosters'][$i]['Expiry_Date']) - strtotime($data['allPosters'][$i]['Presentation_Date'])) / 86400);
}
$ga->requestReportData(
xxxxxxxx,
array('date','eventAction'),
array('uniqueEvents'),
array('eventAction'),
'eventAction=='.base_url().'uploads/'.$data['allPosters'][$i]['PDF_Name'],
date('Y-m-d', strtotime($data['allPosters'][$i]['Presentation_Date'])),
$expiry,
1,
$limit
);
$download_stats = array();
foreach($ga->getResults() as $result){
$download_stats[$result->getDate()] = array(
'pageviews' => $result->getUniqueEvents()
);
}
$data['downloads'] = $ga->getUniqueEvents();
$ga->requestReportData(
xxxxxxx,
array('date'),
array('pageviews','uniquepageviews','visitors'),
array('date'),
'pagePath==/test/'.$data['allPosters'][$i]['Path'],
date('Y-m-d', strtotime($data['allPosters'][$i]['Presentation_Date'])),
$expiry,
1,
$limit
);
$stats = array();
foreach($ga->getResults() as $result){
$stats[] = array(
'origdate' => $result->getDate(),
'date' => date('j F Y', strtotime($result->getDate())),
'pageviews' => $result->getPageviews(),
'uniquepageviews' => $result->getUniquePageviews(),
'visitors' => $result->getVisitors()
);
}
$data['page_views'] = number_format($ga->getPageviews());
$data['unique_page_views'] = number_format($ga->getUniquePageviews());
$data['visitors'] = number_format($ga->getVisitors());
$ga->requestReportData(
xxxxxxxx,
array('date'),
array('pageviews'),
array('date'),
'pagePath==/test/'.$data['allPosters'][$i]['Path'].'/pdf-emailed.php',
date('Y-m-d', strtotime($data['allPosters'][$i]['Presentation_Date'])),
$expiry,
1,
$limit
);
$email_stats = array();
foreach($ga->getResults() as $result){
$email_stats[] = array(
'origdate' => $result->getDate(),
'date' => date('j F Y', strtotime($result->getDate())),
'pageviews' => $result->getPageviews()
);
}
$data['email_views'] = number_format($ga->getPageviews());
$data['stats'] = $stats;
$data['email_stats'] = $email_stats;
$data['download_stats'] = $download_stats;
$objWorkSheet = $objPHPExcel->createSheet($data['allPosters'][$i]['Identifier']);
$objWorkSheet->SetCellValue('A1', 'Date');
$objWorkSheet->SetCellValue('B1', 'Unique page views');
$objWorkSheet->SetCellValue('C1', 'Page views');
$objWorkSheet->SetCellValue('D1', 'Visitors');
$objWorkSheet->SetCellValue('E1', 'Downloads');
$objWorkSheet->SetCellValue('F1', 'e-mails');
$objWorkSheet->SetCellValue('G1', 'SMS');
$a = 2;
foreach($stats as $stat){
$objWorkSheet->SetCellValue('A'.$a, $stat['date']);
$objWorkSheet->SetCellValue('B'.$a, intval($stat['uniquepageviews']));
$objWorkSheet->SetCellValue('C'.$a, intval($stat['pageviews']));
$objWorkSheet->SetCellValue('D'.$a, intval($stat['visitors']));
$dd=$a;
for($j=0;$j<=$total;$j++)
{
//$data['sms'][$j]['from_number']
//$data['sms'][$j]['date'];
$datetocompare=date('d-m-Y', strtotime($stat['date']));
if((strtotime($datetocompare))==(strtotime($data['sms'][$j]['date'])) ){
if(isset($data['sms'][$j]['numbers_of_sms'])){
$objWorkSheet->SetCellValue('G'.$a, ($data['sms'][$j]['numbers_of_sms'] ));
}else{
$objWorkSheet->SetCellValue('G'.$a, 0);
}
}
$dd++;
}
if(isset($download_stats[$stat['origdate']])){
$objWorkSheet->SetCellValue('E'.$a, intval($download_stats[$stat['origdate']]['pageviews']));
}else{
$objWorkSheet->SetCellValue('E'.$a, 0);
}
if(isset($email_stats[$a-2])){
$objWorkSheet->SetCellValue('F'.$a, intval($email_stats[$a-2]['pageviews']));
}else{
$objWorkSheet->SetCellValue('F'.$a, 0);
}
$a++;
}
$objWorkSheet->setTitle($data['allPosters'][$i]['Identifier']);
$i++;
}
}
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('uploads/allposters/'.md5($i).'.xlsx');
}