Jump to content

Export Csv Module


zemacse

Recommended Posts

hi everybody!

 

I'v to create a module wich insert in a csv file, exported data (in that case my product) from an sql query.

 

The problem is that my brand new csv file (excel file) is full of html code and isn't implemented by my query's data.

 

here my code :

 

<?php

class exportDeclinaison extends Module
{

function __construct()
{

	global $cookie;
	$this->_cookie = $cookie;
	$this->name = 'exportDeclinaison';
	$this->tab = 'front_office_features';
	$this->version = '1.0';

	parent::__construct();

	$this->displayName = $this->l('Export Declinaison');
	$this->description = $this->l('Export your products and declinaisons in TXT or CSV file.');

}

function install()
{
	if (!parent::install())
		return false;
	return true;
}

public function uninstall()
{
	if (!parent::uninstall())
		return false;
	return true;
}



function getContent()
{

if(!isset($_POST['produit'])){
echo'Choisissez la cat&eacutegorie de produit d&eacutesir&eacutee :';
//__________Liste categorie

echo"<form action='#' method='POST'>";
	echo "<select name='category' onchange='javascript:submit(this)'>";
	mysql_connect("localhost","root","root");
	mysql_select_db("c1_ks");
	$res = mysql_query("SELECT * FROM ps_category_lang ORDER BY id_category");
	while($cat = mysql_fetch_assoc($res)){
		echo "<option value='".$cat["id_category"]."'>".$cat["name"]."</option>";
	}

echo"</select>";
	echo"</form>";
}

//__________Fin liste categorie


		//__________Liste prod
	if(isset($_POST['category'])){

		echo"<form action='#' method='POST'>";
	echo "<select name='produit' onchange='javascript:submit(this)'>";

	mysql_connect("localhost","root","root");
	mysql_select_db("c1_ks");

	$res = mysql_query('SELECT p.* , pl.* , m.name AS manufacturer_name, s.name AS supplier_name, t.rate, sp.reduction, sp.reduction_type, sp.from, sp.to
					FROM ps_product AS p

					LEFT JOIN ps_category_product AS cp ON p.id_product = cp.id_product
					LEFT JOIN ps_category_lang AS cl ON cl.id_category = cp.id_category
					LEFT JOIN ps_product_lang AS pl ON p.id_product = pl.id_product
					LEFT JOIN ps_manufacturer AS m ON p.id_manufacturer = m.id_manufacturer
					LEFT JOIN ps_supplier AS s ON p.id_supplier = s.id_supplier
					LEFT JOIN ps_tax AS t ON p.id_tax_rules_group = t.id_tax
					LEFT JOIN ps_specific_price AS sp ON p.id_product = sp.id_product
					WHERE cp.id_category ="'.$_POST['category'].'"order by cp.id_product');

	while($prod = mysql_fetch_assoc($res)){
		echo "<option value='".$prod["id_product"]."'>".$prod["name"]."</option>";
	}

echo "</select>";
	}
	if (isset($_POST['produit'])){

	// la variable qui va contenir les données CSV
$fputcsv = '';

// Nom du fichier final
$fileName = 'export-csv.csv';

$req= mysql_query('SELECT cat.* FROM ps_category AS cat');

if(mysql_num_rows($req) > 0)
{
$i = 0;

while($Row = mysql_fetch_assoc($req))
{
	$i++;

	// Si c'est la 1er boucle, on affiche le nom des champs pour avoir un titre pour chaque colonne
	if($i == 1)
	{
		foreach($Row as $clef => $valeur)
			$fputcsv .= trim($clef).';';

		$fputcsv = rtrim($fputcsv, ';');
		$fputcsv .= "\n";
	}

	// On parcours $Row et on ajoute chaque valeur à cette ligne
	foreach($Row as $clef => $valeur)
		$fputcsv .= trim($valeur).';';

	// Suppression du ; qui traine à la fin
	$fputcsv = rtrim($fputcsv, ';');

	// Saut de ligne
	$fputcsv .= "\n";

}

}
else
exit('Aucune donnée à enregistrer.');

// Entêtes (headers) PHP qui vont bien pour la création d'un fichier Excel CSV
header("Content-disposition: attachment; filename=".$fileName);
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: application/text/csv\n");
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
header("Expires: 0");

echo $fputcsv;
exit();
	}

//__________Fin liste prod

}
}
?>

 

and here my csv's content :

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
"   <head>"
"	   <meta http-equiv=""Content-Type"" content=""application/xhtml+xml"
"	   <meta name=""robots"" content=""NOFOLLOW, NOINDEX"" />"
"	   <link type=""text/css"" rel=""stylesheet"" href=""/js/jquery/datepicker/datepicker.css"" />"
"	   <link type=""text/css"" rel=""stylesheet"" href=""/css/admin.css"" />"
"	   <link type=""text/css"" rel=""stylesheet"" href=""/css/jquery.cluetip.css"" />"
"	   <link type=""text/css"" rel=""stylesheet"" href=""themes/flashyturtle/admin.css"" />"
"	   <title>PrestaShop&trade"
"	   <script type=""text/javascript"">"
"		   var helpboxes = 1"
"		   var roundMode = 2"
"	   </script>"
"	   <script type=""text/javascript"" src=""/js/jquery/jquery.min.js""></script>"
"	   <script type=""text/javascript"" src=""/js/jquery/jquery.hoverIntent.minified.js""></script>"
"	   <script type=""text/javascript"" src=""/js/jquery/jquery.cluetip.js""></script>"
"	   <script type=""text/javascript"" src=""/js/admin.js""></script>"
"	   <script type=""text/javascript"" src=""/js/toggle.js""></script>"
"	   <script type=""text/javascript"" src=""/js/tools.js""></script>"
"	   <link rel=""shortcut icon"" href=""/img/favicon.ico"" />"
"	   "
"	   <!--[if IE]>"
"	   <link type=""text/css"" rel=""stylesheet"" href=""/css/admin-ie.css"" />"
"	   <![endif]-->"
"	   <style type=""text/css"">"
"		   div#header_infos, div#header_infos a#header_shopname, div#header_infos a#header_logout, div#header_infos a#header_foaccess {"
"			   color:#383838"
"		   }"
"	   </style>"
"   </head>"
"   <body >"
"   <script>"
"	   var choose_language_trad = ""Choisissez une langue"""
"   </script>"
"   <div id=""top_container"">"
"	   <div id=""container"">"
"		   <div id=""header_infos""><span>"
"			   <a id=""header_shopname"" href=""index.php""><span>KS Motorcycles</span></a><br />"
"			   F.&nbsp"
"			   [ <a href=""index.php?logout"" id=""header_logout""><span>déconnexion</span></a> ]"
"			   - <a href=""/"" id=""header_foaccess"" target=""_blank"" title=""Voir mon magasin""><span>Voir mon magasin</span></a>"
"			   - <a href=""index.php?tab=AdminEmployees&id_employee=12&updateemployee&token=3f6c780568d15bf3b2ab7eba859afe3c"" style=""font-size: 10px"
"		   </span></div>"
"		   <div id=""header_search"">"
"			   <form method=""post"" action=""index.php?tab=AdminSearch&token=b0727016d10a7a3f2b2d208ab8e488cf"">"
"				   <input type=""text"" name=""bo_query"" id=""bo_query"""
"					   value="""""
"				   />"
"				   <select name=""bo_search_type"" id=""bo_search_type"">"
"					   <option value=""0"">tout</option>"
"					   <option value=""1"" >catalogue</option>"
"					   <option value=""2"" >clients</option>"
"					   <option value=""6"" >adresse ip</option>"
"					   <option value=""3"" >commandes</option>"
"					   <option value=""4"" >factures</option>"
"					   <option value=""5"" >paniers</option>"
"				   </select>"
"				   <input type=""submit"" id=""bo_search_submit"" class=""button"" value=""Recherche""/>"
"			   </form>"
"		   </div>"
"		   <div id=""header_quick"">"
"			   <script type=""text/javascript"">"
"			   function quickSelect(elt)"
"			   {"
"				   var eltVal = $(elt).val()"
"				   if (eltVal == ""0"") return false"
"				   else if (eltVal.substr(eltVal.length - 6) == ""_blank"") window.open(eltVal.substr(0, eltVal.length - 6), ""_blank"")"
"				   else location.href = eltVal"
"			   }"
"			   </script>"
"			   <select onchange=""quickSelect(this)"
"				   <option value=""0"">Accès rapide</option><option value=""index.php"">&gt"
"		   </div>"
"		   <div class=""flatclear"">&nbsp"
"		   <a href=""index.php?tab=AdminCatalog&token=843b8197b08ba10d165c51bd6a0faeb4"">"
"			   <img src=""/img/t/AdminCatalog.gif"" alt="""" /> Catalogue"
"		   </a>"
"	   </li><li class=""submenu_size "" id=""maintab2"">"
"		   <a href=""index.php?tab=AdminCustomers&token=bc2504c7ef458aadad5ecff721c3e23c"">"
"			   <img src=""/img/t/AdminCustomers.gif"" alt="""" /> Clients"
"		   </a>"
"	   </li><li class=""submenu_size "" id=""maintab3"">"
"		   <a href=""index.php?tab=AdminOrders&token=59cb173171d0d3dee1c8b08d022cb025"">"
"			   <img src=""/img/t/AdminOrders.gif"" alt="""" /> Commandes"
"		   </a>"
"	   </li><li class=""submenu_size "" id=""maintab4"">"
"		   <a href=""index.php?tab=AdminPayment&token=38c2ad9073d29e84e30176c9c4fe462d"">"
"			   <img src=""/img/t/AdminPayment.gif"" alt="""" /> Paiement"
"		   </a>"
"	   </li><li class=""submenu_size "" id=""maintab5"">"
"		   <a href=""index.php?tab=AdminShipping&token=dfc82a41d4e3927810e67f786185a533"">"
"			   <img src=""/img/t/AdminShipping.gif"" alt="""" /> Transport"
"		   </a>"
"	   </li><li class=""submenu_size "" id=""maintab6"">"
"		   <a href=""index.php?tab=AdminStats&token=54259ac1c2e43749100e327107f13127"">"
"			   <img src=""/img/t/AdminStats.gif"" alt="""" /> Stats"
"		   </a>"
"	   </li><li class=""submenu_size active"" id=""maintab7"">"
"		   <a href=""index.php?tab=AdminModules&token=62b0fc62be3a5d7599cb5b531fee4f86"">"
"			   <img src=""/img/t/AdminModules.gif"" alt="""" /> Modules"
"		   </a>"
"	   </li><li class=""submenu_size "" id=""maintab29"">"
"		   <a href=""index.php?tab=AdminEmployees&token=3f6c780568d15bf3b2ab7eba859afe3c"">"
"			   <img src=""/img/t/AdminEmployees.gif"" alt="""" /> Employés"
"		   </a>"
"	   </li><li class=""submenu_size "" id=""maintab8"">"
"		   <a href=""index.php?tab=AdminPreferences&token=7bb9a099e4a1c3110daf92300668a374"">"
"			   <img src=""/img/t/AdminPreferences.gif"" alt="""" /> Préférences"
"		   </a>"
"	   </li><li class=""submenu_size "" id=""maintab9"">"
"		   <a href=""index.php?tab=AdminTools&token=cec28aa16eec991a7ea89c380612b8b9"">"
"			   <img src=""/img/t/AdminTools.gif"" alt="""" /> Outils"
"		   </a>"
"	   </li>	 </ul><div id=""tab1_subtabs"" style=""display:none""><li><a href=""index.php?tab=AdminTracking&token=9f6a4bdb16b18c12db4af07fbc11ec87"">Suivi</a></li><li><a href=""index.php?tab=AdminManufacturers&token=741a6940440db5d47ae4b4d37ce37ac2"">Fabricants</a></li><li><a href=""index.php?tab=AdminSuppliers&token=14c0a3af54b4de2e5a37090c3992ae35"">Fournisseurs</a></li><li><a href=""index.php?tab=AdminAttributesGroups&token=217c16574d3bec59573a251c27800856"">Attributs et groupes</a></li><li><a href=""index.php?tab=AdminFeatures&token=377e29d6393d55b6694a7a8eb908a526"">Caractéristiques</a></li><li><a href=""index.php?tab=AdminScenes&token=74cdd5bd2184f4a82957ad95a7df08fd"">Scènes</a></li><li><a href=""index.php?tab=AdminTags&token=8f4a2dd17eac8471b26643493b1563b3"">Tags</a></li><li><a href=""index.php?tab=AdminAttachments&token=58c2ee13ba23a2cb40b5bb73d2a4f49b"">Documents joints</a></li><li><a href=""index.php?tab=AdminStockMvt&token=a5cad4d6954c16d6164075bb13eab781"">Mouvements de Stock</a></li><li><a href=""index.php?tab=AdminMiscEdit&token=65d82507b992df83767f0ed8c9b675dd"">Edition en liste des produits</a></li></div><div id=""tab2_subtabs"" style=""display:none""><li><a href=""index.php?tab=AdminAddresses&token=08837a46c8301912b8908bab340d55db"">Adresses</a></li><li><a href=""index.php?tab=AdminGroups&token=b9ce42a4ba5a3bfc3f7402cabfe25010"">Groupes</a></li><li><a href=""index.php?tab=AdminCarts&token=7f100d48f250e4bc0b1d40dee622e7a0"">Paniers</a></li><li><a href=""index.php?tab=AdminSuperUser&token=413775d974fac3e4e3d443cd2dcb8b42"">SuperUtilisateur</a></li></div><div id=""tab3_subtabs"" style=""display:none""><li><a href=""index.php?tab=AdminInvoices&token=f91da142bdd458b032f655b264f55c65"">Factures</a></li><li><a href=""index.php?tab=AdminDeliverySlip&token=7d48f66c5e903c97b22cb4628df67587"">Bons de livraison</a></li><li><a href=""index.php?tab=AdminReturn&token=7043fb834a7b997c0b8ea43fc67f7273"">Retours produits</a></li><li><a href=""index.php?tab=AdminSlip&token=a38292a79b89c4a82485fb34f777b7fa"">Avoirs</a></li><li><a href=""index.php?tab=AdminMessages&token=195551945731c945a61154cd49392aee"">Messages clients</a></li><li><a href=""index.php?tab=AdminStatuses&token=3157f391fbeb5c03dd489c2d2dda793b"">Statuts</a></li><li><a href=""index.php?tab=AdminOrderMessage&token=9e7513a1b7b6ffe0138a84f495bf6da0"">Messages prédéfinis</a></li><li><a href=""index.php?tab=AdminPDF&token=203f4c8c3eadcd0745edadd9ac86186e"">PDF</a></li><li><a href=""index.php?tab=AdminMondialRelay&token=84067e9d7c44b171f6073e8130bf8169"">Mondial Relay</a></li></div><div id=""tab4_subtabs"" style=""display:none""><li><a href=""index.php?tab=AdminCurrencies&token=b2d84a512b153610a561465d37fcb710"">Devises</a></li><li><a href=""index.php?tab=AdminTaxes&token=296f336b7766ef93adad30a794a76fe8"">Taxes</a></li><li><a href=""index.php?tab=AdminTaxRulesGroup&token=3e2c5ec046bcb1672fad92f2494db9ee"">Règles de taxes</a></li><li><a href=""index.php?tab=AdminDiscounts&token=2869c03df4df9aadd871f31f3197dcb5"">Bons de réduction</a></li></div><div id=""tab5_subtabs"" style=""display:none""><li><a href=""index.php?tab=AdminCarriers&token=a3835a7ed5faced6739021348e2cd7ca"">Transporteurs</a></li><li><a href=""index.php?tab=AdminStates&token=2e21bcba49cfb7c68c5c5ad112633eba"">Etats</a></li><li><a href=""index.php?tab=AdminCountries&token=ab90fd378a4396ce86789d7ddc4ec376"">Pays</a></li><li><a href=""index.php?tab=AdminCounty&token=c5f87bdd168c8bd4468ebcd15b625e1f"">Comtés</a></li><li><a href=""index.php?tab=AdminZones&token=efd72a88885391be568f60fb60d0e34f"">Zones</a></li><li><a href=""index.php?tab=AdminRangePrice&token=9c3ba029e17395f9a1259b7c361aca6c"">Tranches de prix</a></li><li><a href=""index.php?tab=AdminRangeWeight&token=c27264d826161788a86c64950c934e15"">Tranches de poids</a></li></div><div id=""tab6_subtabs"" style=""display:none""><li><a href=""index.php?tab=AdminStatsConf&token=15a159c9b764e353a534be6281c6f125"">Configuration</a></li><li><a href=""index.php?tab=AdminSearchEngines&token=f7e5cbc956b63cfde731ef470593f815"">Moteurs de recherche</a></li><li><a href=""index.php?tab=AdminReferrers&token=66a69ca7717dc1c2d2647b98d015befa"">Sites affluents</a></li></div><div id=""tab7_subtabs"" style=""display:none""><li><a href=""index.php?tab=AdminAddonsCatalog&token=9b3d1605d1dbb04eb5f888c93526cdc9"">Catalogue de modules et thèmes</a></li><li><a href=""index.php?tab=AdminAddonsMyAccount&token=5348c414a7b0228487844a6607639fe6"">Mon compte</a></li><li><a href=""index.php?tab=AdminThemes&token=fe751a7e73f7818527f1463c56897513"">Thèmes</a></li><li><a href=""index.php?tab=AdminModulesPositions&token=5173c2ac020f20c2a51f0c8ea024894a"">Positions</a></li></div><div id=""tab29_subtabs"" style=""display:none""><li><a href=""index.php?tab=AdminProfiles&token=5ee00712e6b2841ea9fe721927fa940f"">Profils</a></li><li><a href=""index.php?tab=AdminAccess&token=177a0bb09309da9a78a26d0846609301"">Permissions</a></li><li><a href=""index.php?tab=AdminTabs&token=88e5cceb79edad047bad7a917afa583f"">Onglets</a></li><li><a href=""index.php?tab=AdminContacts&token=628b7861e16d834a8940887f74bf2de0"">Contacts</a></li><li><a href=""index.php?tab=AdminCustomerThreads&token=dd38f841c80cf680e95990fbd1e2bf18"">SAV</a></li></div><div id=""tab8_subtabs"" style=""display:none""><li><a href=""index.php?tab=AdminContact&token=10edd973a15155bf5ef3a8e8a150cf33"">Coordonnées</a></li><li><a href=""index.php?tab=AdminAppearance&token=4a838f58b95a3793375394f199c14c10"">Apparence</a></li><li><a href=""index.php?tab=AdminMeta&token=938d02ef5e4020fecd472fb5305a1e07"">SEO & URLs</a></li><li><a href=""index.php?tab=AdminPPreferences&token=53082108b42a90802838daf076721e2e"">Produits</a></li><li><a href=""index.php?tab=AdminAliases&token=ca637d45223898fd1276a997371ccd28"">Alias</a></li><li><a href=""index.php?tab=AdminEmails&token=7f4c34d53ec7894842a760ee375a9d76"">Emails</a></li><li><a href=""index.php?tab=AdminImages&token=a28e30ba6d3ec76c6447eb324b1b1d13"">Images</a></li><li><a href=""index.php?tab=AdminDb&token=6fd6b0d0e2bf994fff52562eef0160e4"">Base de données</a></li><li><a href=""index.php?tab=AdminLocalization&token=f39cc01003d71ac7de32a163a8f0673b"">Localisation</a></li><li><a href=""index.php?tab=AdminSearchConf&token=4d4248340704367dc0051098aad9b3e0"">Recherche</a></li><li><a href=""index.php?tab=AdminPerformance&token=33db2146c30c81a5070346dec26b98e9"">Performances</a></li><li><a href=""index.php?tab=AdminGeolocation&token=b06f26c61226a16b339f0cc92a51bebf"">Géolocalisation</a></li></div><div id=""tab9_subtabs"" style=""display:none""><li><a href=""index.php?tab=AdminLanguages&token=0df22b35e1c32fef65114851d19a613d"">Langues</a></li><li><a href=""index.php?tab=AdminTranslations&token=07c8c70b18840c1e41926f4b0535c0cf"">Traductions</a></li><li><a href=""index.php?tab=AdminQuickAccesses&token=85b585540a4ff39a553c08b31dd34b48"">Accès rapide</a></li><li><a href=""index.php?tab=AdminImport&token=e0c701c158c4e8690e575ad5e4c35215"">Import</a></li><li><a href=""index.php?tab=AdminSubDomains&token=19698ec1cc95d98c96e3a26c8433e14b"">Sous domaines</a></li><li><a href=""index.php?tab=AdminBackup&token=b3250f62be8ebeb5b932cace47a6cc0b"">Sauvegarde BDD</a></li><li><a href=""index.php?tab=AdminCMSContent&token=de17a704a1e2a30dfa8ce839acfbef14"">CMS</a></li><li><a href=""index.php?tab=AdminGenerator&token=cbe1d968c57526afc84525502f5fbfb1"">Générateurs</a></li><li><a href=""index.php?tab=AdminInformation&token=a251fc578d9b7fc2dc299ff767ff95bb"">Informations</a></li><li><a href=""index.php?tab=AdminStores&token=e226404ccd950589ea166f33738ad857"">Magasins</a></li><li><a href=""index.php?tab=AdminWebservice&token=fc039012bc7cf0e364e302be4b3c3042"">Service web</a></li><li><a href=""index.php?tab=AdminLogs&token=27ff1c798dc5b0525e9d5826ec344bbd"">Log</a></li><li><a href=""index.php?tab=AdminSelfUpgrade&token=88324186e711ac05936d00dd7d5f246a"">1-Click Upgrade</a></li></div>	  <ul id=""submenu"" class=""withLeftBorder clearfix""><li><a href=""index.php?tab=AdminAddonsCatalog&token=9b3d1605d1dbb04eb5f888c93526cdc9"">Catalogue de modules et thèmes</a></li><li><a href=""index.php?tab=AdminAddonsMyAccount&token=5348c414a7b0228487844a6607639fe6"">Mon compte</a></li><li><a href=""index.php?tab=AdminThemes&token=fe751a7e73f7818527f1463c56897513"">Thèmes</a></li><li><a href=""index.php?tab=AdminModulesPositions&token=5173c2ac020f20c2a51f0c8ea024894a"">Positions</a></li></ul>"
"		   <div id=""main"">"
"			   <div id=""content"">"
"			   <script type=""text/javascript"">"

"   $(document).ready(function(){"
"	   $.ajax({"
"		   type : ""POST"","
"		   url: ""ajax.php"","
"		   data:{"
"			   ""helpAccess"":""1"","
"			   ""item"":""AdminModules"","
"			   ""isoUser"":""fr"","
"			   ""country"":""FR"","
"			   ""version"":""1.4.9"""
"		   },"
"		   async : true,"
"		   success: function(msg) {"
"			   $(""#help-button"").html(msg)"
"			   $(""#help-button"").fadeIn(""slow"")"
"		   }"
"	   })"
"   })"
"   <div id=""help-button"" class=""floatr"" style=""display: none"
"		<img src=""../img/admin/separator_breadcrum.png"" style=""margin-right:5px"" alt=""&gt"
"			   <div style=""float:left"
"				   <a href=""http://www.prestashop.com/"" target=""_blank"" style=""font-weight:700"
"				   <span style=""font-size:10px"">Temps de chargement : 0.135s</span>"
"			   </div>"
"			   <div style=""float:right"
"				   | <a href=""http://forge.prestashop.com"" target=""_blank"" class=""footer_link"">Bug Tracker</a>"
"				   | <a href=""http://www.prestashop.com/forums/"" target=""_blank"" class=""footer_link"">Forum</a>   "
"			   </div>"
"		   </div>"
"	   </div>"
"   </div>"
"   </body>"
</html>
</html>

 

I would like to now why my csv file is full of html content and not whith my datas and thanks you to help me if you've ever seen this problem before ;)

Edited by zemacse (see edit history)
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...