title: Passing data from PHP class to PHPExcel via AJAX
body: <p>i got stuck with OOP PHP and json data. i'm not completely new to OOP, but i can't get my head around this. if anyone can please explain to me, would be great!</p>
  
  <p>i have the following grid object in PHP:</p>
  
  <pre><code>    Class Grid {
  
          var $data;
          var $joins;
          var $fields;
          var $where;
          var $table;
          var $groupBy;
          var $having;
          var $limit;
          var $order_by;
          var $sort;
          var $security;
          var $set;
          var $sql;
  
  ....
  
          // loads data into the grid
          function load() {
      ...
              // setup the sql - bring it all together
              $sql = "
                  SELECT $post[cols]
                  FROM `$table`
                  $joins
                  $where
                  $groupBy
                  $having
                  ORDER BY $order_by $sort
                  $limit
              ";
  
              $this-&gt;sql = $sql;
  
              // execute the sql, get back a multi dimensial array
              $rows = $this-&gt;_queryMulti($sql);
  
              // form an array of the data to send back
              $data = array();
              $data['rows'] = array();
              foreach($rows as $i=&gt;$row) {
                  foreach($row as $col=&gt;$cell) {
                      // use primary key if possible, other wise use index
                      $key = $primaryKey ? $row[$primaryKey] : $i;
                      // primary key has an _ infront becuase of google chrome re ordering JSON objects
                      //http://code.google.com/p/v8/issues/detail?id=164
                      $data['rows']["_".$key][$col] = $cell;
                  }
              }
  
      ...        
              $data['order_by'] = $order_by;
              $data['sort'] = $sort;
              $data['page'] = $page;
              $data['start'] = $startRow + 1;
              $data['end'] = $startRow + $nRowsShowing;
              $data['colData'] = $colData;
  
              $this-&gt;data = $data;
          }
  </code></pre>
  
  <p>and it's called by AJAX callgrid.php:</p>
  
  <pre><code>$grid-&gt;load();
          // here we need to add field in data[sql] = sql query, then we can pass it to toExcel() - how?
          echo json_encode($grid-&gt;data);
  </code></pre>
  
  <p>what i'm trying to get is to be able to export current sql query (it can be all or searched results) into Excel using PHPExcel. So i've got toExcel.php with function toexcel($query) - that will take a query and export it to excel.</p>
  
  <p>now - HOW do i pass sql query from grid to toexcel via AJAX?</p>
  
  <ol>
  <li><p>I understand that i need to add to $data(): </p>
  
  <p>$data['sql'] = $sql;</p></li>
  </ol>
  
  <p>what next?</p>
  
  <hr>
  
  <p>UPDATE:
  I'm using the following jquery grid:
  <a href="http://square-bracket.com/openjs" rel="noreferrer">http://square-bracket.com/openjs</a></p>
  
  <p>I understand that PHPExcel should be initiated either by grid or jquery </p>