Julien
Julien

Reputation: 619

Php to Excel : fill the output Excel file with a loop

I want to create an Excel file (with PhpExcel) and fill it with the content of a MySQL query.

I only have one column so the result will look like this :

$sheet->setCellValueByColumnAndRow(0, $i, $content)

So I have to loop inside my query and create a counter to fill each row corresponding to each item of the column ptlum of my content.
So the goal is to have the following result :

1 AX001
2 AX003
3 AX012

The code is the one :

$column = 1;
while($data = mysql_fetch_assoc($result)) {

    $workbook = new PHPExcel;
    $sheet = $workbook->getActiveSheet();
    $sheet->setCellValueByColumnAndRow(0, $column, $data['ptlum']);
    //echo($column. " "  . $data['ptlum']. " ");
    $column = $column + 1; //or $column++; 

The problem is that my Excel file is empty.. If i put a number instead of $column in the setCellValueByColumnAndRow line it works. But the variable does not work.. On the other hand if I put "$column = 1;" inside the loop, my Excel file will always contain one only row..

Have you an idea ?

Thank you very much !

Upvotes: 2

Views: 2110

Answers (2)

Veve
Veve

Reputation: 6748

You just have to change the call to setCellValueByColumnAndRow for each column and increment it:

$sql = "SELECT ptlum FROM `ptlum` WHERE nomcom = 'AIGREFEUILLE' and RIGHT (date,4) < 2015 and RIGHT(date,4) > 0 ";
$result = mysql_query($sql);

$row = 1; // 1-based index
$column = 1;
while($data = mysql_fetch_assoc($result)) {
    $sheet->setCellValueByColumnAndRow($column, $row, $data['ptlum']);
    $column = $column + 1; //or $column++; if you prefer
}

As you see, you can retrieve the field/column you want with mysql_fetch_assoc, returning an associative array.

Also, you don't have to include the field(s) of your WHERE condition(s) in the SELECT.

Then finally, you should replace the deprecated mysql_* function by their equivalents mysqli_*, as explained here.

Edit:

For your "new" problem, this code should work:

$sql = "SELECT ptlum FROM `ptlum` WHERE nomcom = 'AIGREFEUILLE' and RIGHT (date,4) < 2015 and RIGHT(date,4) > 0 ";
$result = mysql_query($sql);

$row = 1; // 1-based index
$column = 1;
$workbook = new PHPExcel;
$sheet = $workbook->getActiveSheet();
while($data = mysql_fetch_assoc($result)) {
    $sheet->setCellValueByColumnAndRow($column, $row, $data['ptlum']);
    $column = $column + 1; //or $column++; if you prefer
}

First, don't instanciate your workbook and sheet in each loop, do it before, once.

Second, you had your arguments in the wrong order, it's column then row, not the inverse, as explicited in the method name.

Upvotes: 2

KotBehemot
KotBehemot

Reputation: 121

Maybe this is the thing you wanted:

$sql = "SELECT ptlum, RIGHT(date,4)  FROM `ptlum` WHERE nomcom = 'AIGREFEUILLE' and RIGHT (date,4) < 2015 and RIGHT(date,4) > 0 ";
$result = mysql_query($sql);

$i = 0;
while($data = mysql_fetch_assoc($result)) {
    $sheet->setCellValueByColumnAndRow(0, $i, $i+1); //1-based index
    $sheet->setCellValueByColumnAndRow(1, $i, $data['ptlum']);
    $i++;
}

Upvotes: 0

Related Questions