Здравствуйте! Есть электронный школьный журнал, который позволяет выгружать оценки в .xls файл. Но недавно появились сдвоенные уроки в расписании и поэтому понадобилось, чтобы выгружались дублирующиеся даты, то есть, если в один день были 2 одинаковых урока, то за каждый из них выставляется оценка и нужно выгружать именно 2 эти даты с оценками. Пока не могу добиться результата, запутался с циклами. Буду благодарен, если кто поможет с кодом или подскажет какой фрагмент поправить, чтобы все остальное работало. download.php Код (Text): $sql="SELECT sol.student_id, s.last_name, s.first_name, s.middle_name, l.lesson_date, sol.grade FROM `students_on_lesson` AS sol JOIN students_in_class AS sic ON sic.student_id = sol.student_id JOIN classes AS c ON c.class_id = sic.class_id JOIN lessons AS l ON l.lesson_id = sol.lesson_id JOIN students AS s ON s.student_id = sol.student_id WHERE c.class_id={$_POST['class_id']} AND sol.subj_id={$_POST['subject_id']}"; $sql="Select * from classes where class_id={$_POST['class_id']}"; $res=mysql_query($sql); $names=mysql_fetch_array($res); $sql="SELECT s.student_id, s.last_name, s.first_name, s.middle_name FROM `classes` as c join students_in_class as sic on sic.class_id=c.class_id join students as s on s.student_id=sic.student_id WHERE c.class_id={$_POST['class_id']} order by s.last_name"; $query_dates="SELECT * FROM lessons WHERE subject_id={$_POST['subject_id']} order by lesson_date"; $result_dates=mysql_query($query_dates); while ($dates=mysql_fetch_array($result_dates)) { $lessons_date[$dates['lesson_date']]=0; } $i=1; $query=$sql; $res=mysql_query($query); //Создание Excel документа------------------------------------------------------------------------------------------- include_once 'phpexcel/PHPExcel/IOFactory.php'; $objPHPExcel = PHPExcel_IOFactory::load("1.xls"); $objPHPExcel->setActiveSheetIndex(0); $aSheet = $objPHPExcel->getActiveSheet(); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 0, 1, $_POST['class_id'] ); /** select lesson by subject **/ $sqlLesson = "SELECT `d`.`discipline` FROM `disciplines` as `d`, `subjects` as `s` WHERE `s`.`discipline_id` = `d`.`discipline_id` AND `s`.`subject_id` = {$_POST['subject_id']} LIMIT 0, 1"; $lessonName=mysql_query($sqlLesson); $lessonName = array_shift(mysql_fetch_array($lessonName)); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 1, 1, $lessonName ); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 9, 1, "Ведомость успеваемости для {$names['class']}-{$names['letter']} класса" ); $sqlTeacher = "SELECT `t`.`last_name`, `t`.`first_name`, `t`.`middle_name` FROM `teachers` as `t`, `subjects` as `s` WHERE `s`.`teacher_id` = `t`.`teacher_id` AND `s`.`subject_id` = {$_POST['subject_id']} LIMIT 0, 1"; $teacherInfo=mysql_query($sqlTeacher); $teacherInfo = mysql_fetch_array($teacherInfo); $fioTeacher = $teacherInfo['last_name'] . " " . $teacherInfo['first_name'] . " " . $teacherInfo['middle_name']; $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 2, 1, $fioTeacher); $i=$i+2; $pos=0; while ($row=mysql_fetch_array($res)) { $students[]=$row['student_id']; $fio=$row['last_name']." ".$row['first_name']." ".$row['middle_name']; $sql2="SELECT s.student_id, l.lesson_date, sol.grade FROM `classes` as c left join students_in_class as sic on sic.class_id=c.class_id left join students as s on s.student_id=sic.student_id left join students_on_lesson as sol on sol.student_id=sic.student_id left join lessons as l on l.lesson_id=sol.lesson_id WHERE c.class_id={$_POST['class_id']} and s.student_id={$row['student_id']} AND sol.subj_id={$_POST['subject_id']} order by s.last_name, l.lesson_date"; $array[$row['student_id']]=$lessons_date; $res2=mysql_query($sql2); while($row2=mysql_fetch_array($res2)) { $array[$row2['student_id']][$row2['lesson_date']]=$row2['grade']; } foreach($array as $idStudent=>$lessonInfo){ $array[$idStudent]['grade'] = false; foreach($lessonInfo as $grade){ if(!empty($grade)){ $array[$idStudent]['grade'] = true; break 1; } } } if($array[$idStudent]['grade']){ $i++; $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 0, $i, iconv("cp1251", "utf-8", $row['student_id'] )); $pos++; $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 1, $i, iconv("cp1251", "utf-8", $pos )); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 2, $i, $fio); } } $i=2; $y=3; foreach($lessons_date as $key=>$lesson) { $i++; $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( $i, 3, iconv("cp1251", "utf-8", $key )); } $i=4; $y=3; foreach($students as $key=>$student_id) { if ($array[$student_id]['grade']){ foreach($lessons_date as $key2=>$lesson) { if ($array[$student_id][$key2]!="0") { $st_grade=$array[$student_id][$key2]; } else { $st_grade=''; } $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( $y, $i, $st_grade ); $y++; } $y=3; $i++; } } //Стили границ таблицы $styleArray = array( 'borders' => array( 'allborders' => array( 'style' => PHPExcel_Style_Border::BORDER_THIN, ), ), ); $objPHPExcel->getActiveSheet()->getStyle("A4:AI{$i}")->applyFromArray($styleArray); //Второй лист excel (здесь с датами все нормально) $objPHPExcel->setActiveSheetIndex(1); $aSheet = $objPHPExcel->getActiveSheet(); $sql="SELECT * FROM lessons WHERE subject_id={$_POST['subject_id']} ORDER BY lesson_date"; $res=mysql_query($sql); $i=2; while ($row=mysql_fetch_array($res)) { $i++; $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 0, $i, $row['lesson_id']); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 1, $i, $row['lesson_order']); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 2, $i, $row['lesson_date']); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 3, $i, $row['topic'] ); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 4, $i, $row['dz']); } //Стили границ таблицы $styleArray = array( 'borders' => array( 'allborders' => array( 'style' => PHPExcel_Style_Border::BORDER_THIN, ), ), ); $objPHPExcel->getActiveSheet()->getStyle("A4:E{$i}")->applyFromArray($styleArray); $objPHPExcel->setActiveSheetIndex(0);
Ладно, я понял, что тут все гуру. Только хоть кто-то бы по делу оставил ответ, а не указывал на очевидное