title: Force Download CSV File
body: <p>I am trying to create a button that will force a CSV file download but for some reason I am not able to get it working. This is the code that I have:</p>
  
  <pre><code>public function export_to_csv($result) {
  
  $shtml="";
  
  for ($i=0; $i&lt;count($result); $i++){
  
  $shtml = $shtml.$result[$i]["order_number"]. "," .$result[$i]["first_name"]. "," .$result[$i]["middle_name"]. "," .$result[$i]["last_name"]. "," .$result[$i]["email"]. "," .$result[$i]["shipment_name"]. "," .$result[$i]["payment_name"]. "," .$result[$i]["created_on"]. "," .$result[$i]["modified_on"]. "," .$result[$i]["order_status"]. "," .$result[$i]["order_total"]. "," .$result[$i]["virtuemart_order_id"]. "\n";
  
  }
  
  Header('Content-Description: File Transfer');
  Header('Content-Type: application/force-download');
  Header('Content-Disposition: attachment; filename=pedidos.csv');
  
  }
  </code></pre>
  
  <p>The $result variable comes from here:</p>
  
  <pre><code>public function get_orders_k() {
  
  $db=JFactory::getDBO();
  
      $query = " select o.order_number, o.virtuemart_order_id, o.order_total, o.order_status, o.created_on, o.modified_on, u.first_name,u.middle_name,u.last_name "
      .',u.email, pm.payment_name, vsxlang.shipment_name ' .
      $from = $this-&gt;getOrdersListQuery();
  
  $db-&gt;setQuery($query);
  
  $result=$db-&gt;loadAssocList();
  
      if ( $result &gt; 0 )
      {
          $datos = VirtueMartModelKiala::export_to_csv($result);
  
      }else return 0;
  }
  </code></pre>
  
  <p>Im not sure even where to start looking. I have look over the internet on various ways of doing this and tried all of them and still can't manage to get it working. Please help me out! </p>
  
  <p>-Thanks</p>