title: PHPExcel dynamically incrementing rows and columns
body: <p>I am currently using PHPExcel to output an order's information to Excel.</p>
  
  <p>I have the following foreach-statements that ultimate generate my data such as the city in which the order was made in, the restaurant name, etc. </p>
  
  <p>Pardon the nested foreach-loops - it was the only way I could nest through all that many cities and legal entities and restaurants to generate the data the customer wanted.</p>
  
  <p>Using an answer posted <a href="https://stackoverflow.com/a/2585231/1913389">here</a> which dynamically generates the row and column integer, I tried it in my code.</p>
  
  <pre><code>$row = 1; // 1-based index
  $col = 0;
  
  foreach ($this-&gt;data['total_by_city'] as $city_id =&gt; $total_city){
      $city_name = '';
      foreach ($this-&gt;data['total_by_legal_entities'] as $legal_entity_id =&gt; $total_legal_entity) {
          $legal_entity_name = '';
  
  
          foreach ($this-&gt;data['restaurant_by_legal_entities'][$legal_entity_id] as $restaurant_id) {
              $orders = $this-&gt;data['order_by_restaurants'][$restaurant_id];
              $restaurant_name = '';
              if ($orders)
              {
                  foreach ($orders as $order_id =&gt; $order)
                  {
                          $restaurant_name = $order['restaurant_name'];
                          $legal_entity_name = $order['legal_entity'];
                          $city_name = $order['city'];
                          echo $row . ", ". $col . "&lt;br&gt;";
                          $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValueByColumnAndRow($col, $row, $order['city']);
                          $col++;
                          $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValueByColumnAndRow($col, $row, $order['legal_entity']);
                          $col++;
                          $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValueByColumnAndRow($col, $row, $order['restaurant_name']);
                          $col++;
                          $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValueByColumnAndRow($col, $row, $order['payment_method']);
                          $col++;
                          $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValueByColumnAndRow($col, $row, $order['order_number']);
                          $col++;
                          $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValueByColumnAndRow($col, $row, $order['date_created']);
                          $col++;
                          $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValueByColumnAndRow($col, $row, $order['customer_name']);
                          $col++;
                          $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValueByColumnAndRow($col, $row, $order['order_type']);
                          $col++;
                          $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValueByColumnAndRow($col, $row, $order['amount']);
                          $row++;
                          echo $row . ", ". $col . "&lt;br&gt;";
                          $col = 0;
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>When I ran my code, and attempted to open the .xlsx file, I received a "corrupted Excel spreadsheet" error in Excel.</p>
  
  <p>To make sure that my rows and column (respectively) indices are correct, I printed them out:</p>
  
  <pre><code>1, 0
  2, 8
  2, 0
  3, 8
  3, 0
  4, 8
  4, 0
  5, 8
  5, 0
  6, 8
  6, 0
  7, 8
  7, 0
  8, 8
  8, 0
  9, 8
  9, 0
  10, 8
  10, 0
  11, 8
  11, 0
  12, 8
  12, 0
  13, 8
  13, 0
  14, 8
  14, 0
  15, 8
  15, 0
  16, 8
  </code></pre>
  
  <p>From that observation, I see that the rows and columns indices I am using are incorrect. They are both in the wrong location and reset/incremented incorrectly.</p>
  
  <p>My question is - how do I increment and reset my column and row indices correctly?</p>