추천인 엑셀출력 관련입니다.
본문
<?php
include_once('./_common.php');
function column_char($i) { return chr( 65 + $i ); }
include_once(G5_LIB_PATH.'/PHPExcel.php');
// $bo_table = $_GET["bo_table"];
$bo_table = "voice";
// $wr_id = $_GET["wr_id"];
$headers = array('wr_id','작성일시','이름', 'ID', '정책제안','추천수','추천인');
$widths = array(10,30,30,30,100,10,100);
$header_bgcolor = 'FFABCDEF';
$last_char = column_char(count($headers) - 1);
$sql = "select * from g5_write_{$bo_table} where wr_is_comment = 0 order by wr_id desc";
$result = sql_query($sql);
for($i=1; $row=sql_fetch_array($result); $i++) {
$sql1 = "select mb_id from g5_board_good where bo_table = '{$bo_table}' and wr_id = '{$row[wr_id]}' and bg_flag = 'good'";
$result1 = sql_query($sql1);
while($row1 = sql_fetch_array($result1)) {
$r_id[] = $row1['mb_id'];
}
$r_list = implode(",", $r_id);
$rows[] =
array(
$row['wr_id'],
$row['wr_datetime'],
$row['wr_name'],
$row['mb_id'],
$row['wr_subject'],
$row['wr_good'],$r_list
);
}
$data = array_merge(array($headers), $rows);
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0)->getStyle( "A1:${last_char}1" )->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB($header_bgcolor);
$excel->setActiveSheetIndex(0)->getStyle( "A:$last_char" )->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)->setWrapText(true);
foreach($widths as $i => $w) $excel->setActiveSheetIndex(0)->getColumnDimension( column_char($i) )->setWidth($w);
$excel->getActiveSheet()->fromArray($data,NULL,'A1');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"voice_good-".date("ymd", time()).".xls\"");
header("Cache-Control: max-age=0");
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$writer->save('php://output');
?>
이렇게 파일을 생성하고 엑셀로 출력을 해봤는데요
이런식으로 추천인이 누적으로 뽑아져서 나와요
도와주세요 ㅠㅠ
!-->
답변 4
for($i=1; $row=sql_fetch_array($result); $i++) {
$sql1 = "select mb_id from g5_board_good where bo_table = '{$bo_table}' and wr_id = '{$row[wr_id]}' and bg_flag = 'good'";
$result1 = sql_query($sql1);
while($row1 = sql_fetch_array($result1)) {
$r_id[] = $row1['mb_id'];
}
$r_list = implode(",", $r_id);
$rows[] =
array(
$row['wr_id'],
$row['wr_datetime'],
$row['wr_name'],
$row['mb_id'],
$row['wr_subject'],
$row['wr_good'],$r_list
);
unset($r_id); // 추가
}
$rows[] 를 다시 돌려두시고
unset으로 $r_id 배열을 초기화 해보세요.
!-->
$rows[] =
array(
$row['wr_id'],
$row['wr_datetime'],
$row['wr_name'],
$row['mb_id'],
$row['wr_subject'],
$row['wr_good'],$r_list
);
해당 코드에서 $rows를 2차원 배열로 선언하고
해당 배열을 2차원 배열로 계속 사용하기 때문에 그렇습니다.
$rows =
array(
$row['wr_id'],
$row['wr_datetime'],
$row['wr_name'],
$row['mb_id'],
$row['wr_subject'],
$row['wr_good'],$r_list
);
$rows[] => $rows 로 변경해보세요.
!-->!-->
for($i=1; $row=sql_fetch_array($result); $i++) {
$sql1 = "select mb_id from g5_board_good where bo_table = '{$bo_table}' and wr_id = '{$row[wr_id]}' and bg_flag = 'good'";
$result1 = sql_query($sql1);
$r_id=array(); // 추가
while($row1 = sql_fetch_array($result1)) {
$r_id[] = $row1['mb_id'];
}
$r_list = implode(",", $r_id);
$rows[] =
array(
$row['wr_id'],
$row['wr_datetime'],
$row['wr_name'],
$row['mb_id'],
$row['wr_subject'],
$row['wr_good'],$r_list
);
}
다른 방법
for($i=1; $row=sql_fetch_array($result); $i++) {
$sql1 = "select ifnull( group_concat(mb_id),'') id_set from g5_board_good where bo_table = '{$bo_table}' and wr_id = '{$row[wr_id]}' and bg_flag = 'good'";
$tmp = sql_fetch($sql1);
$r_list= $tmp['id_set'];
$r_list = implode(",", $r_id);
$rows[] =
array(
$row['wr_id'],
$row['wr_datetime'],
$row['wr_name'],
$row['mb_id'],
$row['wr_subject'],
$row['wr_good'],$r_list
);
}
답변을 작성하시기 전에 로그인 해주세요.