我有一个页面,用户可以在其中选择日期范围和大约40个复选框(默认情况下处于选中状态)。每个复选框代表我捕获的数据,并允许它们导出。我希望将选中的复选框导出到我的Excel文件。
我要导出的所有图形都通过$ _POST中的此变量传递:
array (size=3)
'from' => string '2019-01-24' (length=10)
'to' => string '2019-02-24' (length=10)
'graphs' =>
array (size=5)
'matches-graphs' =>
array (size=3)
0 => string 'amount_contact_per_day_org_vol' (length=30)
1 => string 'amount_total_matches' (length=20)
2 => string 'amount_clicks_phone' (length=19)
'volunteers-graphs' =>
array (size=3)
0 => string 'amount_new_volunteers' (length=21)
1 => string 'amount_volunteers' (length=17)
2 => string 'amount_findable_volunteers' (length=26)
'vacancies-graphs' =>
array (size=6)
0 => string 'amount_new_vacancies' (length=20)
1 => string 'amount_active_vacancies' (length=23)
2 => string 'amount_expired_vacancies' (length=24)
3 => string 'amount_archived_vacancies' (length=25)
4 => string 'amount_active_vacancies_per_interest' (length=36)
5 => string 'vacancies_list_org' (length=18)
'traffic-graphs' =>
array (size=5)
0 => string 'amount_views_per_vol_profile' (length=28)
1 => string 'amount_views_per_vacancy' (length=24)
2 => string 'amount_views_per_org_profile' (length=28)
3 => string 'amount_logins_volunteers' (length=24)
4 => string 'amount_logins_orgs' (length=18)
'filters-graphs' =>
array (size=3)
0 => string 'filters_used_search_vol' (length=23)
1 => string 'filters_used_search_org' (length=23)
2 => string 'filters_used_search_vac' (length=23)
然后,我将此$ graphs数组传递给要导出指定Excel图形的函数。我为此使用phpoffice / phpspreadsheet库。
我想通过此数组,并为其中的每个项目写入Excel文件。
我在考虑以下代码,但是必须有更好的方法吗?
if (!empty($graphs)) {
if (!empty($graphs['matches-graphs'])) {
foreach ($graphs['matches-graphs'] as $graph) {
if ($graph == 'amount_contact_per_day_org_vol') {
//Export data
}
if ($graph == 'amount_total_matches') {
//Export data
}
if ($graph == 'amount_clicks_phone') {
//Export data
}
}
}
if (!empty($graphs['volunteers-graphs'])) {
foreach ($graphs['volunteers-graphs'] as $graph) {
if ($graph == 'amount_new_volunteers') {
//Export data
}
if ($graph == 'amount_volunteers') {
//Export data
}
if ($graph == 'amount_findable_volunteers') {
//Export data
}
}
}
//etc for all categories
}