コード例 #1
0
 public function search()
 {
     parent::checkLogin();
     $data = null;
     $user = new UserModel();
     $tables = $user->getTables();
     foreach ($tables as $item) {
         $item->tableName = substr($item->tableName, 10);
         $item->tableName = str_replace('_', '-', $item->tableName);
     }
     $BMIArr = array(0 => '肥胖', 1 => '超重', 2 => '低体重', '正常');
     $otherArr = array(0 => '不及格', 1 => '及格', 2 => '良好', 3 => '优秀');
     if (isset($_POST['send'])) {
         $model = new UserModel();
         $model->tableName = 'tice_data_' . str_replace('-', '_', $_POST['year']);
         $model->stuId = $_SESSION['ticeStuId'];
         $data = $model->search();
         //var_dump($data);
     }
     $config = new AttributesModel();
     $configArr = $config->getConfig();
     $pageArr = array(0 => 'site/searchColleage', 1 => 'site/searchColleage', 2 => 'site/searchColleage', 3 => 'site/searchPrimary');
     $this->render($pageArr[$configArr->schoolType], array('yearArr' => $tables, 'data' => $data, 'post' => isset($_POST['send']) ? true : false, 'BMIArr' => $BMIArr, 'otherArr' => $otherArr, 'searchYear' => isset($_POST['year']) ? $_POST['year'] : ''));
 }
コード例 #2
0
 public function upload()
 {
     $attrModel = new AttributesModel();
     $configArr = (array) $attrModel->getConfig();
     if ($configArr['beginRow'] == 0) {
         Tool::alertBackNoRefresh('请先设置上传文件的数据格式:)');
         return;
     }
     $allAttr = $attrModel->getAttributes();
     $allAttrArray = array();
     foreach ($allAttr as $attr) {
         $allAttrArray[] = $attr->attributeName;
     }
     $attributes = array_values(array_slice($configArr, 4, 57));
     $attributesFlip = array_flip($attributes);
     /*print_r($attributesFlip);
       echo '<hr/>';*/
     $sheetNumArr = explode('-', $configArr['dataSheetNum']);
     foreach ($sheetNumArr as $key => &$sheetNum) {
         if (trim($sheetNum)) {
             $sheetNum = trim($sheetNum) - 1;
         } else {
             unset($sheetNumArr[$key]);
         }
     }
     if (isset($_POST['send'])) {
         $beginTime = microtime(true);
         $fileName = $_FILES['excel']['name'];
         $explodeArr = explode('.', $fileName);
         $extensionName = $explodeArr[count($explodeArr) - 1];
         $inputFileName = $_FILES['excel']['tmp_name'];
         require '../Extension/PHPExcel_1.8.0/Classes/PHPExcel.php';
         require '../Extension/PHPExcel_1.8.0/Classes/PHPExcel/Writer/Excel2007.php';
         require '../Extension/PHPExcel_1.8.0/Classes/PHPExcel/Writer/Excel5.php';
         $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
         if ($inputFileType != 'Excel2007' && $inputFileType != 'Excel5') {
             Tool::alertBackNoRefresh('上传文件格式不正确:(');
             return;
         }
         $objReader = PHPExcel_IOFactory::createReader($inputFileType);
         $PHPExcel = $objReader->load($inputFileName);
         //根据自己的数据表的大小修改
         $arr = array('A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, 'F' => 6, 'G' => 7, 'H' => 8, 'I' => 9, 'J' => 10, 'K' => 11, 'L' => 12, 'M' => 13, 'N' => 14, 'O' => 15, 'P' => 16, 'Q' => 17, 'R' => 18, 'S' => 19, 'T' => 20, 'U' => 21, 'V' => 22, 'W' => 23, 'X' => 24, 'Y' => 25, 'Z' => 26, 'AA' => 27, 'AB' => 28, 'AC' => 29, 'AD' => 30, 'AE' => 31, 'AF' => 32, 'AG' => 33, 'AH' => 34, 'AI' => 35, 'AJ' => 36, 'AK' => 37, 'AL' => 38, 'AM' => 39, 'AN' => 40, 'AO' => 41, 'AP' => 42, 'AQ' => 43, 'AR' => 44, 'AS' => '45', 'AT' => 46, 'AU' => 47, 'AV' => 48, 'AW' => 49, 'AX' => 50, 'AY' => 51, 'AZ' => 52, 'BA' => 53, 'BB' => 54, 'BC' => 55, 'BD' => 56, 'BE' => 57);
         $sexArr = array('男' => 1, '女' => 0);
         $BMIArr = array('肥胖' => 0, '超重' => 1, '低体重' => 2, '正常' => 3);
         $otherArr = array('不及格' => 0, '及格' => 1, '良好' => 2, '优秀' => 3);
         $year = $_POST['year'];
         $year = str_replace('-', '_', $year);
         $tableName = 'tice_data_' . $year;
         $model = new DataModel();
         $model->dbName = 'tice';
         $model->tableName = $tableName;
         if (!$model->tableExist()) {
             $model->createTable();
         }
         $model = new DataModel();
         foreach ($allAttrArray as $attr) {
             $model->{$attr} = 0;
         }
         $user = new UserModel();
         $totalEmptyErrorArr = array();
         $totalInsertErrorArr = array();
         //检查插入失败数据的二维数组
         $emptyErrorArr = array();
         //检查不完整的数据的二维数组
         $sqlParams = array();
         $affectedRows = 0;
         foreach ($sheetNumArr as $i) {
             $currentSheet = $PHPExcel->getSheet($i);
             //获得工作表,从0开始
             $highestRow = $currentSheet->getHighestRow();
             // 取得总行数
             $highestColumn = $currentSheet->getHighestColumn();
             //取得总列数 比如 ‘AJ'列这样子
             //                echo $currentSheet->getTitle() . ' ' . $highestRow . '<br/>'; // 得到当前工作表的名字
             //读取一个工作表的数据 每次读取一行,再在行中循环每列的数值
             for ($row = $configArr['beginRow']; $row <= $highestRow; $row++) {
                 $listRow = array();
                 for ($column = self::BEGIN_COL; $column <= $arr[$highestColumn]; $column++) {
                     $val = $currentSheet->getCellByColumnAndRow($column, $row)->getValue();
                     $listRow[] = $val;
                     $listRow['row'] = $row;
                 }
                 //                    print_r($listRow);
                 //检查数据的完整性
                 if (!self::checkDataComplete($configArr['schoolType'], $listRow, $attributesFlip)) {
                     $empty = array();
                     $empty['sheetName'] = $currentSheet->getTitle();
                     $empty['row'] = $row;
                     $emptyErrorArr[] = $empty;
                     unset($empty);
                 }
                 $model->tableName = $tableName;
                 $listRow[$attributesFlip['sex']] = $sexArr[trim($listRow[$attributesFlip['sex']])];
                 $listRow[$attributesFlip['BMIGrade']] = $BMIArr[trim($listRow[$attributesFlip['BMIGrade']])];
                 $listRow[$attributesFlip['FVCGrade']] = $otherArr[trim($listRow[$attributesFlip['FVCGrade']])];
                 $listRow[$attributesFlip['_50mGrade']] = $otherArr[trim($listRow[$attributesFlip['_50mGrade']])];
                 $listRow[$attributesFlip['SARGrade']] = $otherArr[trim($listRow[$attributesFlip['SARGrade']])];
                 $listRow[$attributesFlip['SLJGrade']] = $otherArr[trim($listRow[$attributesFlip['SLJGrade']])];
                 $listRow[$attributesFlip['ChinUpGrade']] = $otherArr[trim($listRow[$attributesFlip['ChinUpGrade']])];
                 $listRow[$attributesFlip['SitUpsGrade']] = $otherArr[trim($listRow[$attributesFlip['SitUpsGrade']])];
                 $listRow[$attributesFlip['_1000mGrade']] = $otherArr[trim($listRow[$attributesFlip['_1000mGrade']])];
                 $listRow[$attributesFlip['_800mGrade']] = $otherArr[trim($listRow[$attributesFlip['_800mGrade']])];
                 $listRow[$attributesFlip['_400mGrade']] = $otherArr[trim($listRow[$attributesFlip['_400mGrade']])];
                 $listRow[$attributesFlip['ropeSkippingGrade']] = $otherArr[trim($listRow[$attributesFlip['ropeSkippingGrade']])];
                 $listRow[$attributesFlip['SumUpGrade']] = $otherArr[trim($listRow[$attributesFlip['SumUpGrade']])];
                 $user->stuId = trim($listRow[$attributesFlip['stuId']]);
                 $user->password = sha1(substr(trim($listRow[$attributesFlip['stuId']]), -6));
                 $sqlParams[] = array('userFlag' => true, 'sql' => $user->create(true), 'sheetName' => $currentSheet->getTitle(), 'row' => $listRow['row']);
                 foreach ($attributes as $key => $attr) {
                     $listRow[$key] = str_replace(' ', '', trim($listRow[$key]));
                     $model->{$attr} = $listRow[$key];
                     if (empty($model->{$attr})) {
                         $model->{$attr} = 0;
                     }
                 }
                 //                    print_r($model);
                 $model->stuName = mb_substr($model->stuName, mb_strlen($model->stuName) - 2);
                 $model->stuName = "*" . $model->stuName;
                 //将 姓氏 转化成*
                 $sqlParams[] = array('userFlag' => false, 'sql' => $model->create(true), 'sheetName' => $currentSheet->getTitle(), 'row' => $listRow['row']);
                 unset($listRow);
                 //每个页面每1000行提交一次
                 if ($row % 1000 == 0 || $row == $highestRow) {
                     $model->multipleExecute($sqlParams, false, $errorRowArr, $affectedRows);
                     unset($sqlParams);
                     if (count($errorRowArr) && !empty($errorRowArr[0]['sheetName'])) {
                         $totalInsertErrorArr[] = $errorRowArr;
                         unset($errorRowArr);
                     }
                 }
             }
             //记录数据不完整的行
             if (count($emptyErrorArr)) {
                 $totalEmptyErrorArr[] = $emptyErrorArr;
                 $emptyErrorArr = array();
             }
         }
         /*if (count($totalEmptyErrorArr)) {
               echo '有' . count($totalEmptyErrorArr) . '条数据不完整';
               print_r($totalEmptyErrorArr);
           }
           if (count($totalInsertErrorArr)) {
               echo '有' . count($totalInsertErrorArr) . '条数据插入失败';
               var_dump($totalInsertErrorArr);
           }*/
     }
     $currentYear = date('Y');
     $yearArr = array($currentYear - 1 . '-' . $currentYear, $currentYear . '-' . ($currentYear + 1), $currentYear - 2 . '-' . ($currentYear - 1));
     $this->render('data/upload', array('yearArr' => $yearArr, 'totalEmptyErrorArr' => $totalEmptyErrorArr, 'totalInsertErrorArr' => $totalInsertErrorArr, 'post' => isset($_POST['send']) ? true : false, 'affectedRows' => $affectedRows, 'usedTime' => sprintf("%0.2f", microtime(true) - $beginTime)));
 }