MySQL to Excel with PHP, encoding problem

Discussion in 'PHP' started by ludwig, Dec 4, 2006.

  1. #1
    Hi guys,

    My friend is having problems when exporting the report to an excel file which needs to be downloaded by the user.

    The problem is that such characters as é are not correctly displayed. This letter is shown as й

    After the report is generated with an excel file, a mail should be send to the user which includes that file.

    Here is the code that he is using

    
    <?
    $header1 .= 'Référence (identifiant unique)'. "\t";
    $header1 .= "Nom de l'article". "\t";
    $header1 .= 'Description courte (150 caractères)'. "\t";
    $header1 .= 'Description longue (texte non limité)'. "\t";
    $header1 .= 'Nos conseils de pro'. "\t";
    $header1 .= 'TVA (en %)'. "\t";
    $header1 .= 'Prix unitaire TTC'. "\t";
    $header1 .= 'Prix unitaire HT'. "\t";
    $header1 .= 'Qauntity'. "\t";
    $header1 .= 'Disponibilité (oui/non)'. "\t";
    $header1 .= 'Conditionnement'. "\t";
    $header1 .= 'Poids (en grammes)'. "\t";
    $header1 .= 'Référence article chez fournisseur'. "\t";
    $header1 .= 'Color'. "\t";
    $header1 .= 'Famille'. "\t";
    $header1 .= 'Sous-Famille';
    
    
    $weight = 0;
    $price = 0;
    $query = mysql_query("SELECT * FROM TABLE_NAME WHERE user_id = '$user_id' AND delivery = '0' ORDER BY id");
    $q = mysql_num_rows($query);
    while ($row = mysql_fetch_array($query)) { 
    	$article_id = $row['article_id'] . '<br>';
    	$query_sub = mysql_query("SELECT * FROM TABLE_NAME_2 WHERE showrow != '0' AND id = '$article_id' AND lg = '$_GET[lg]' ORDER BY id");
    	while ($res = mysql_fetch_array($query_sub)) {
    		$price = $row['qauntity'] * $res['price_unit_ht'] + $price;
    		$weight = $row['qauntity'] * $res['weight'] + $weight;
    
    		$line = ''; 
    
    			if ((!isset($res['ref_num'])) || ($res['ref_num'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', $res['ref_num']); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    
    			if ((!isset($res['title'])) || ($res['title'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', $res['title']); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    			
    			if ((!isset($res['description_short'])) || ($res['description_short'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', $res['description_short']); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    			
    			if ((!isset($res['description_full'])) || ($res['description_full'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', $res['description_full']); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    			
    			if ((!isset($res['conseils_pro'])) || ($res['conseils_pro'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', $res['conseils_pro']); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    			
    			if ((!isset($res['tva'])) || ($res['tva'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', $res['tva']); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    			
    			if ((!isset($res['price_unit_ttc'])) || ($res['price_unit_ttc'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', $res['price_unit_ttc']); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    
    			if ((!isset($res['price_unit_ht'])) || ($res['price_unit_ht'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', $res['price_unit_ht']); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    			
    			$line .= '"' . $row['qauntity']. '"' . "\t";
    						
    			if ((!isset($res['available'])) || ($res['available'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				if ($res['available'] ==0) {$available = 'non';} else {$available = 'oui';}
    				$value = str_replace('"', '""', $available); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    			
    			if ((!isset($res['conditionnement'])) || ($res['conditionnement'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', $res['weight']); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    
    			
    			if ((!isset($res['weight'])) || ($res['weight'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', $res['weight']); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    
    			if ((!isset($res['supplier'])) || ($res['supplier'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', $res['supplier']); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    
    			if ((!isset($res['color'])) || ($res['color'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', $res['color']); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    			
    			if ((!isset($res['cat_id'])) || ($res['cat_id'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				
    				$value = str_replace('"', '""', item_data_2var ('category', 'description', 'id', $res['cat_id'], 'lg', $_GET[lg])); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    			$line .= $value;
    						
    			if ((!isset($res['subcat_id'])) || ($res['subcat_id'] == "")) { 
    				$value = "\t"; 
    			} else { 
    				$value = str_replace('"', '""', item_data_2var ('subcategory', 'title', 'id', $res['subcat_id'], 'lg', $_GET[lg])); 
    				$value = '"' . $value . '"' . "\t"; 
    			} 
    
    
    	}
    
    		$data .= trim($line)."\t" ."\n"; 
    } 
     $data = str_replace("\r","",$data); 
    
    
    if ($data == "") { 
        $data = "\n(0) Records Found!\n";
    } 
    
    
    $query_user = mysql_query("SELECT * FROM TABLE_NAME_3 WHERE id='$user_id'");
    $user_field = mysql_fetch_array($query_user);
    
    	$bound="spravkaweb-1234";
    	$header.="From: info@info.info\n";
    	$header.="mime-version: 1.0\n";
    	$header.="content-type: multipart/mixed; boundary=\"$bound\"";
    	$body="\n\n--$bound\n";
    	$body.="content-type: text/html; charset=utf-8\n";
    	$body.="content-transfer-encoding: quoted-printable\n\n";
    	$body.="Nom:  ".$user_field['name']."<br>\n";
    	$body.="Prenom: ".$user_field['lastname']."<br>\n";
    	$body.="Message:  ajsdf;kasj;dflk<br>\n";
    	$body.="\n\n--$bound\n";
    	$body.="content-type: application/vnd.ms-excel; charset=utf-8\n";
    	$body.="\t"."name=\"report.xls\"\n";
    	$body.="content-transfer-encoding: quoted-printable\"\n";
    	$body.="content-disposition:attachment;\n";
    	$body.="\t"."filename=\"report.xls\"\n";
    	$body.="\n$header1"."\n" . "$data";
    	$body.="\n--$bound--\n\n";
    	
    	mail($email, '', $body, $header);
    	mail('info@info.info', '', $body, $header);
    	header("Location: index.php?page=monparier&lg=$_GET[lg]&send=send");
    ?>
    
    Code (markup):
     
    ludwig, Dec 4, 2006 IP