我想要使用PHPExcel将一些SQL数据传递给Excel文档。
共有3个表格:
书籍:
1.1 bookID
1.2 bookName
标签:
2.1 tagID
2.2 tagName
Post_Tags:
3.1 id
3.2 tagID
3.3 bookID
我希望标签位于xls工作表的第二列,用逗号分隔。所以我在while循环中有一个while循环。
问题:
在制作的Excel工作表中,并非所有书籍标签都列在每个B列的单元格中,而只列在一个标签中。
任何想法为什么?在此先感谢:)
require_once "includes/config.php";
require_once "includes/functions.php";
require_once 'phptoxls/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$F = $objPHPExcel -> getActiveSheet();
/* START: RECORDS */
$line = 2;
$query = mysql_query('SELECT * FROM `books`') or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
$bookID = $row['bookID'];
$bookName = $row['bookName'];
$query2 = mysql_query('SELECT * FROM `books`
INNER JOIN `post_tags`
ON post_tags.bookID = books.bookID
INNER JOIN `tags`
ON tags.tagID = post_tags.tagID
WHERE books.bookID = "'.$bookID.'"') or die(mysql_error());
if(mysql_num_rows($query2) > 0){
$rowNum = mysql_num_rows($query2);
$i = 1;
while($row1 = mysql_fetch_assoc($query2)){
$tags = $row1['tagName'];
if($i < $rowNum){
$tags .= ', ';
}
}
}
$F -> setCellValue('A'.$line, $bookName)
-> setCellValue('B'.$line, $tags);
++$line;
}
/* END: RECORDS */
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="books.xls');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
答案 0 :(得分:0)
这就是你的问题:
while($row1 = mysql_fetch_assoc($query2)){
$tags = $row1['tagName'];
您正在重置每个循环的$ tag值。要使用您提供的代码,只需执行以下操作:
if(mysql_num_rows($query2) > 0){
$rowNum = mysql_num_rows($query2);
$i = 1;
$tags = "";
while($row1 = mysql_fetch_assoc($query2)){
$tags .= $row1['tagName'];
if($i < $rowNum){
$tags .= ', ';
}
}
}
但我建议您通过这样做来解决问题:
if(mysql_num_rows($query2) > 0){
while($row1 = mysql_fetch_assoc($query2)){
$tags[] = $row1['tagName'];
}
$tags = implode(', ',$tags);
}
答案 1 :(得分:0)
首先请更好地缩进代码,因为它几乎不可读(http://beta.phpformatter.com)。第二:我认为你只是用',' - s连接标签。在你的第一次开始时认真对待Dude:
<?php
require_once "includes/config.php";
require_once "includes/functions.php";
require_once 'phptoxls/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$F = $objPHPExcel->getActiveSheet();
/* START: RECORDS */
$line = 2;
$query = mysql_query('SELECT * FROM `books`') or die(mysql_error());
while ($row = mysql_fetch_assoc($query)) {
$all_tags_for_one_row = '';
$bookID = $row['bookID'];
$bookName = $row['bookName'];
$query2 = mysql_query('SELECT * FROM `books`
INNER JOIN `post_tags`
ON post_tags.bookID = books.bookID
INNER JOIN `tags`
ON tags.tagID = post_tags.tagID
WHERE books.bookID = "' . $bookID . '"') or die(mysql_error());
if (mysql_num_rows($query2) > 0) {
$rowNum = mysql_num_rows($query2);
$i = 1;
while ($row1 = mysql_fetch_assoc($query2)) {
$tags = $row1['tagName'];
if ($i < $rowNum) {
$all_tags_for_one_row .= ($i != $rowNum ? $tags . ', ' : $tags );
}
$i++;
}
}
$F->setCellValue('A' . $line, $bookName);
$F->setCellValue('B' . $line, $all_tags_for_one_row);
++$line;
}
/* END: RECORDS */
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="books.xls');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>