Jump to content

How to loop and increase column number...?


nerd99

Recommended Posts

For the section of code below, is there a way to say, "first time through the query print on A2, B2,etc, then each query following add increase the number besides column name to be A3, B3, etc, then A4, B4, etc"...?

 

$result = mysqli_query($connection,"SELECT * FROM table")
or die(mysqli_error($connection));

while($row = mysqli_fetch_array($result)) {

//get all rows you want from the table

$var1 = $row['var1'];
$var2 = $row['var2'];
$var3 = $row['var3'];
$var4 = $row['var4'];

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A2', $var1)
            ->setCellValue('B2', $var2)
            ->setCellValue('C2', $var3)
            ->setCellValue('D2', $var4);

}

 

This is for an excel spreadsheet. The code is currently printing/exporting one row only from the table.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/210901-how-to-loop-and-increase-column-number/
Share on other sites

Does this do what you want?

 

$result = mysqli_query($connection,"SELECT * FROM table")
or die(mysqli_error($connection));

$index = 2;

while($row = mysqli_fetch_array($result)) {

//get all rows you want from the table

$var1 = $row['var1'];
$var2 = $row['var2'];
$var3 = $row['var3'];
$var4 = $row['var4'];

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A' . $index, $var1)
            ->setCellValue('B' . $index, $var2)
            ->setCellValue('C' . $index, $var3)
            ->setCellValue('D' . $index, $var4);

$index++;

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.