Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel MySQL Export w/2 querys

I've started using PHPExcel, and I have to say, it's a wonderful piece of software. However, I can't get pass through this piece of code that is below written, and I don't know how to act.

<?php
   $query1 = "SELECT DISTINCT id FROM table_name;";
   $result1 = mysql_query($query1);
   $registers1 = mysql_fetch_object($result1);

   $row = 15;
   $row2 = 16;
   $row3 = 17;
   $row4 = 18;
   $col = 1; // B

   for ($i = 1; $i <= $registers1; $i++) {
      while ($row_x = mysql_fetch_object($result1)) {
         $startColumn = $col;
         $endColumn = $col+5;
         $range = PHPExcel_Cell::stringFromColumnIndex($startColumn) . $row.':' .
                  PHPExcel_Cell::stringFromColumnIndex($endColumn) . $row2;
         $objPHPExcel->getActiveSheet()->mergeCells($range);
         // Print rest of cells.

         $query2 = "SELECT * FROM table2_name WHERE id = '".$registers1->id."' ORDER BY ID;";
         $result2 = mysql_query($query2);

         while ($row_y = mysql_fetch_object($result2)) {
            $objPHPExcel->getActiveSheet()->setCellValue('B'.$row4, $row_y->value_xyz);
            // Print rest of cells.
            $row4++;
         }
      $row += 20;
      $row2 += 20;
      $row3 += 20;
      }
   }

?>

Now, as you can see, there are two different querys on that code. I want to dump these results onto an XLS page (that is already created with PHPExcel routines) that will show:

  • id
    • Values for that id

(spaces)

  • id
    • Values for that id

and so on. I know that this question might be something odd to ask (aka easy to solve), but I can't stare at this code any more. Any ideas? Thanks all.

like image 592
AleksanderKseniya Avatar asked Nov 04 '22 10:11

AleksanderKseniya


1 Answers

Finally, I've come across the solution.

Looks like I was doubling the loops without needing to. Once that this was solved, the rest was iterating the values and deleting rows instead of adding those.

Thanks anyway for all the help.

like image 164
AleksanderKseniya Avatar answered Nov 08 '22 14:11

AleksanderKseniya