<?php
  require_once("models/config.php");
  if(isUserLoggedIn()) { 
    
  }else{
    header("Location: index.php"); die(); 
  }

$mysqli = new mysqli("localhost", "bdicentralserver_sc", "Myl-PGfSbzcf", "bdicentralserver_sc");

$resulta = $mysqli->query("SELECT p.SKU, p.MFRPartNumber, p.UPCNumber, CONCAT(p.ProductDescription1, ' ', p.ProductDescription2) AS description, fi.marca as marca, ci.nombre as categoria FROM pricefile_mx p INNER JOIN manufacturer_mx fi ON fi.id = p.id_marca INNER JOIN categorias_mx as ci on ci.id = p.id_subcategoria WHERE p.id_marca IN (SELECT manufacturer_id_xref FROM manufacturer_xref_user_mx WHERE user_id = ".$loggedInUser->user_id.") AND p.id_subcategoria IN (SELECT category_id FROM category_xref_user_mx WHERE user_id = ".$loggedInUser->user_id.")");
$timestamp = time();
        $filename = 'Export_excel_' . $timestamp . '.xls';
        
        header("Content-Type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=\"$filename\"");
echo "<table><thead><tr><th>SKU</th><th>MODELO</th><th>CODIGO DE BARRAS</th><th>MARCA</th><th>CATEGORIA</th></tr></thead>";
echo "<tbody>";
while ($row = $resulta->fetch_assoc()) {
  echo "<tr>";
  echo "<td>".$row["SKU"]."</td>";
  echo "<td>".$row["MFRPartNumber"]."</td>";
  echo "<td>".$row["UPCNumber"]."</td>";
  echo "<td>".utf8_encode($row["description"])."</td>";
  echo "<td>".$row["marca"]."</td>";
  echo "<td>".utf8_encode($row["categoria"])."</td>";
  echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?> 
        
         