简体   繁体   English

PHPExcel | while循环内的while循环无法正常工作

[英]PHPExcel | While loop inside while loop doesn't work properly

I have some SQL data that I want to pass to an Excel document using PHPExcel. 我有一些要使用PHPExcel传递到Excel文档的SQL数据。

There are 3 tables: 有3个表:

  1. Books: 图书:

    1.1 bookID 1.1 bookID

    1.2 bookName 1.2书名

  2. Tags: 标签:

    2.1 tagID 2.1 tagID

    2.2 tagName 2.2 tagName

  3. Post_Tags: 后标签:

    3.1 id 3.1 ID

    3.2 tagID 3.2 tagID

    3.3 bookID 3.3 bookID

I want the tags to be in the second column of the xls sheet, seperated by commas. 我希望标签在xls表格的第二列中,以逗号分隔。 So here I have a while loop inside a while loop. 所以这里我在while循环中有一个while循环。

The problem: 问题:

In the produced excel sheet, not all the book's tags are listed in each column B's cell, but only a single tag. 在产生的excel表格中,不是每列B的单元格中都列出了所有书籍的标签,而是仅列出了一个标签。

Any ideas why? 有什么想法吗? Thanks in advance :) 提前致谢 :)

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;

Here lies your issue: 这是您的问题:

while($row1 = mysql_fetch_assoc($query2)){
    $tags = $row1['tagName'];

You are resetting the value of $tags with every loop. 您将在每个循环中重置$ tags的值。 To get it working with the code you supplied simply do the following: 要使其与您提供的代码一起使用,只需执行以下操作:

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 .= ', ';
        }
    }
}

I would however suggest you fix it by doing this: 但是,我建议您通过执行以下操作来修复它:

if(mysql_num_rows($query2) > 0){
    while($row1 = mysql_fetch_assoc($query2)){
        $tags[] = $row1['tagName'];
    }
    $tags = implode(', ',$tags);
}

First of all please indent your code better, because it's almost unreadable( http://beta.phpformatter.com ). 首先,请更好地缩进代码,因为它几乎不可读( http://beta.phpformatter.com )。 Second: I think you just concatenating the tags with ','-s. 第二:我想您只是将标签与','-s连接在一起。 Dude seriously at the beginning of your 1st while: 杜德(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;
?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM