If $row is an array you can just do something like:
function getCSV() {
$mun = $this->input->POST('municipality');
$sl = $this->input->POST('saleslimit');
$q = $this->db->query("
SELECT
RollNum,
Address,
v2_lat,
v2_lng
FROM
tblontario
WHERE
Municipality = '".$mun."'"."
LIMIT ".$sl
);
$row = $q->result();
$csvRow = implode(",",$row);
file_put_contents('/home/apts/Dropbox/ptax.ca/js/salecomps.csv', $csvRow);
echo $csvRow;
}
Since I see a limit there, I'm guessing you're getting more than one result and that $q->result(); returns an array of results instead of just a row, in which case..
function getCSV() {
$mun = $this->input->POST('municipality');
$sl = $this->input->POST('saleslimit');
$q = $this->db->query("
SELECT
RollNum,
Address,
v2_lat,
v2_lng
FROM
tblontario
WHERE
Municipality = '".$mun."'"."
LIMIT ".$sl
);
$res = $q->result();
if(!empty($res)){
$fh = fopen('/home/apts/Dropbox/ptax.ca/js/salecomps.csv','w');
foreach($res as $row){
// as suggested by @GigaWatt, swapped fwrite for fputcsv
fputcsv($fh,$row);
}
fclose($fh);
echo file_get_contents('/home/apts/Dropbox/ptax.ca/js/salecomps.csv');
} else {
echo "";
}
}
EDIT: Looking a GeoJSON spec, what you prob want is something like:
function getGeoJSON() {
$mun = $this->input->POST('municipality');
$sl = $this->input->POST('saleslimit');
$q = $this->db->query("
SELECT
RollNum,
Address,
v2_lat,
v2_lng
FROM
tblontario
WHERE
Municipality = '".$mun."'"."
LIMIT ".$sl
);
$res = $q->result();
if(!empty($res)){
$geoArr = Array(
"type" => "MultiPoint",
"coordinates" => Array()
);
foreach($res as $row) {
$geoArr["coordinates"][] = Array($row['v2_lat'],$row['v2_lng']);
}
$geoJSON = json_encode($geoArr);
file_put_contents('/home/apts/Dropbox/ptax.ca/js/salecomps.geojson',$geoJSON);
echo $geoJSON;
} else {
echo "{}";
}
}