前面我们写了表单填写界面的生成以及表单的创建,今天我们来处理数据的输入和导出
数据写入
在前面,我们写的 form
表单中写入的是 name, number 和一个包含成绩的 data 数组,现在我们来处理它
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| <?php namespace dxkite\statistical\response;
use suda\core\Request;
class TableResponse extends \suda\core\Response { public function onRequest(Request $request) { $tables = app()->getModuleConfig(app()->getFileModule(__FILE__), 'statistic'); $tableId = $request->get('table_id'); if (isset($tables[$tableId])) { if ($request->hasPost()) { $view = $this->page('state'); if ($type = $this->insertData($tableId, $request)) { if ($type == 1) { $view->set('message','写入成功'); } else { $view->set('message','数据未更新'); } } else { $view->set('message','写入失败'); } $view->render(); } else { $view = $this->page('table'); $view->set('fields', $tables[$tableId]['fields']); $view->set('name', $tables[$tableId]['name']); $view->render(); } } else { hook()->exec('system:404'); } }
public function insertData(string $tableId, Request $request): int { $table = new \dxkite\statistical\table\StatisticTable; if ($request->post('name', null) && $request->post('number', null) && $request->post('data', null)) { $where = [ 'table_id' => $tableId, 'student_id' => $request->post('number') ]; if ($data = $table->select(['id'],$where)->fetch()) { if ($table->updateByPrimaryKey($data['id'], [ 'name' => $request->post('name'), 'data' => $request->post('data') ])) { return 1; } else { return 2; } } else { $id = $table->insert([ 'table_id' => $tableId, 'student_id' => $request->post('number'), 'name' => $request->post('name'), 'data' => $request->post('data'), ]); if ($id > 0) { return 1; } } } return 0; } }
|
在这里,我们查询 table_id 和 student_id ,是否存在,存在则更新,否则插入。
数据下载
我们的数据已经储存在数据库中了,现在我们要它可以导出下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| <?php namespace dxkite\statistical\response;
use suda\core\Request;
class DownloadResponse extends \suda\core\Response { public function onRequest(Request $request) { $tables = app()->getModuleConfig(app()->getFileModule(__FILE__), 'statistic'); $tableId = $request->get('table_id'); if (isset($tables[$tableId])) { $data = $this->exportData($tableId, $tables[$tableId]['fields']); $path = RUNTIME_DIR .'/'. $tableId .'.csv'; storage()->put($path,$data); $this->file($path); } else { hook()->exec('system:404'); } }
public function exportData(string $tableId, array $fields): string { $table = new \dxkite\statistical\table\StatisticTable; $datas = $table->listWhere(['table_id' => $tableId]); $csv = []; $csv[0] = [ 0 =>'学号', 1 => '姓名']; $header = [0,1]; foreach ($fields as $id => $value) { $csv[0][$id] = $value['name']; $header[] = $id; } foreach ($datas as $data) { $id = count($csv); $csv[$id][0]= $data['student_id']; $csv[$id][1]= $data['name']; foreach ($data['data'] as $index => $value) { $csv[$id][$index]=$value; } } $text = ''; foreach ($csv as $data) { $row = []; foreach ($header as $id) { $row[] = $data[$id]; } $text .= implode(',', $row) . "\r\n"; } return $text; } }
|
其中 exportData
函数是将数据从数据库中取出并生成 csv
格式的数据,file
函数是将文件生成下载。
添加一下密码认证?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| <?php namespace dxkite\statistical\response;
use suda\core\Request;
class DownloadResponse extends \suda\core\Response { public function onRequest(Request $request) { $tables = app()->getModuleConfig(app()->getFileModule(__FILE__), 'statistic'); $tableId = $request->get('table_id'); if (isset($tables[$tableId])) { if (isset($tables[$tableId]['password'])) { if ($request->get('password') == $tables[$tableId]['password']) { $this->download($tableId, $tables[$tableId]['fields']); } else { echo '密码错误'; } } else { $this->download($tableId, $tables[$tableId]['fields']); } } else { hook()->exec('system:404'); } }
public function download(string $tableId, array $fields) { $data = $this->exportData($tableId, $fields); $path = RUNTIME_DIR .'/'. $tableId .'.csv'; storage()->put($path, $data); $this->file($path); }
public function exportData(string $tableId, array $fields): string { $table = new \dxkite\statistical\table\StatisticTable; $datas = $table->listWhere(['table_id' => $tableId]); $csv = []; $csv[0] = [ 0 =>'学号', 1 => '姓名']; $header = [0,1]; foreach ($fields as $id => $value) { $csv[0][$id] = $value['name']; $header[] = $id; } foreach ($datas as $data) { $id = count($csv); $csv[$id][0]= $data['student_id']; $csv[$id][1]= $data['name']; foreach ($data['data'] as $index => $value) { $csv[$id][$index]=$value; } } $text = ''; foreach ($csv as $data) { $row = []; foreach ($header as $id) { $row[] = $data[$id]; } $text .= implode(',', $row) . "\r\n"; } return $text; } }
|
我把生成下载文件的代码变成了一个函数(因为会写两次),然后验证了一下配置文件是否配置了密码,配置了则要输入密码才可以下载。
配置密码:
下载URL:
http://score.dxkite.org/dev.php/table_id_1/download?password=密码
作业
完整模块代码下载