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
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++;

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.