Hi
I don't know if the thread title really makes sense but basically i have a query that returns the results array and then i loop through it to output a csv file
the problem is that I use the db table col names as col names in the csv and when i loop through it outputs the col name and the col number for each one - - then it outputs each variable value twice, too, like this :
client_id;0;client_nom;1;client_prenom;2;client_dob;3;client_email;4;client_titre;5;client_adresse;6;client_cp;7;client_ville;8;client_tel;9;client_pays;10;client_lang;11;client_date;12;client_how;13;
83;83;Johnson;Johnson;Susan;Susan;0000-00-00;0000-00-00;Susan@Johnson.com;Susan@Johnson.com;ms;ms;kuykuyfk uyfkuyfky fkyf kuyfkuyfk;kuykuyfk uyfkuyfky fkyf kuyfkuyfk;12345;12345;London;London;12 34 56 78 90;12 34 56 78 90;England;England;en;en;2008-11-17;2008-11-17;blah;blah;
the query code is this :
function selectQuery($conn,$sql){
op($sql);
$out = array();
$sth = $conn -> prepare($sql);
$sth->execute();
$res = $sth->fetchAll();
$sth->closeCursor();
//print_r($res);
if (!$res) {
op("no res");
//print_r($conn->errorInfo());
}else{
$i = 1;
foreach ($res as $row) {
$out[$i] = array();
foreach($row as $k => $v){
$out[$i][$k] = $v;
}
$i++;
}
}
return $out;
}
and the csv outputting is this :
$sql = "SELECT * FROM `client` WHERE ".$sqlPartsToGetTotalResults;
$export = selectQuery($conn,$sql);
$header = "";
foreach($export[1] as $k => $v){
$header .= $k.";";
}
$header .= "\r\n";
$data = "";
for($c=1;$c<=count($export);$c++){
foreach($export[$c] as $k => $v){
$data .= $v.";";
}
$data .= "\r\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.txt");
header("Pragma: no-cache");
header("Expires: 0");
print($header."\n".$data);
exit;
what would i need to change so that i just output the named key/value pairs ?
thanks
PS is there any way to force the output file into UTF-8 mode ?