Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在每个现有数组元素之间添加数组元素_Php_Mysql_Arrays_Phpexcel - Fatal编程技术网

Php 在每个现有数组元素之间添加数组元素

Php 在每个现有数组元素之间添加数组元素,php,mysql,arrays,phpexcel,Php,Mysql,Arrays,Phpexcel,我试图通过从MySql获取数据在Excel中创建一个报告。我需要从数据库中获取主题标题,并将其作为列输出到excel表中,例如单元格:D5、E5、F5。。。M5等 到目前为止,我已经成功地做到了这一点,因此单元格下的显示包含如下标题: D5:数学,E5:英语,F5:物理等 | A | B | C | D | E | F | G | H | ---------------------------------------------------

我试图通过从MySql获取数据在Excel中创建一个报告。我需要从数据库中获取主题标题,并将其作为列输出到excel表中,例如单元格:D5、E5、F5。。。M5等

到目前为止,我已经成功地做到了这一点,因此单元格下的显示包含如下标题:

D5:数学,E5:英语,F5:物理等

  | A | B | C | D           | E       | F       | G   | H   |
--------------------------------------------------------------
1 |   |   |   |             |         |         |     |     |
...
5 |   |   |   | Mathematics | English | Physics | ... | ... |
这很好。挑战是,我需要在每个
科目
之间插入一个
等级
标题,如下所示:

  | A | B | C | D           | E     | F       | G     | H       | I     | J   | K   |
--------------------------------------------------------------------------------------
1 |   |   |   |             |       |         |       |         |       |     |     |
...
5 |   |   |   | Mathematics | GRADE | English | GRADE | Physics | GRADE | ... | ... |
$stringindex = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";    // For the excel column cells
$columns = str_split($stringindex);             // create array elements
$rowIndex = 5;      // start at row 5 in Excel
$columnIndex = 3;   // start at column D in Excel
$subjects = array();

while($sub_row = mysqli_fetch_array($resub, MYSQLI_ASSOC)) {
    $subject[] = $sub_row['name']; // Get the subjects from the database and put them in an array
}

$sheet = $objPHPExcel->setActiveSheetIndex(0); // PHPExcel functions
foreach($subject as $index => $subjectName) { // GET SUBJECTS
    $columnName = getColumnName($columnIndex, $columns);
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnName)->setAutoSize(true); // Sets Column Width
    $sheet->setCellValue($columnName.$rowIndex, $subjectName); // Lists Subjects as columns
    $columnIndex++;

    $columnName = getColumnName($columnIndex, $columns);
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnName)->setAutoSize(true); // Sets Column Width
    $sheet->setCellValue($columnName.$rowIndex, "GRADE"); 
    $columnIndex++;
} // END of FOREACH LOOP

function getColumnName($index, $columns) {
    $columnName = "";
    $numColumns = count($columns);
    while($index >= $numColumns) {
        $columnName = $columns[($index % $numColumns)] . $columnName;
        $index = floor($index / $numColumns) - 1;
    }
    $columnName = $columns[($index % $numColumns)] . $columnName;
    return $columnName;
}
以下是我目前掌握的情况:

$stringindex = "DEFGHIJKLMNOPQRSTUVWXYZ"; // For the excel column cells
$arr_index = str_split($stringindex); // create array elements
$arr_val2 = array(); // create array
$subject = array();
$subname2 = array();


while($sub_row = mysqli_fetch_array($resub, MYSQLI_ASSOC)) {
    $subject[] = $sub_row['name']; // Get the subjects from the database and put them in an array
}

foreach($subject as $sub => $subname) {
    $subkey[] = $sub; // array of subject keys e.g 0|1|2|3
    $subname2[] = $subname; // array of subjects
}

foreach($arr_index as $arr => $arr_val) {
    $arr_val2[] = $arr_val; // array of letters e.g D|E|F|G
}

$acount = count($subname2);
$bcount = count($arr_val2);

$size = ($acount > $bcount) ? $bcount : $acount;
$a = array_slice($subname2, 0, $size);
$b = array_slice($arr_val2, 0, $size);

$combine = array_combine($a, $b);

$sheet = $objPHPExcel->setActiveSheetIndex(0); // PHPExcel functions

foreach($combine as $key => $val) { // GET SUBJECTS
     $objPHPExcel->getActiveSheet()->getColumnDimension($val)->setAutoSize(true); // Sets Column Width

     $sheet
        ->setCellValue($val.'5', $key); // Lists Subjects as columns

} // END of FOREACH LOOP
上述代码能够在excel中将主题显示为列标题:

问题: 如何添加代码以在每个主题标题后添加成绩列

我希望你能给我指出正确的方向,因为我现在被卡住了

提前感谢。

$temp=true

然后在通过列的循环中:

if($temp==true){
    //do stuff
    $temp=false;
}else{
    //do other stuff
    $temp=true;
}

您可以尝试以下方法:

  | A | B | C | D           | E     | F       | G     | H       | I     | J   | K   |
--------------------------------------------------------------------------------------
1 |   |   |   |             |       |         |       |         |       |     |     |
...
5 |   |   |   | Mathematics | GRADE | English | GRADE | Physics | GRADE | ... | ... |
$stringindex = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";    // For the excel column cells
$columns = str_split($stringindex);             // create array elements
$rowIndex = 5;      // start at row 5 in Excel
$columnIndex = 3;   // start at column D in Excel
$subjects = array();

while($sub_row = mysqli_fetch_array($resub, MYSQLI_ASSOC)) {
    $subject[] = $sub_row['name']; // Get the subjects from the database and put them in an array
}

$sheet = $objPHPExcel->setActiveSheetIndex(0); // PHPExcel functions
foreach($subject as $index => $subjectName) { // GET SUBJECTS
    $columnName = getColumnName($columnIndex, $columns);
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnName)->setAutoSize(true); // Sets Column Width
    $sheet->setCellValue($columnName.$rowIndex, $subjectName); // Lists Subjects as columns
    $columnIndex++;

    $columnName = getColumnName($columnIndex, $columns);
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnName)->setAutoSize(true); // Sets Column Width
    $sheet->setCellValue($columnName.$rowIndex, "GRADE"); 
    $columnIndex++;
} // END of FOREACH LOOP

function getColumnName($index, $columns) {
    $columnName = "";
    $numColumns = count($columns);
    while($index >= $numColumns) {
        $columnName = $columns[($index % $numColumns)] . $columnName;
        $index = floor($index / $numColumns) - 1;
    }
    $columnName = $columns[($index % $numColumns)] . $columnName;
    return $columnName;
}

我添加了一个函数getColumnName(),它将为您提供“AA”、“AB”。。。如果你的科目和成绩栏超过A-Z限制。

假设你每个学生有一行,如果不是成绩,你会在每个“科目”栏中放什么?@Squig,是的,每个学生有一行,在每个科目下我显示百分比标记,例如95,然后在一年级下显示A,大概,为了对百分比(例如高、低、平均、中位数…)进行计算,您希望将它们保存在单独的列中(而不是将
95%(A)
之类的内容保存在单个列中)?您知道吗?您的代码似乎解决了我的问题…初步测试表明它在我试图实现的目标方面具有潜力…让我再测试一下,然后我会对此竖起大拇指。在将代码放入锅中并添加更多成分后,它对我起了作用,再次感谢您为我指明了正确的方向。我认为这个问题已经解决了。当然,你可以用PHPExcel内置的PHPExcel文件::StrugFieldCopyNoXixType()方法替换所有的GealCopnNAME省()内容。或者利用PHP的字符增量($columnName='A';$columnName++;)